Structopt: How to implement group?

Created on 18 Apr 2018  路  13Comments  路  Source: TeXitoi/structopt

clap allows to specify a group where, for example, one (and only one) argument from that group must be present at runtime.

How would I do it in structop?

question

Most helpful comment

As an example is better that a long talk: https://github.com/TeXitoi/structopt/blob/master/examples/group.rs

Does it fix your use case?

All 13 comments

As an example is better that a long talk: https://github.com/TeXitoi/structopt/blob/master/examples/group.rs

Does it fix your use case?

Does it fix your use case?

Yes, great.

Sorry, that I was not able to figure it out from the documentation.

Hm, I still confused.
If change the function vers_arg_group to

fn vers_arg_group() -> ArgGroup<'static> {
    // As the attributes of the struct are executed before the struct
    // fields, we can't use .args(...), but we can use the group
    // attribute on the fields.
    ArgGroup::with_name("blabla")
        .required(true)
}

it is still working.

You then must change the group = "vers" to group = "blabla"

I also thought I had to.

But I didn't change the group = "vers" to "blabla" and your sample worked anyway.

I'm not sure I understand correctly. If your problem is solved, please close the issue, else, feel free to ask a question.

I try to understand the code in your example.

You say: when I change "vers" to "blabla" in function vers_arg_group() I have to change it as well in
group = "vers"

My question: why does the code work when I change "vers" to "blabla" in function vers_arg_group() but do not change it in group = "vers" ???

Does it really work? I suspect you have the only one constraint because the args are in a group, bit you lost the required constraint, ie you can call the command without args. Sorry, on mobile, can't test.

Well, I get this when changing only that one line in vers_arg_group()

% ./target/debug/sotest --major  --set-ver 1
error: The argument '--major' cannot be used with one or more of the other specified arguments

USAGE:
    sotest <> <--set-ver <set_ver>|--major|--minor|--patch>

For more information try --help

or

 % ./target/debug/sotest --minor  --major                 
error: The argument '--minor' cannot be used with one or more of the other specified arguments

USAGE:
    sotest <> <--set-ver <set_ver>|--major|--minor|--patch>

For more information try --help

Perhaps I overlooked a case where it doesn't work.

Your 4 args are still in the same group, named vers, that is created implicitly. Thus, you can use at most one of them. But you can use none, as the required constraint is done on the blabla group, not the vers group.

Aah, I think I got it.

If I call ./target/debug/sotest without parameter then I should get

error: The following required arguments were not provided:
    <--set-ver <set_ver>|--major|--minor|--patch>

USAGE:
    sotest [OPTIONS] <--set-ver <set_ver>|--major|--minor|--patch>

For more information try --help

but in the "blabla" case I get:

./target/debug/sotest                                   :(
error: The following required arguments were not provided:
    <>

USAGE:
    sotest [FLAGS] [OPTIONS] <>

For more information try --help

Thanks a lot for your patience but I needed to understand.

One last point.

If I just want to make sure I use at most one argument of a group I could just leave out the vers_arg_group() function and the statement #[structopt(raw(group = "vers_arg_group()"))]

Yes exactly, you don't need that.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TedDriggs picture TedDriggs  路  6Comments

SirWindfield picture SirWindfield  路  3Comments

bestouff picture bestouff  路  6Comments

coder543 picture coder543  路  9Comments

rask picture rask  路  8Comments