Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to parse mv <files>... <target>? #725

Closed
hh9527 opened this issue Nov 1, 2016 · 9 comments
Closed

how to parse mv <files>... <target>? #725

hh9527 opened this issue Nov 1, 2016 · 9 comments
Labels
A-parsing Area: Parser's logic and needs it changed somehow. C-enhancement Category: Raise on the bar on expectations

Comments

@hh9527
Copy link

hh9527 commented Nov 1, 2016

E.g.: mv a1 a2 a3/

I use the code like this:

let m = App::new()
  .arg(Arg::with_name("files").required(true).multiple(true))
  .arg(Arg::with_name("target").required(true))
  .get_matches();

When input mv a1 a2 a3/, I expect result like this:

{
  files: ["a1", "a2"],
  target: "a3",
}

But I got a error:

error: The following required arguments were not provided:
    <target>
@kbknapp
Copy link
Member

kbknapp commented Nov 1, 2016

This is because there's no way to know when <files> ends and <target> starts. Positional arguments that take multiple values must be the last positional argument.

There are two ways to fix this, either swap the arguments (i.e. <target> <files>...) or limit the number of values to a fixed number (i.e. <file> <file> <target>). Obviously one of those solutions is less viable than the other (fixed number of files), but it's still an option.

Hope this helps! I'm also open to suggestions 😉

@kbknapp kbknapp changed the title how to parse mv <files> <target>? how to parse mv <files>... <target>? Nov 1, 2016
@kbknapp
Copy link
Member

kbknapp commented Nov 1, 2016

Also, thinking about this a little more, there may be a way I could add this feature. Let me play with some implementations and get back with you!

@kbknapp kbknapp added C-enhancement Category: Raise on the bar on expectations P4: nice to have A-parsing Area: Parser's logic and needs it changed somehow. labels Nov 1, 2016
kbknapp added a commit that referenced this issue Nov 1, 2016
…l argument as multiple(true)

Now one can build CLIs that support things like `mv <files>... <target>`

There are a few requirements and caveats;

 * The final positional argument (and all positional arguments prior) *must* be required
 * Only one positional argument may be `multiple(true)`
 * Only the second to last, or last positional argument may be `multiple(true)`

Closes #725
@kbknapp
Copy link
Member

kbknapp commented Nov 1, 2016

I found a way to make this work, after #727 merges I'll upload v2.17.0 to crates.io

At which point this will give the expected results:

let m = App::new()
  .arg(Arg::with_name("files").required(true).multiple(true))
  .arg(Arg::with_name("target").required(true))
  .get_matches();

@homu homu closed this as completed in 1ced2a7 Nov 1, 2016
@kbknapp
Copy link
Member

kbknapp commented Nov 2, 2016

@hh9527 v2.17.0 is up on crates.io now

@hh9527
Copy link
Author

hh9527 commented Nov 2, 2016

waooo, it is so nice of you! thank you for this great project!

@hh9527
Copy link
Author

hh9527 commented Nov 2, 2016

@kbknapp I have tried v2.17.0, it works without subcommand, but still break in subcommand.

This works:

    let m = App::new("mv")
        .arg(Arg::with_name("paths").required(true).multiple(true))
        .arg(Arg::with_name("target").required(true))
        .get_matches();

And this not:

    let m = App::new("cli")
        .subcommand(
            SubCommand::with_name("mv")
                .arg(Arg::with_name("paths").required(true).multiple(true))
                .arg(Arg::with_name("target").required(true))
        ).get_matches();

@kbknapp
Copy link
Member

kbknapp commented Nov 2, 2016

Ah, thanks for pointing this out, I hadn't testsed it after a subcommand, only before!

@kbknapp kbknapp reopened this Nov 2, 2016
@kbknapp
Copy link
Member

kbknapp commented Nov 2, 2016

It's fixed in #730 😉

@homu homu closed this as completed in 33924e8 Nov 2, 2016
@kbknapp
Copy link
Member

kbknapp commented Nov 2, 2016

v2.17.1 is up now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-parsing Area: Parser's logic and needs it changed somehow. C-enhancement Category: Raise on the bar on expectations
Projects
None yet
Development

No branches or pull requests

2 participants