2

I have a program foo that uses Clap to handle command argument parsing. foo invokes another program, bar. Recently, I decided that users of foo should be able to pass arguments to bar if they like. I added the bar command to Clap:

let matches = App::new("Foo")
    .arg(Arg::with_name("file").value_name("FILE").required(true))
    .arg(
        Arg::with_name("bar")
            .value_name("[BAR_OPTIONS]")
            .short("b")
            .long("bar")
            .multiple(true)
            .help("Invoke bar with these options"),
    )
    .get_matches();

When I try to pass the command "-baz=3" to bar like so:

./foo -b -baz=3 file.txt

or

./foo -b "-baz=3" file.txt

clap returns this error:

error: Found argument '-b' which wasn't expected, or isn't valid in this context

How do I tunnel commands through Clap?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
HiDefender
  • 2,088
  • 2
  • 14
  • 31

1 Answers1

4

If the value of argument bar may itself start with a hyphen, then you need to set the allow_hyphen_values option:

let _matches = App::new("Foo")
    .arg(Arg::with_name("file").value_name("FILE").required(true))
    .arg(
        Arg::with_name("bar")
            .value_name("[BAR_OPTIONS]")
            .allow_hyphen_values(true)
            .short("b")
            .long("bar")
            .multiple(true)
            .help("Invoke bar with these options"),
    )
    .get_matches();
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
harmic
  • 28,606
  • 5
  • 67
  • 91