You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Right now, they are incompatible, but I'm not sure that's right. For example, I wanted something like my-app --db-path [DB], where there's a default database path if not set.
Is there another way to achieve that?
Reacted by Anastasios Andronidis and Felipe S. S. Schneider
Thus, I don't think that's possible to have an argument that can optionally take value, That's why default_value is prohibited in Option, as it will always have a value, and thus will always be Some(a).
I can't find any way to do it using clap. If you can show me a working program doing what you want with clap, I'll try to allow using the feature in structopt.
Another way of achieving optional value of the long argument in clap is to use Arg::min_values(0) as described here: clap-rs/clap#892
Then we can distinguish between say --help and --help topic by checking matches.is_present("help") and pattern matching matches.value_of("help") against Some (as demonstrated in the code from the issue linked above).
I am now trying to describe this behaviour with structopt, but can't find a way :( It sets the field of the datatype to None in both cases when it's omitted and when its value is empty, so I can't tell the difference.
@sphynx That's a quite different topic. You'd want support for Option<Option<T>> or Option<Vec<T>>. If that's what you're looking for, then you can create an issue about this.
As a workaround, you can do the parsing "by hand":
let app = Opt::clap();let matches = app.get_matches();// do something special to retrive the needed informationlet opt = Opt::from_matches(&matches);
Hello, could someone post how the above example can be currently implemented in structopt? I don't really get how default values with Option can be combined. I have a similar use case that I would like to accommodate.
Just pasting the example again here:
$ my-app # db is None
$ my-app --db # db is Some("my-app.db")
$ my-app --db foo.db # db is Some("foo.db")
Activity
TeXitoi commentedon Jul 9, 2018
Can you give example of command line and corresponding struct, because I don't understand what you expect.
lnicola commentedon Jul 9, 2018
Example usage:
TeXitoi commentedon Jul 9, 2018
In clap, an argument takes value or not: https://docs.rs/clap/2.32.0/clap/struct.Arg.html#method.takes_value
Thus, I don't think that's possible to have an argument that can optionally take value, That's why
default_valueis prohibited inOption, as it will always have a value, and thus will always beSome(a).lnicola commentedon Jul 9, 2018
I suppose
default_valueandoccurrences_ofcould be used in this case. The latter will return zero if the argument wasn't passed at all.TeXitoi commentedon Jul 10, 2018
I can't find any way to do it using clap. If you can show me a working program doing what you want with clap, I'll try to allow using the feature in structopt.
lnicola commentedon Jul 10, 2018
TeXitoi commentedon Jul 10, 2018
I'll try to see how I can do it cleanly in structopt. Thanks for the example.
sphynx commentedon May 25, 2019
Another way of achieving optional value of the long argument in
clapis to useArg::min_values(0)as described here: clap-rs/clap#892Then we can distinguish between say
--helpand--help topicby checkingmatches.is_present("help")and pattern matchingmatches.value_of("help")againstSome(as demonstrated in the code from the issue linked above).I am now trying to describe this behaviour with
structopt, but can't find a way :( It sets the field of the datatype toNonein both cases when it's omitted and when its value is empty, so I can't tell the difference.TeXitoi commentedon May 27, 2019
@sphynx That's a quite different topic. You'd want support for
Option<Option<T>>orOption<Vec<T>>. If that's what you're looking for, then you can create an issue about this.As a workaround, you can do the parsing "by hand":
sphynx commentedon May 27, 2019
@TeXitoi Thanks for your reply! As suggested, I've created a new issue: #188
I think I'll either have to use the workaround or restructure the arguments differently for now.
andronat commentedon Nov 22, 2022
Hello, could someone post how the above example can be currently implemented in structopt? I don't really get how default values with Option can be combined. I have a similar use case that I would like to accommodate.
Just pasting the example again here:
epage commentedon Nov 22, 2022
I've not checked but hopefully it works if you use Option<Option> for the type
Though, keep in mind that structopt is deprecated in favor of clap v3+ supporting derives directly.