rust/rhg/src/main.rs
changeset 46504 2e5dd18d6dc3
parent 46503 d8730ff51d5a
child 46505 a25033eb43b5
equal deleted inserted replaced
46503:d8730ff51d5a 46504:2e5dd18d6dc3
    17             .help("repository root directory")
    17             .help("repository root directory")
    18             .short("-R")
    18             .short("-R")
    19             .long("--repository")
    19             .long("--repository")
    20             .value_name("REPO")
    20             .value_name("REPO")
    21             .takes_value(true),
    21             .takes_value(true),
       
    22     )
       
    23     .arg(
       
    24         Arg::with_name("config")
       
    25             .help("set/override config option (use 'section.name=value')")
       
    26             .long("--config")
       
    27             .value_name("CONFIG")
       
    28             .takes_value(true)
       
    29             // Ok: `--config section.key1=val --config section.key2=val2`
       
    30             .multiple(true)
       
    31             // Not ok: `--config section.key1=val section.key2=val2`
       
    32             .number_of_values(1),
    22     )
    33     )
    23 }
    34 }
    24 
    35 
    25 fn main() {
    36 fn main() {
    26     env_logger::init();
    37     env_logger::init();
    45     let args = subcommand_matches
    56     let args = subcommand_matches
    46         .expect("no subcommand arguments from clap despite AppSettings::SubcommandRequired");
    57         .expect("no subcommand arguments from clap despite AppSettings::SubcommandRequired");
    47 
    58 
    48     // Global arguments can be in either based on e.g. `hg -R ./foo log` v.s.
    59     // Global arguments can be in either based on e.g. `hg -R ./foo log` v.s.
    49     // `hg log -R ./foo`
    60     // `hg log -R ./foo`
    50     let global_arg =
    61     let value_of_global_arg =
    51         |name| args.value_of_os(name).or_else(|| matches.value_of_os(name));
    62         |name| args.value_of_os(name).or_else(|| matches.value_of_os(name));
       
    63     // For arguments where multiple occurences are allowed, return a
       
    64     // possibly-iterator of all values.
       
    65     let values_of_global_arg = |name: &str| {
       
    66         let a = matches.values_of_os(name).into_iter().flatten();
       
    67         let b = args.values_of_os(name).into_iter().flatten();
       
    68         a.chain(b)
       
    69     };
    52 
    70 
    53     let repo_path = global_arg("repository").map(Path::new);
    71     let repo_path = value_of_global_arg("repository").map(Path::new);
    54     let result = (|| -> Result<(), CommandError> {
    72     let result = (|| -> Result<(), CommandError> {
    55         let config = hg::config::Config::load()?;
    73         let config_args = values_of_global_arg("config")
       
    74             // `get_bytes_from_path` works for OsStr the same as for Path
       
    75             .map(hg::utils::files::get_bytes_from_path);
       
    76         let config = hg::config::Config::load(config_args)?;
    56         run(&ui, &config, repo_path, args)
    77         run(&ui, &config, repo_path, args)
    57     })();
    78     })();
    58 
    79 
    59     let exit_code = match result {
    80     let exit_code = match result {
    60         Ok(_) => exitcode::OK,
    81         Ok(_) => exitcode::OK,