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

[Question] positional argument with at least one value mandatory #13

Open
Geobert opened this issue Feb 19, 2020 · 5 comments
Open

[Question] positional argument with at least one value mandatory #13

Geobert opened this issue Feb 19, 2020 · 5 comments

Comments

@Geobert
Copy link

Geobert commented Feb 19, 2020

Is there a way to specify that a positional arg need at least one value?

#[derive(FromArgs)]
/// MyCMD
struct Opt {
    #[argh(positional)]
    /// script files to run
    test_file: Vec<PathBuf>,
}

gives me:

Usage: mycmd.exe [<test_file...>]

Which suggests the tool can work without any input when it needs at least one.

@benbrittain
Copy link
Contributor

There is not currently, I'd potentially be open to a PR depending on how it's implemented.

I'd suggest checking after parsing if it meets the conditions for now.

@Geobert
Copy link
Author

Geobert commented Feb 19, 2020

Proposal

#[derive(FromArgs)]
/// MyCMD
struct Opt {
    #[argh(positional(1))] // expected number of items
    /// script files to run
    test_file: Vec<PathBuf>,
}

if minimum number of expected item > 0, do not print brackets.

I'm not very familiar with proc_macro yet, so I don't know if this is acceptable :)

EDIT: I'm checking after parsing for now, yes :)

EDIT2: even better? if not Option<_>, it is mandatory, regardless of the field kind

@trevarj
Copy link

trevarj commented Apr 11, 2020

I started hacking this type of syntax into the attribute macro, but it seems that the requested functionality is unclear.

Should inside the parenthesis be the exact expected number of values or the minimum number number of values expected?

I'm able to get the macro syntax to play nice, i.e positional(1) or falling back to just positional, but after that it gets way over my head. Time to learn!

@Geobert
Copy link
Author

Geobert commented Apr 11, 2020

Good question, my first post speaks about "at least" and the 2nd post is "expected number of items". Is it possible to specify something like: =1 or >=1 or >1 for "exact number", minimum number, more than number?

@trevarj
Copy link

trevarj commented Apr 11, 2020

I didn't see that ability in my exploration. I only saw meta data capabilities for key value pairs which would allow positional=1 or a list type that would allow positional(1).

Ben Brittain or another maintainer would know much better than me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants