Mercurial > public > mercurial-scm > hg
annotate rust/rhg/src/main.rs @ 45361:47997afadf08
rhg: ask the error message from `CommandError`
Avoid repeating the display of the same error messages in different commands.
Differential Revision: https://phab.mercurial-scm.org/D8865
author | Antoine Cezar <antoine.cezar@octobus.net> |
---|---|
date | Mon, 20 Jul 2020 18:14:52 +0200 |
parents | 18f8d3b31baa |
children | 26440adbe3e9 |
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), |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
19 ); |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
20 |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
21 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
|
22 std::process::exit(exitcode::UNIMPLEMENTED_COMMAND) |
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 |
45361
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
25 let ui = ui::Ui::new(); |
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
26 |
45050
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
27 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
|
28 Some(name) => match name { |
45361
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
29 "root" => commands::root::RootCommand::new(&ui).run(), |
45050
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
30 _ => std::process::exit(exitcode::UNIMPLEMENTED_COMMAND), |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
31 }, |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
32 _ => { |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
33 match app.print_help() { |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
34 Ok(_) => std::process::exit(exitcode::OK), |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
35 Err(_) => std::process::exit(exitcode::ABORT), |
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 } |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
38 }; |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
39 |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
40 match command_result { |
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
41 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
|
42 Err(e) => { |
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
43 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
|
44 if let Some(msg) = message { |
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
45 match ui.write_stderr(&msg) { |
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
46 Ok(_) => (), |
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
47 Err(_) => std::process::exit(exitcode::ABORT), |
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
48 }; |
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
49 }; |
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
50 e.exit() |
47997afadf08
rhg: ask the error message from `CommandError`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45050
diff
changeset
|
51 } |
45050
18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
45049
diff
changeset
|
52 } |
44981 | 53 } |