How to load binaries like cargo-web.exe in Clap?


#1

Is it possible to interrupt parsing after astro air, for example, once it ultrapasses 'air'?

extern crate clap;
use clap::{App, Arg, ArgGroup, SubCommand};

// mod config;
// mod storage;

fn main() {
    let app = App::new("Astro")
        .version("0.1.0")
        .subcommand(SubCommand::with_name("new")
            .group(ArgGroup::with_name("type")
                .required(true))
            .arg(Arg::from_usage("--bin [dev-type]").group("type"))
            .arg(Arg::from_usage("--lib [dev-type]").group("type"))
            .arg_from_usage("<name>"))
        .subcommand(SubCommand::with_name("air"));

    // Got to figure in how to add sym-link
    // commands w/ clap.
//  storage::devtools::commands(&app);

    let matches = app.get_matches();

    match matches.subcommand_name() {
        Some("new") => println!("Rant new"),
        Some("air") => println!("{}", std::env::args().nth(0).unwrap().as_str()),
        None => (),
        _ => println!("Missing impl")
    }
}

Not really working.


#2

So far I didn’t find anything related in Cargo’s source (this is where all the CLI goes). To clarify I want to know how cargo web works. How does cargo-web.exe runs as a subcommand in cargo if it’s not a built-in one?

It looks like my GitHub issue won’t be answered too and I’m not wanting to waste time again like my threwn away parsers.


#3

If I’m reading the source correctly, cargo matches on clap errors and if it’s an unrecognized subcommand, it will try to execute an external binary cargo-<subcommandname>:

and execute_external_subcommand:

There’s probably more to it than just this, but the rest can probably be found by digging around in cargo’s source searching for usages of execute_external_subcommand and the like?


#4

It’s not always ::UnrecognizedSubcommand, so…

if let Err(err) = result {
        match err.kind {
              ErrorKind::UnrecognizedSubcommand
            | ErrorKind::UnknownArgument => {
                let name = &err.info.unwrap()[0];
                println!("Probably air");
                return;
            },
            _ => {
                if err.use_stderr() {
                    err.exit();
                }
                return;
            }
        }
    }