-
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
Parse different arguments to the same field from &ArgMatches #126
Comments
That's a quite complicated feature to design. Maybe using the parse feature, giving a custom parsing command that takes the 2 parameters and returns the value, with a way to list on a struct field that this field is linked to 2 options. Fuzzy try: fn parse_log_level(quiet: u32, verbose: u32) -> log::LevelFilter {
match (quiet, verbose) {
(0, 0) => log::LevelFilter::Warn,
(0, 1) => log::LevelFilter::Error,
(0, _) => log::LevelFilter::Off,
(1, _) => log::LevelFilter::Info,
(2, _) => log::LevelFilter::Debug,
(_, _) => log::LevelFilter::Trace,
}
}
#[derive(StructOpt)]
struct Opt {
#[structopt(
multiarg(short = "v", long = "verbose", parse(from_occurence)),
multiarg(short = "q", long = "quiet", parse(from_occurence)),
parse(from_multiargs = "parse_log_level"),
)]
verbose: log::LevelFilter,
} |
What are your thoughts about delegating #[derive(StructOpt)]
struct Opt {
#[structopt(collapse(LogLevelOpt)]
verbose: log::LevelFilter,
...
}
#[derive(StructOpt)]
struct LogLevelOpt {
#[structopt(short = "v", long = "verbose", parse(from_occurences)]
verbose: u8,
#[structopt(short = "q", long = "quiet", parse(from_occurences)]
quiet: u8,
}
impl Into<log::LevelFilter> for LogLevelOpt {
fn into(self) -> log::LevelFilter {
match (self.quiet, self.verbose) {
(0, 0) => log::LevelFilter::Warn,
(0, 1) => log::LevelFilter::Error,
(0, _) => log::LevelFilter::Off,
(1, _) => log::LevelFilter::Info,
(2, _) => log::LevelFilter::Debug,
(_, _) => log::LevelFilter::Trace,
}
}
} Also, there might be other version that uses custom function instead of #[derive(StructOpt)]
struct Opt {
#[structopt(collapse(LogLevelOpt), parse(from_collapsed = "custom_function")]
verbose: log::LevelFilter,
...
}
#[derive(StructOpt)]
struct LogLevelOpt {
...
}
fn custom_function(opt: LogLevelOpt) -> log::LevelFilter {
...
} |
@I60R That's another path that looks promising. I prefer it to my proposition. |
I have another use case that I think may benefit from having multiple arguments apply to the same field, and it is hard or impossible to implement this using multiple separate arguments. I have a program that's processes some Unicode characters, user should be able to enable or disable processing of some ranges, multiple enable/disable arguments are possible, they should be processes in order they are supplied. For example: --include-range 100-200 --exclude-range 120-130 The result should be the same as: --include-range 100-119 --include-range 131-200 I would like to model this as follows: enum UnicodeRange {...}
enum RangeSpec {
Included {
include_range: UnicodeRange,
},
Excluded {
exclude_range: UnicodeRange,
},
}
struct Opt {
...
#[structopt(???)]
ranges: Vec[RangeSpec],
...
} I could not find any good way to do this. Note that separate arguments would not work because the order is important. I've tried to use |
@TeXitoi indeed it is. Thanks a lot! |
This is an enhancement, and structopt is now feature frozen. |
Example: there is
-v
which increases log verbosity, and-q
which decreases it, and that should be backed by singlelog_level
field inOptions
struct.Alacritty implements that pattern with use of
clap
and manually filled struct; but with use of structopt there will be no other way as splitlog_level
into two fields:q
andv
.I propose something like
parse(from_matches = "parser")
to avoid that.Also, I have a use case where parser like that may greatly simplify my code
The text was updated successfully, but these errors were encountered: