Add help_text()

Replaces the help() functions with help_text(), which returns the help_text of imag.
Use the .help() function of clap::App to overwrite the help text generated by clap
Remove unneeded argument '--help', generated by clap now
This commit is contained in:
mario 2016-09-03 14:58:14 +02:00 committed by Matthias Beyer
parent 1900d6922c
commit 882b2ef5a7
1 changed files with 12 additions and 21 deletions

View File

@ -19,8 +19,8 @@ use clap::{Arg, App, AppSettings, SubCommand};
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagrt::setup::generate_runtime_setup; use libimagrt::setup::generate_runtime_setup;
fn help(cmds: Vec<String>) { fn help_text(cmds: Vec<String>) -> String {
println!(r#" let text = format!(r#"
_ _
(_)_ __ ___ __ _ __ _ (_)_ __ ___ __ _ __ _
@ -39,13 +39,8 @@ fn help(cmds: Vec<String>) {
modules can be used independently. modules can be used independently.
Available commands: Available commands:
"#);
for cmd in cmds.iter() { {imagbins}
println!("\t{}", cmd);
}
println!(r#"
Call a command with 'imag <command> <args>' Call a command with 'imag <command> <args>'
Each command can be called with "--help" to get the respective helptext. Each command can be called with "--help" to get the respective helptext.
@ -55,9 +50,16 @@ fn help(cmds: Vec<String>) {
imag is free software. It is released under the terms of LGPLv2.1 imag is free software. It is released under the terms of LGPLv2.1
(c) 2016 Matthias Beyer and contributors"#); (c) 2016 Matthias Beyer and contributors"#, imagbins = cmds.into_iter()
.map(|cmd| format!("\t{}\n", cmd))
.fold(String::new(), |s, c| {
let s = s + c.as_str();
s
}));
text
} }
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() {
@ -118,14 +120,8 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.required(false) .required(false)
.multiple(false) .multiple(false)
.help("Get the versions of the imag commands")) .help("Get the versions of the imag commands"))
.arg(Arg::with_name("help")
.long("help")
.short("h")
.takes_value(false)
.required(false)
.multiple(false)
.help("Show help"))
.subcommand(SubCommand::with_name("help").help("Show help")) .subcommand(SubCommand::with_name("help").help("Show help"))
.help(help_text(get_commands()))
} }
fn main() { fn main() {
@ -136,11 +132,6 @@ fn main() {
let matches = rt.cli(); let matches = rt.cli();
debug!("matches: {:?}", matches); debug!("matches: {:?}", matches);
if matches.is_present("help") {
debug!("Calling help()");
help(get_commands());
exit(0);
}
if matches.is_present("version") { if matches.is_present("version") {
debug!("Showing version"); debug!("Showing version");