11use std:: ffi:: { OsStr , OsString } ;
22use std:: path:: PathBuf ;
33
4- use cargo:: util:: command_prelude:: { flag, multi_opt, opt} ;
4+ use camino:: Utf8PathBuf ;
5+ use cargo:: util:: command_prelude:: { flag, heading, multi_opt, opt} ;
56use cargo:: util:: command_prelude:: { get_ws_member_candidates, CommandExt } ;
67use cargo:: util:: { style, CliError , CliResult } ;
78
89use cargo_util:: { ProcessBuilder , ProcessError } ;
910
11+ use clap:: builder:: { StringValueParser , TypedValueParser } ;
1012use clap:: { Arg , ArgAction , ArgMatches , Command , CommandFactory , Parser } ;
1113use clap_complete:: ArgValueCandidates ;
1214
@@ -58,6 +60,37 @@ struct Common {
5860 meson : bool ,
5961}
6062
63+ trait CommandExtC : CommandExt {
64+ fn arg_manifest_path_alias_path ( self , with_alias : bool ) -> Self {
65+ let o = opt ( "manifest-path" , "Path to Cargo.toml" )
66+ . value_name ( "PATH" )
67+ . help_heading ( heading:: MANIFEST_OPTIONS )
68+ . value_parser ( StringValueParser :: new ( ) . map ( |p| {
69+ let mut p = Utf8PathBuf :: from ( p) ;
70+ if p. is_dir ( ) {
71+ p. push ( "Cargo.toml" ) ;
72+ }
73+ p. into_string ( )
74+ } ) )
75+ . add ( clap_complete:: engine:: ArgValueCompleter :: new (
76+ clap_complete:: engine:: PathCompleter :: any ( ) . filter ( |path| {
77+ path. join ( "Cargo.toml" ) . exists ( )
78+ || path. file_name ( ) == Some ( OsStr :: new ( "Cargo.toml" ) )
79+ || cargo:: util:: toml:: is_embedded ( path)
80+ } ) ,
81+ ) ) ;
82+
83+ let o = if with_alias {
84+ o. visible_alias ( "path" )
85+ } else {
86+ o
87+ } ;
88+ self . _arg ( o)
89+ }
90+ }
91+
92+ impl CommandExtC for Command { }
93+
6194pub fn main_cli ( ) -> Command {
6295 let styles = {
6396 clap:: builder:: styling:: Styles :: styled ( )
@@ -136,7 +169,6 @@ fn base_cli() -> Command {
136169 . arg_features ( )
137170 . arg_target_triple ( "Build for the target triple" )
138171 . arg_target_dir ( )
139- . arg_manifest_path ( )
140172 . arg_message_format ( ) ;
141173
142174 if let Ok ( t) = default_target {
@@ -168,6 +200,7 @@ pub fn subcommand_build(name: &'static str, about: &'static str) -> Command {
168200 "Exclude packages from the build" ,
169201 ArgValueCandidates :: new ( get_ws_member_candidates) ,
170202 )
203+ . arg_manifest_path_alias_path ( false )
171204 . after_help (
172205 "
173206Compilation can be configured via the use of profiles which are configured in
@@ -191,6 +224,7 @@ pub fn subcommand_install(name: &'static str, about: &'static str) -> Command {
191224 "Exclude packages from being installed" ,
192225 ArgValueCandidates :: new ( get_ws_member_candidates) ,
193226 )
227+ . arg_manifest_path_alias_path ( true )
194228 . after_help (
195229 "
196230Compilation can be configured via the use of profiles which are configured in
@@ -222,6 +256,7 @@ pub fn subcommand_test(name: &'static str) -> Command {
222256 "Exclude packages from the test" ,
223257 ArgValueCandidates :: new ( get_ws_member_candidates) ,
224258 )
259+ . arg_manifest_path_alias_path ( false )
225260 . arg ( flag ( "no-run" , "Compile, but don't run tests" ) )
226261 . arg ( flag ( "no-fail-fast" , "Run all tests regardless of failure" ) )
227262}
0 commit comments