These are chat archives for kbknapp/clap-rs

4th
Jun 2018
Stefan Puhlmann
@hstefan
Jun 04 2018 16:39
Hey everyone! I'm trying to rewrite some tools using clap but I haven't been able to positional args to work the way I'd like. What I have so far makes it mandatory to have "--" before the last argument, but I was wondering if it is possible to make it optional...
let matches = App::new("xargs (clone)")
        .version("0.1")
        .author("Stefan Puhlmann")
        .about("A toy xargs clone written in rust")
        .arg(
            Arg::with_name("command")
                .multiple(true)
                .allow_hyphen_values(true)
                .default_value("echo"),
        )
        .get_matches();
this is how I'd like to be able to run it:
ls | ./target/debug/xargs du -s
error: Found argument '-s' which wasn't expected, or isn't valid in this context
also, damn that's a nice arg parsing library :-)
Denis Lisov
@tanriol
Jun 04 2018 17:58
Kevin K.
@kbknapp
Jun 04 2018 20:18
@tanriol is correct, that should work for you @hstefan but let us know if it doesn't ;)
Stefan Puhlmann
@hstefan
Jun 04 2018 21:23
yup! looks like it does @tanriol @kbknapp . But then I can only have one positional .arg, right?
Kevin K.
@kbknapp
Jun 04 2018 21:24
no, you can have as many as you want...however only one that accepts multiple values
Stefan Puhlmann
@hstefan
Jun 04 2018 21:25
shouldn't this also work, then?
    let matches = App::new("xargs (clone)")
        .setting(AppSettings::TrailingVarArg)
        .arg(
            Arg::with_name("command")
                .required(true)
                .default_value("echo"))
        .arg(
            Arg::from_usage("<initial-args>... initial arguments for command")
            .allow_hyphen_values(true)
            .default_value("")
        )
        .get_matches();
it works ok, unless I have arguments with hyphen
fine: ./target/debug/xargs du a b c, errors: ./target/debug/xargs du -s # error: Found argument '-s' which wasn't expected, or isn't valid in this context
Kevin K.
@kbknapp
Jun 04 2018 21:27
It should work, however I'd remove the default value from the initial-args argument
let me try it real quick
this is the full code, if you'd like to experiment with it
Kevin K.
@kbknapp
Jun 04 2018 21:34
I'm doing some testing on it now :wink:
it's a bug
Would you mind filing an issue? I think it's actually a pretty easy one to fix, however I may not have time to knock it out tonight
Stefan Puhlmann
@hstefan
Jun 04 2018 21:41
@kbknapp sure thing, thanks for being that responsive! I could also try to have a look and fix it, but I'd need some pointers
Kevin K.
@kbknapp
Jun 04 2018 21:41
The bandaid fix is to use AppSettings::AllowLeadingHyphen
Stefan Puhlmann
@hstefan
Jun 04 2018 21:43
awesome, thanks
I'll fill an issue and then
Kevin K.
@kbknapp
Jun 04 2018 21:45
awesome thanks! I'll throw some pointers in where the fix can be made if you'd like the commit :wink:
Stefan Puhlmann
@hstefan
Jun 04 2018 21:45
yes, that would be great
Kevin K.
@kbknapp
Jun 04 2018 21:46
Will do! I've got to head out for a bit, but I'll check the issue tonight and put some thoughts down on where I think the bug is and how to fix it
Stefan Puhlmann
@hstefan
Jun 04 2018 21:46
mhmm, I'm off to be real soon anyway
Kevin K.
@kbknapp
Jun 04 2018 21:47
no worries, take your time!
Stefan Puhlmann
@hstefan
Jun 04 2018 22:12
@kbknapp kbknapp/clap-rs#1287