These are chat archives for kbknapp/clap-rs

8th
Feb 2017
Richard Fussenegger
@Fleshgrinder
Feb 08 2017 21:41
Hello :)

I'm trying to use a global arg like so:

App::new("foo")
        .arg(Arg::with_name("quiet")
            .long("quiet")
            .short("q")
            .global(true)
            .help("Do not print any messages to standard output.")
        )
        .arg(Arg::with_name("verbose")
            .long("verbose")
            .short("v")
            .global(true)
            .multiple(true)
            .help("Increase verbosity of messages: `-v` normal, `-vv` more, and `-vvv` for debug.")
        )

But I'm getting the following error:

--- stderr
thread 'main' panicked at 'Non-unique argument name: quiet is already in use', ~/.cargo/registry/src/github.com-1ecc6299db9ec823/clap-2.20.3/src/app/parser.rs:147
note: Run with `RUST_BACKTRACE=1` for a backtrace.
I have a custom build.rs for creating the shell completion stuff, disabling it resolves the issue.
The content of that file is pretty much like advertised:
extern crate clap;

use clap::Shell;
use std::fs;

include!("src/cli.rs");

fn main() {
    let mut app = build_cli();
    let dir = env!("CARGO_MANIFEST_DIR").to_string() + "/resources/completion";

    fs::create_dir_all(&dir).unwrap();

    for shell in &[Shell::Bash, Shell::PowerShell] {
        app.gen_completions(env!("CARGO_PKG_NAME"),*shell, &dir);
    }
}
Should I create an issue?
Kevin K.
@kbknapp
Feb 08 2017 21:47
That doesn't appear to be related to completions. Is there more source? It's saying you've added 2 args with the name quiet
Are you using subcommands?
Richard Fussenegger
@Fleshgrinder
Feb 08 2017 21:47
Yes, I am.
Full content of cli.rs:
use clap::{App, AppSettings, Arg, SubCommand};

pub fn build_cli() -> App<'static, 'static> {
    App::new(env!("CARGO_PKG_NAME"))
        .author(env!("CARGO_PKG_AUTHORS"))
        .about(env!("CARGO_PKG_DESCRIPTION"))
        .bin_name(env!("CARGO_PKG_NAME"))
        .version(env!("CARGO_PKG_VERSION"))
        .global_settings(&[
            AppSettings::ColorAuto,
            AppSettings::GlobalVersion,
            AppSettings::StrictUtf8,
            AppSettings::UnifiedHelpMessage,
            AppSettings::VersionlessSubcommands,
        ])
        .arg(Arg::with_name("quiet")
            .long("quiet")
            .short("q")
            .global(true)
            .help("Do not print any messages to standard output.")
        )
        .arg(Arg::with_name("verbose")
            .long("verbose")
            .short("v")
            .global(true)
            .multiple(true)
            .help("Increase verbosity of messages: `-v` normal, `-vv` more, and `-vvv` for debug.")
        )
        .subcommand(SubCommand::with_name("install")
            .about("Install one or more PHP executables.")
        )
        .subcommand(SubCommand::with_name("uninstall")
            .about("Uninstall one or more PHP installations.")
        )
        .subcommand(SubCommand::with_name("update")
            .about("Update one or more PHP installations.")
        )
}
Full content of main.rs:
extern crate ansi_term;
extern crate clap;

mod cli;

use clap::ArgMatches;

fn main() {
    let args = cli::build_cli().get_matches();

    flags(&args);

    match args.subcommand() {
        ("install", Some(m))   => install(m),
        ("uninstall", Some(m)) => uninstall(m),
        ("update", Some(m))    => update(m),
        (_, _) => unreachable!(),
    }
}

fn install(args: &ArgMatches) {
    flags(args);
    println!("install");
}

fn uninstall(args: &ArgMatches) {
    flags(args);
    println!("uninstall");
}

fn update(args: &ArgMatches) {
    flags(args);
    println!("update");
}

fn flags(args: &ArgMatches) {
    if args.is_present("q") {
        println!("quiet");
    }

    match args.occurrences_of("v") {
        0 => println!("normal"),
        1 => println!("verbose"),
        2 => println!("very verbose"),
        3 | _ => println!("debug"),
    }
}
That's the MWE pretty much. ;)
Kevin K.
@kbknapp
Feb 08 2017 21:51
Ah I see the issue
Richard Fussenegger
@Fleshgrinder
Feb 08 2017 21:51
Shoot
Kevin K.
@kbknapp
Feb 08 2017 21:52
Move let mut app = build_cli(); inside the for loop
To build the completions
This is actually a bug, but that's the temporary fix
Richard Fussenegger
@Fleshgrinder
Feb 08 2017 21:53
It makes sense that it behaves that way but it probably shouldn't, yeah.
Kevin K.
@kbknapp
Feb 08 2017 21:53
Could you also file an issue? I'm traveling for work right now but should be able to fix it soon.
Richard Fussenegger
@Fleshgrinder
Feb 08 2017 21:53
Sure thing, will do, many thanks! :+1:
Kevin K.
@kbknapp
Feb 08 2017 21:53
Correct, it shouldn't ;)
Thanks!
Richard Fussenegger
@Fleshgrinder
Feb 08 2017 22:06
kbknapp/clap-rs#846