annotate rust/rhg/src/main.rs @ 45451:a6a000ab135b

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