11use crate :: command_prelude:: * ;
2+ use anyhow:: bail;
3+ use cargo:: { drop_println, CargoResult } ;
24use serde:: Serialize ;
35
46pub fn cli ( ) -> App {
57 subcommand ( "locate-project" )
68 . about ( "Print a JSON representation of a Cargo.toml file's location" )
79 . arg ( opt ( "quiet" , "No output printed to stdout" ) . short ( "q" ) )
810 . arg_manifest_path ( )
11+ . arg (
12+ opt (
13+ "message-format" ,
14+ "Output representation [possible values: json, plain]" ,
15+ )
16+ . value_name ( "FMT" ) ,
17+ )
918 . after_help ( "Run `cargo help locate-project` for more detailed information.\n " )
1019}
1120
@@ -29,6 +38,29 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
2938
3039 let location = ProjectLocation { root } ;
3140
32- config. shell ( ) . print_json ( & location) ;
41+ match MessageFormat :: parse ( args) ? {
42+ MessageFormat :: Json => config. shell ( ) . print_json ( & location) ,
43+ MessageFormat :: Plain => drop_println ! ( config, "{}" , location. root) ,
44+ }
45+
3346 Ok ( ( ) )
3447}
48+
49+ enum MessageFormat {
50+ Json ,
51+ Plain ,
52+ }
53+
54+ impl MessageFormat {
55+ fn parse ( args : & ArgMatches < ' _ > ) -> CargoResult < Self > {
56+ let fmt = match args. value_of ( "message-format" ) {
57+ Some ( fmt) => fmt,
58+ None => return Ok ( MessageFormat :: Json ) ,
59+ } ;
60+ match fmt. to_ascii_lowercase ( ) . as_str ( ) {
61+ "json" => Ok ( MessageFormat :: Json ) ,
62+ "plain" => Ok ( MessageFormat :: Plain ) ,
63+ s => bail ! ( "invalid message format specifier: `{}`" , s) ,
64+ }
65+ }
66+ }
0 commit comments