main.rs
57 lines
| 1.6 KiB
| application/rls-services+xml
|
RustLexer
|
r45593 | use clap::App; | ||
use clap::AppSettings; | ||||
use clap::SubCommand; | ||||
|
r45515 | mod commands; | ||
mod error; | ||||
|
r45503 | mod exitcode; | ||
|
r45592 | mod ui; | ||
|
r45593 | use commands::Command; | ||
|
r45503 | |||
fn main() { | ||||
|
r45593 | let mut app = App::new("rhg") | ||
.setting(AppSettings::AllowInvalidUtf8) | ||||
.setting(AppSettings::SubcommandRequired) | ||||
.setting(AppSettings::VersionlessSubcommands) | ||||
.version("0.0.1") | ||||
.subcommand( | ||||
SubCommand::with_name("root").about(commands::root::HELP_TEXT), | ||||
|
r45924 | ) | ||
.subcommand( | ||||
SubCommand::with_name("files").about(commands::files::HELP_TEXT), | ||||
|
r45593 | ); | ||
let matches = app.clone().get_matches_safe().unwrap_or_else(|_| { | ||||
std::process::exit(exitcode::UNIMPLEMENTED_COMMAND) | ||||
}); | ||||
|
r45920 | let ui = ui::Ui::new(); | ||
|
r45593 | let command_result = match matches.subcommand_name() { | ||
Some(name) => match name { | ||||
|
r45920 | "root" => commands::root::RootCommand::new(&ui).run(), | ||
|
r45924 | "files" => commands::files::FilesCommand::new(&ui).run(), | ||
|
r45593 | _ => std::process::exit(exitcode::UNIMPLEMENTED_COMMAND), | ||
}, | ||||
_ => { | ||||
match app.print_help() { | ||||
Ok(_) => std::process::exit(exitcode::OK), | ||||
Err(_) => std::process::exit(exitcode::ABORT), | ||||
}; | ||||
} | ||||
}; | ||||
match command_result { | ||||
Ok(_) => std::process::exit(exitcode::OK), | ||||
|
r45920 | Err(e) => { | ||
let message = e.get_error_message_bytes(); | ||||
if let Some(msg) = message { | ||||
match ui.write_stderr(&msg) { | ||||
Ok(_) => (), | ||||
Err(_) => std::process::exit(exitcode::ABORT), | ||||
}; | ||||
}; | ||||
e.exit() | ||||
} | ||||
|
r45593 | } | ||
|
r45503 | } | ||