epage / clapng

A full featured, fast Command Line Argument Parser for Rust
docs.rs/clap
Apache License 2.0
0 stars 0 forks source link

`TrailingVarArg` doesn't work without `--` #123

Open epage opened 2 years ago

epage commented 2 years ago

Issue by naftulikay Saturday Aug 31, 2019 at 00:05 GMT Originally opened as https://github.com/clap-rs/clap/issues/1538


Rust Version

Stable

Affected Version of clap

Latest

Bug or Feature Request Summary

I am unable to write a program which accepts a variable amount of final args which include flag-like values without using a delimiter.

Expected Behavior Summary

I'd like to be able to have my variable amount of args be of any format, including flag-like options.

Actual Behavior Summary

Parsing fails.

Steps to Reproduce the issue

I have created a playground example which demonstrates the problem directly.

Sample Code or Link to Sample Code

Playground

fn parser<'a, 'b>() -> App<'a, 'b> {
    App::new("varargs")
        .setting(AppSettings::TrailingVarArg)
        // boolean
        .arg(Arg::with_name("prog-flag")
            .long("prog-flag")
            .takes_value(false))
        // path to executable
        .arg(Arg::with_name("EXECUTABLE")
            .takes_value(true)
            .required(true))
        // list of arguments to pass to executable
        .arg(Arg::with_name("ARGS")
            .takes_value(true)
            .multiple(true)
            .allow_hyphen_values(true))
}

#[test]
fn test_with_delimiter() {
    parser().get_matches_from_safe(vec!["--prog-flag", "executable", "--", "-a", "1"]).unwrap();
}

#[test]
fn test_without_delimiter() {
    parser().get_matches_from_safe(vec!["--prog-flag", "executable", "-a", "1"]).unwrap();
}

My exact use-case is that I have a positional argument which is a path to an executable, followed by a list of arguments to pass to that executable.

Example:

./my-program --prog-flag /usr/bin/watch -n 1 echo true

Internally, I'm using --prog-flag to customize my application's behavior, the executable path to find the binary to execute, and the variable length of args to pass to the program as arguments.

I can't seem to find a way to write the above without needing a delimiter (--) between executable and the argument list.

epage commented 2 years ago

Comment by ldm0 Saturday Aug 14, 2021 at 06:21 GMT


Do we really want this?

epage commented 2 years ago

Comment by pksunkara Saturday Aug 14, 2021 at 09:16 GMT


Yeah, this looks like a bug.

epage commented 2 years ago

Comment by kristof-mattei Wednesday Oct 27, 2021 at 22:24 GMT


+ 1 on this one. This is behavior that's quite common seen when using getopts_long. Is there a way to stop clap-rs from parsing the the incoming args as soon as it hit something unknown? That would solve this.