##// END OF EJS Templates
rhg: Use clap’s support for global CLI arguments...
Simon Sapin -
r47351:4e4c7040 default
parent child Browse files
Show More
@@ -15,39 +15,38 b' mod exitcode;'
15 mod ui;
15 mod ui;
16 use error::CommandError;
16 use error::CommandError;
17
17
18 fn add_global_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> {
19 app.arg(
20 Arg::with_name("repository")
21 .help("repository root directory")
22 .short("-R")
23 .long("--repository")
24 .value_name("REPO")
25 .takes_value(true),
26 )
27 .arg(
28 Arg::with_name("config")
29 .help("set/override config option (use 'section.name=value')")
30 .long("--config")
31 .value_name("CONFIG")
32 .takes_value(true)
33 // Ok: `--config section.key1=val --config section.key2=val2`
34 .multiple(true)
35 // Not ok: `--config section.key1=val section.key2=val2`
36 .number_of_values(1),
37 )
38 }
39
40 fn main_with_result(
18 fn main_with_result(
41 ui: &ui::Ui,
19 ui: &ui::Ui,
42 process_start_time: &blackbox::ProcessStartTime,
20 process_start_time: &blackbox::ProcessStartTime,
43 ) -> Result<(), CommandError> {
21 ) -> Result<(), CommandError> {
44 env_logger::init();
22 env_logger::init();
45 let app = App::new("rhg")
23 let app = App::new("rhg")
46 .setting(AppSettings::AllowInvalidUtf8)
24 .global_setting(AppSettings::AllowInvalidUtf8)
47 .setting(AppSettings::SubcommandRequired)
25 .setting(AppSettings::SubcommandRequired)
48 .setting(AppSettings::VersionlessSubcommands)
26 .setting(AppSettings::VersionlessSubcommands)
27 .arg(
28 Arg::with_name("repository")
29 .help("repository root directory")
30 .short("-R")
31 .long("--repository")
32 .value_name("REPO")
33 .takes_value(true)
34 // Both ok: `hg -R ./foo log` or `hg log -R ./foo`
35 .global(true),
36 )
37 .arg(
38 Arg::with_name("config")
39 .help("set/override config option (use 'section.name=value')")
40 .long("--config")
41 .value_name("CONFIG")
42 .takes_value(true)
43 .global(true)
44 // Ok: `--config section.key1=val --config section.key2=val2`
45 .multiple(true)
46 // Not ok: `--config section.key1=val section.key2=val2`
47 .number_of_values(1),
48 )
49 .version("0.0.1");
49 .version("0.0.1");
50 let app = add_global_args(app);
51 let app = add_subcommand_args(app);
50 let app = add_subcommand_args(app);
52
51
53 let matches = app.clone().get_matches_safe()?;
52 let matches = app.clone().get_matches_safe()?;
@@ -58,26 +57,15 b' fn main_with_result('
58 let subcommand_args = subcommand_matches
57 let subcommand_args = subcommand_matches
59 .expect("no subcommand arguments from clap despite AppSettings::SubcommandRequired");
58 .expect("no subcommand arguments from clap despite AppSettings::SubcommandRequired");
60
59
61 // Global arguments can be in either based on e.g. `hg -R ./foo log` v.s.
60 let config_args = matches
62 // `hg log -R ./foo`
61 .values_of_os("config")
63 let value_of_global_arg = |name| {
62 // Turn `Option::None` into an empty iterator:
64 subcommand_args
63 .into_iter()
65 .value_of_os(name)
64 .flatten()
66 .or_else(|| matches.value_of_os(name))
67 };
68 // For arguments where multiple occurences are allowed, return a
69 // possibly-iterator of all values.
70 let values_of_global_arg = |name: &str| {
71 let a = matches.values_of_os(name).into_iter().flatten();
72 let b = subcommand_args.values_of_os(name).into_iter().flatten();
73 a.chain(b)
74 };
75
76 let config_args = values_of_global_arg("config")
77 .map(hg::utils::files::get_bytes_from_os_str);
65 .map(hg::utils::files::get_bytes_from_os_str);
78 let non_repo_config = &hg::config::Config::load(config_args)?;
66 let non_repo_config = &hg::config::Config::load(config_args)?;
79
67
80 let repo_path = value_of_global_arg("repository").map(Path::new);
68 let repo_path = matches.value_of_os("repository").map(Path::new);
81 let repo = match Repo::find(non_repo_config, repo_path) {
69 let repo = match Repo::find(non_repo_config, repo_path) {
82 Ok(repo) => Ok(repo),
70 Ok(repo) => Ok(repo),
83 Err(RepoError::NotFound { at }) if repo_path.is_none() => {
71 Err(RepoError::NotFound { at }) if repo_path.is_none() => {
@@ -141,7 +129,7 b' macro_rules! subcommands {'
141 fn add_subcommand_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> {
129 fn add_subcommand_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> {
142 app
130 app
143 $(
131 $(
144 .subcommand(add_global_args(commands::$command::args()))
132 .subcommand(commands::$command::args())
145 )+
133 )+
146 }
134 }
147
135
General Comments 0
You need to be logged in to leave comments. Login now