These are chat archives for kbknapp/clap-rs

25th
Mar 2017
Jessica Stokes
@ticky
Mar 25 2017 02:45
Hi there, I’m trying to build an app which requires a subcommand, and otherwise prints help and exits with an error code. I can’t see a way to configure this behaviour (please let me know if I’ve missed it!), but I can’t seem to get it to work manually either, as after having parsed the options, I no longer have access to the App object which is the only thing which has print_help available. Is there a way to make this work?
fn main() {
    let mut app = app_from_crate!()
        .subcommand(SubCommand::with_name("test").about("Tests the game controller subsystem"));

    let matches = app.get_matches();

    match matches.subcommand_name() {
        Some("test") => {
            test();
        },
        None => {
            app.print_help();
        },
        _ => ()
    }
}
Kevin K.
@kbknapp
Mar 25 2017 02:48
@mersinvald sorry I missed your message. Use App::usage
it does exactly that for you ;)
fn main() {
    let mut app = app_from_crate!()
        .setting(AppSettings::SubcommandRequiredElseHelp)
        .subcommand(SubCommand::with_name("test").about("Tests the game controller subsystem"));

    let matches = app.get_matches();
}
Used like that
Vincent Prouillet
@Keats
Mar 25 2017 03:45
@kbknapp I can't find it in the code, is there a way to set a default arg for an arg using clap_app!?
Jessica Stokes
@ticky
Mar 25 2017 03:52
@kbknapp aha! Thanks a bunch 😃
Vincent Prouillet
@Keats
Mar 25 2017 04:01
(it's easy to do manually after the matches, just wondering if there's a built-in shortcut)
Kevin K.
@kbknapp
Mar 25 2017 13:12
@Keats there's no shortcut, but you should be able to do default_value("foo') inside the macro
"foo"*