-
Notifications
You must be signed in to change notification settings - Fork 152
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
default_value and Option? #123
Comments
Can you give example of command line and corresponding struct, because I don't understand what you expect. |
#[derive(Clone, Debug, StructOpt)]
pub struct Options {
#[structopt(long = "db", help = "Database path", default_value = "my-app.db")]
pub db: Option<String>,
} Example usage:
|
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 |
I suppose |
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. |
extern crate clap;
use clap::{App, Arg, ArgMatches};
fn get_optional_value<'a, 'b>(m: &'a ArgMatches, name: &'b str) -> Option<&'a str> {
if m.occurrences_of(name) == 0 {
None
} else {
m.value_of(name)
}
}
fn main() {
let app = App::new("my-app").arg(Arg::with_name("db").long("db").default_value("my-app.db"));
let m = app.clone().get_matches_from(vec!["my-app", "--db"]);
assert!(get_optional_value(&m, "db") == Some("my-app.db"));
let m = app
.clone()
.get_matches_from(vec!["my-app", "--db", "foo.db"]);
assert!(get_optional_value(&m, "db") == Some("foo.db"));
let m = app.clone().get_matches_from(vec!["my-app"]);
assert!(get_optional_value(&m, "db") == None);
} |
I'll try to see how I can do it cleanly in structopt. Thanks for the example. |
Another way of achieving optional value of the long argument in Then we can distinguish between say I am now trying to describe this behaviour with |
@sphynx That's a quite different topic. You'd want support for 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 information
let 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:
|
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. |
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?
The text was updated successfully, but these errors were encountered: