Mercurial > hg
comparison rust/rhg/src/main.rs @ 45050:18f8d3b31baa
rhg: add a limited `rhg root` subcommand
Clap has been choosen for argument parsing for the following reasons:
- it's a wildly used and maintained crate
- it can deal with OS encoding making it suitable for any encoding
- it supports nonambiguous prefix matching as already available in hg
- it will soon allow for structopts-style declarative pattern instead of the
currently used builder pattern
Differential Revision: https://phab.mercurial-scm.org/D8613
author | Antoine Cezar <antoine.cezar@octobus.net> |
---|---|
date | Tue, 07 Jul 2020 14:05:15 +0530 |
parents | 513b3ef277a3 |
children | 47997afadf08 |
comparison
equal
deleted
inserted
replaced
45049:513b3ef277a3 | 45050:18f8d3b31baa |
---|---|
1 use clap::App; | |
2 use clap::AppSettings; | |
3 use clap::SubCommand; | |
4 | |
1 mod commands; | 5 mod commands; |
2 mod error; | 6 mod error; |
3 mod exitcode; | 7 mod exitcode; |
4 mod ui; | 8 mod ui; |
9 use commands::Command; | |
5 | 10 |
6 fn main() { | 11 fn main() { |
7 std::process::exit(exitcode::UNIMPLEMENTED_COMMAND) | 12 let mut app = App::new("rhg") |
13 .setting(AppSettings::AllowInvalidUtf8) | |
14 .setting(AppSettings::SubcommandRequired) | |
15 .setting(AppSettings::VersionlessSubcommands) | |
16 .version("0.0.1") | |
17 .subcommand( | |
18 SubCommand::with_name("root").about(commands::root::HELP_TEXT), | |
19 ); | |
20 | |
21 let matches = app.clone().get_matches_safe().unwrap_or_else(|_| { | |
22 std::process::exit(exitcode::UNIMPLEMENTED_COMMAND) | |
23 }); | |
24 | |
25 let command_result = match matches.subcommand_name() { | |
26 Some(name) => match name { | |
27 "root" => commands::root::RootCommand::new().run(), | |
28 _ => std::process::exit(exitcode::UNIMPLEMENTED_COMMAND), | |
29 }, | |
30 _ => { | |
31 match app.print_help() { | |
32 Ok(_) => std::process::exit(exitcode::OK), | |
33 Err(_) => std::process::exit(exitcode::ABORT), | |
34 }; | |
35 } | |
36 }; | |
37 | |
38 match command_result { | |
39 Ok(_) => std::process::exit(exitcode::OK), | |
40 Err(e) => e.exit(), | |
41 } | |
8 } | 42 } |