Show More
@@ -15,14 +15,24 b' mod exitcode;' | |||
|
15 | 15 | mod ui; |
|
16 | 16 | use error::CommandError; |
|
17 | 17 | |
|
18 | fn add_global_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> { | |
|
19 | app.arg( | |
|
18 | fn main_with_result( | |
|
19 | ui: &ui::Ui, | |
|
20 | process_start_time: &blackbox::ProcessStartTime, | |
|
21 | ) -> Result<(), CommandError> { | |
|
22 | env_logger::init(); | |
|
23 | let app = App::new("rhg") | |
|
24 | .global_setting(AppSettings::AllowInvalidUtf8) | |
|
25 | .setting(AppSettings::SubcommandRequired) | |
|
26 | .setting(AppSettings::VersionlessSubcommands) | |
|
27 | .arg( | |
|
20 | 28 | Arg::with_name("repository") |
|
21 | 29 | .help("repository root directory") |
|
22 | 30 | .short("-R") |
|
23 | 31 | .long("--repository") |
|
24 | 32 | .value_name("REPO") |
|
25 |
.takes_value(true) |
|
|
33 | .takes_value(true) | |
|
34 | // Both ok: `hg -R ./foo log` or `hg log -R ./foo` | |
|
35 | .global(true), | |
|
26 | 36 | ) |
|
27 | 37 | .arg( |
|
28 | 38 | Arg::with_name("config") |
@@ -30,24 +40,13 b" fn add_global_args<'a, 'b>(app: App<'a, " | |||
|
30 | 40 | .long("--config") |
|
31 | 41 | .value_name("CONFIG") |
|
32 | 42 | .takes_value(true) |
|
43 | .global(true) | |
|
33 | 44 | // Ok: `--config section.key1=val --config section.key2=val2` |
|
34 | 45 | .multiple(true) |
|
35 | 46 | // Not ok: `--config section.key1=val section.key2=val2` |
|
36 | 47 | .number_of_values(1), |
|
37 | 48 | ) |
|
38 | } | |
|
39 | ||
|
40 | fn main_with_result( | |
|
41 | ui: &ui::Ui, | |
|
42 | process_start_time: &blackbox::ProcessStartTime, | |
|
43 | ) -> Result<(), CommandError> { | |
|
44 | env_logger::init(); | |
|
45 | let app = App::new("rhg") | |
|
46 | .setting(AppSettings::AllowInvalidUtf8) | |
|
47 | .setting(AppSettings::SubcommandRequired) | |
|
48 | .setting(AppSettings::VersionlessSubcommands) | |
|
49 | 49 | .version("0.0.1"); |
|
50 | let app = add_global_args(app); | |
|
51 | 50 | let app = add_subcommand_args(app); |
|
52 | 51 | |
|
53 | 52 | let matches = app.clone().get_matches_safe()?; |
@@ -58,26 +57,15 b' fn main_with_result(' | |||
|
58 | 57 | let subcommand_args = subcommand_matches |
|
59 | 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. | |
|
62 | // `hg log -R ./foo` | |
|
63 | let value_of_global_arg = |name| { | |
|
64 | subcommand_args | |
|
65 | .value_of_os(name) | |
|
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") | |
|
60 | let config_args = matches | |
|
61 | .values_of_os("config") | |
|
62 | // Turn `Option::None` into an empty iterator: | |
|
63 | .into_iter() | |
|
64 | .flatten() | |
|
77 | 65 | .map(hg::utils::files::get_bytes_from_os_str); |
|
78 | 66 | let non_repo_config = &hg::config::Config::load(config_args)?; |
|
79 | 67 | |
|
80 |
let repo_path = value_of_ |
|
|
68 | let repo_path = matches.value_of_os("repository").map(Path::new); | |
|
81 | 69 | let repo = match Repo::find(non_repo_config, repo_path) { |
|
82 | 70 | Ok(repo) => Ok(repo), |
|
83 | 71 | Err(RepoError::NotFound { at }) if repo_path.is_none() => { |
@@ -141,7 +129,7 b' macro_rules! subcommands {' | |||
|
141 | 129 | fn add_subcommand_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> { |
|
142 | 130 | app |
|
143 | 131 | $( |
|
144 |
.subcommand |
|
|
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