Add some comments
This commit is contained in:
parent
c828bed0e1
commit
d69b8498e9
1 changed files with 17 additions and 8 deletions
|
@ -20,6 +20,8 @@ use clap::{Arg, AppSettings, SubCommand};
|
||||||
use libimagrt::runtime::Runtime;
|
use libimagrt::runtime::Runtime;
|
||||||
use libimagerror::trace::trace_error;
|
use libimagerror::trace::trace_error;
|
||||||
|
|
||||||
|
/// Returns the helptext, putting the Strings in cmds as possible
|
||||||
|
/// subcommands into it
|
||||||
fn help_text(cmds: Vec<String>) -> String {
|
fn help_text(cmds: Vec<String>) -> String {
|
||||||
let text = format!(r#"
|
let text = format!(r#"
|
||||||
|
|
||||||
|
@ -60,7 +62,7 @@ fn help_text(cmds: Vec<String>) -> String {
|
||||||
text
|
text
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the list of imag-* executables found in $PATH
|
||||||
fn get_commands() -> Vec<String> {
|
fn get_commands() -> Vec<String> {
|
||||||
let path = env::var("PATH");
|
let path = env::var("PATH");
|
||||||
if path.is_err() {
|
if path.is_err() {
|
||||||
|
@ -108,11 +110,12 @@ fn get_commands() -> Vec<String> {
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
// Initialize the Runtime and build the CLI
|
||||||
let appname = "imag";
|
let appname = "imag";
|
||||||
let version = &version!();
|
let version = &version!();
|
||||||
let about = "imag - the PIM suite for the commandline";
|
let about = "imag - the PIM suite for the commandline";
|
||||||
let commands = get_commands();
|
let commands = get_commands();
|
||||||
let helptext = help_text(commands);
|
let helptext = help_text(commands.clone());
|
||||||
let app = Runtime::get_default_cli_builder(appname, version, about)
|
let app = Runtime::get_default_cli_builder(appname, version, about)
|
||||||
.settings(&[AppSettings::AllowExternalSubcommands, AppSettings::ArgRequiredElseHelp])
|
.settings(&[AppSettings::AllowExternalSubcommands, AppSettings::ArgRequiredElseHelp])
|
||||||
.arg(Arg::with_name("version")
|
.arg(Arg::with_name("version")
|
||||||
|
@ -139,6 +142,8 @@ fn main() {
|
||||||
|
|
||||||
debug!("matches: {:?}", matches);
|
debug!("matches: {:?}", matches);
|
||||||
|
|
||||||
|
// Begin checking for arguments
|
||||||
|
|
||||||
if matches.is_present("version") {
|
if matches.is_present("version") {
|
||||||
debug!("Showing version");
|
debug!("Showing version");
|
||||||
println!("imag {}", &version!()[..]);
|
println!("imag {}", &version!()[..]);
|
||||||
|
@ -168,15 +173,19 @@ fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Matches any subcommand given
|
||||||
match matches.subcommand() {
|
match matches.subcommand() {
|
||||||
(subcommand, Some(scmd)) => {
|
(subcommand, Some(scmd)) => {
|
||||||
debug!("Calling with subcommand: {}", subcommand);
|
// Get all given arguments and further subcommands to pass to
|
||||||
|
// the imag-<> binary
|
||||||
|
// Providing no arguments is OK, and is therefore ignored here
|
||||||
let subcommand_args : Vec<&str> = match scmd.values_of("") {
|
let subcommand_args : Vec<&str> = match scmd.values_of("") {
|
||||||
Some(values) => values.collect(),
|
Some(values) => values.collect(),
|
||||||
None => Vec::new()
|
None => Vec::new()
|
||||||
};
|
};
|
||||||
|
|
||||||
if !get_commands().contains(&String::from(subcommand)) {
|
// Typos happen, so check if the given subcommand is one found in $PATH
|
||||||
|
if !commands.clone().contains(&String::from(subcommand)) {
|
||||||
println!("No such command: 'imag-{}'", subcommand);
|
println!("No such command: 'imag-{}'", subcommand);
|
||||||
println!("See 'imag --help' for available subcommands");
|
println!("See 'imag --help' for available subcommands");
|
||||||
exit(2);
|
exit(2);
|
||||||
|
@ -184,6 +193,7 @@ fn main() {
|
||||||
|
|
||||||
debug!("Calling 'imag-{}' with args: {:?}", subcommand, subcommand_args);
|
debug!("Calling 'imag-{}' with args: {:?}", subcommand, subcommand_args);
|
||||||
|
|
||||||
|
// Create a Command, and pass it the gathered arguments
|
||||||
match Command::new(format!("imag-{}", subcommand))
|
match Command::new(format!("imag-{}", subcommand))
|
||||||
.stdin(Stdio::inherit())
|
.stdin(Stdio::inherit())
|
||||||
.stdout(Stdio::inherit())
|
.stdout(Stdio::inherit())
|
||||||
|
@ -223,9 +233,8 @@ fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
// clap ensures we have valid input by exiting if not.
|
// Calling for example 'imag --versions' will lead here, as this option does not exit.
|
||||||
// The above case is a catch-all for subcommands,
|
// There's nothing to do in such a case
|
||||||
// so nothing else needs to be expexted.
|
_ => {},
|
||||||
_ => unreachable!(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue