Mercurial > hg
annotate rust/rhg/src/main.rs @ 45438:ed95ccc94333
rhg: pass `ui` to `Command` `run`
Allow implementation of `From<clap::ArgMatches> for Command`
Differential Revision: https://phab.mercurial-scm.org/D8954
author | Antoine Cezar <antoine.cezar@octobus.net> |
---|---|
date | Tue, 04 Aug 2020 16:11:23 +0200 |
parents | 26440adbe3e9 |
children | a6a000ab135b |
rev | line source |
---|---|
45050
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
1 use clap::App; |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
2 use clap::AppSettings; |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
3 use clap::SubCommand; |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
4 |
44982
bacf6c7ef01b
rhg: add Command trait for subcommands implemented by rhg
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44981
diff
changeset
|
5 mod commands; |
bacf6c7ef01b
rhg: add Command trait for subcommands implemented by rhg
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44981
diff
changeset
|
6 mod error; |
44981 | 7 mod exitcode; |
45049
513b3ef277a3
rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44982
diff
changeset
|
8 mod ui; |
45050
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
9 use commands::Command; |
44981 | 10 |
11 fn main() { | |
45050
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
12 let mut app = App::new("rhg") |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
13 .setting(AppSettings::AllowInvalidUtf8) |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
14 .setting(AppSettings::SubcommandRequired) |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
15 .setting(AppSettings::VersionlessSubcommands) |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
16 .version("0.0.1") |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
17 .subcommand( |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
18 SubCommand::with_name("root").about(commands::root::HELP_TEXT), |
45365
26440adbe3e9
rhg: add a limited `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45361
diff
changeset
|
19 ) |
26440adbe3e9
rhg: add a limited `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45361
diff
changeset
|
20 .subcommand( |
26440adbe3e9
rhg: add a limited `rhg files` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45361
diff
changeset
|
21 SubCommand::with_name("files").about(commands::files::HELP_TEXT), |
45050
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
22 ); |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
23 |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
24 let matches = app.clone().get_matches_safe().unwrap_or_else(|_| { |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
25 std::process::exit(exitcode::UNIMPLEMENTED_COMMAND) |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
26 }); |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
27 |
45361
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
28 let ui = ui::Ui::new(); |
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
29 |
45050
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
30 let command_result = match matches.subcommand_name() { |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
31 Some(name) => match name { |
45438
ed95ccc94333
rhg: pass `ui` to `Command` `run`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45365
diff
changeset
|
32 "root" => commands::root::RootCommand::new().run(&ui), |
ed95ccc94333
rhg: pass `ui` to `Command` `run`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45365
diff
changeset
|
33 "files" => commands::files::FilesCommand::new().run(&ui), |
45050
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
34 _ => std::process::exit(exitcode::UNIMPLEMENTED_COMMAND), |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
35 }, |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
36 _ => { |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
37 match app.print_help() { |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
38 Ok(_) => std::process::exit(exitcode::OK), |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
39 Err(_) => std::process::exit(exitcode::ABORT), |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
40 }; |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
41 } |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
42 }; |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
43 |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
44 match command_result { |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
45 Ok(_) => std::process::exit(exitcode::OK), |
45361
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
46 Err(e) => { |
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
47 let message = e.get_error_message_bytes(); |
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
48 if let Some(msg) = message { |
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
49 match ui.write_stderr(&msg) { |
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
50 Ok(_) => (), |
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
51 Err(_) => std::process::exit(exitcode::ABORT), |
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
52 }; |
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
53 }; |
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
54 e.exit() |
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
55 } |
45050
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
56 } |
44981 | 57 } |