rust/rhg/src/main.rs
changeset 48732 d4a5c2197208
parent 48731 f591b377375f
child 48733 39c447e03dbc
equal deleted inserted replaced
48731:f591b377375f 48732:d4a5c2197208
    64                 .long("--cwd")
    64                 .long("--cwd")
    65                 .value_name("DIR")
    65                 .value_name("DIR")
    66                 .takes_value(true)
    66                 .takes_value(true)
    67                 .global(true),
    67                 .global(true),
    68         )
    68         )
       
    69         .arg(
       
    70             Arg::with_name("color")
       
    71                 .help("when to colorize (boolean, always, auto, never, or debug)")
       
    72                 .long("--color")
       
    73                 .value_name("TYPE")
       
    74                 .takes_value(true)
       
    75                 .global(true),
       
    76         )
    69         .version("0.0.1");
    77         .version("0.0.1");
    70     let app = add_subcommand_args(app);
    78     let app = add_subcommand_args(app);
    71 
    79 
    72     let matches = app.clone().get_matches_safe()?;
    80     let matches = app.clone().get_matches_safe()?;
    73 
    81 
   177                 false,
   185                 false,
   178             )
   186             )
   179         });
   187         });
   180 
   188 
   181     non_repo_config
   189     non_repo_config
   182         .load_cli_args_config(early_args.config)
   190         .load_cli_args(early_args.config, early_args.color)
   183         .unwrap_or_else(|error| {
   191         .unwrap_or_else(|error| {
   184             exit(
   192             exit(
   185                 &initial_current_dir,
   193                 &initial_current_dir,
   186                 &Ui::new_infallible(&non_repo_config),
   194                 &Ui::new_infallible(&non_repo_config),
   187                 OnUnsupported::from_config(&non_repo_config),
   195                 OnUnsupported::from_config(&non_repo_config),
   524 /// These arguments are still declared when we do use Clap later, so that Clap
   532 /// These arguments are still declared when we do use Clap later, so that Clap
   525 /// does not return an error for their presence.
   533 /// does not return an error for their presence.
   526 struct EarlyArgs {
   534 struct EarlyArgs {
   527     /// Values of all `--config` arguments. (Possibly none)
   535     /// Values of all `--config` arguments. (Possibly none)
   528     config: Vec<Vec<u8>>,
   536     config: Vec<Vec<u8>>,
       
   537     /// Value of all the `--color` argument, if any.
       
   538     color: Option<Vec<u8>>,
   529     /// Value of the `-R` or `--repository` argument, if any.
   539     /// Value of the `-R` or `--repository` argument, if any.
   530     repo: Option<Vec<u8>>,
   540     repo: Option<Vec<u8>>,
   531     /// Value of the `--cwd` argument, if any.
   541     /// Value of the `--cwd` argument, if any.
   532     cwd: Option<Vec<u8>>,
   542     cwd: Option<Vec<u8>>,
   533 }
   543 }
   534 
   544 
   535 impl EarlyArgs {
   545 impl EarlyArgs {
   536     fn parse(args: impl IntoIterator<Item = OsString>) -> Self {
   546     fn parse(args: impl IntoIterator<Item = OsString>) -> Self {
   537         let mut args = args.into_iter().map(get_bytes_from_os_str);
   547         let mut args = args.into_iter().map(get_bytes_from_os_str);
   538         let mut config = Vec::new();
   548         let mut config = Vec::new();
       
   549         let mut color = None;
   539         let mut repo = None;
   550         let mut repo = None;
   540         let mut cwd = None;
   551         let mut cwd = None;
   541         // Use `while let` instead of `for` so that we can also call
   552         // Use `while let` instead of `for` so that we can also call
   542         // `args.next()` inside the loop.
   553         // `args.next()` inside the loop.
   543         while let Some(arg) = args.next() {
   554         while let Some(arg) = args.next() {
   547                 }
   558                 }
   548             } else if let Some(value) = arg.drop_prefix(b"--config=") {
   559             } else if let Some(value) = arg.drop_prefix(b"--config=") {
   549                 config.push(value.to_owned())
   560                 config.push(value.to_owned())
   550             }
   561             }
   551 
   562 
       
   563             if arg == b"--color" {
       
   564                 if let Some(value) = args.next() {
       
   565                     color = Some(value)
       
   566                 }
       
   567             } else if let Some(value) = arg.drop_prefix(b"--color=") {
       
   568                 color = Some(value.to_owned())
       
   569             }
       
   570 
   552             if arg == b"--cwd" {
   571             if arg == b"--cwd" {
   553                 if let Some(value) = args.next() {
   572                 if let Some(value) = args.next() {
   554                     cwd = Some(value)
   573                     cwd = Some(value)
   555                 }
   574                 }
   556             } else if let Some(value) = arg.drop_prefix(b"--cwd=") {
   575             } else if let Some(value) = arg.drop_prefix(b"--cwd=") {
   565                 repo = Some(value.to_owned())
   584                 repo = Some(value.to_owned())
   566             } else if let Some(value) = arg.drop_prefix(b"-R") {
   585             } else if let Some(value) = arg.drop_prefix(b"-R") {
   567                 repo = Some(value.to_owned())
   586                 repo = Some(value.to_owned())
   568             }
   587             }
   569         }
   588         }
   570         Self { config, repo, cwd }
   589         Self {
       
   590             config,
       
   591             color,
       
   592             repo,
       
   593             cwd,
       
   594         }
   571     }
   595     }
   572 }
   596 }
   573 
   597 
   574 /// What to do when encountering some unsupported feature.
   598 /// What to do when encountering some unsupported feature.
   575 ///
   599 ///