-
For example: #[derive(Debug, Args, Clone, Default, PartialEq, Eq)]
pub struct Config {
#[clap(
short = 'k', long, env = "APP_INSECURE",
)]
pub insecure: Option<bool>
} Here, I want to be able to invoke as: # Set with explicit value.
$ app -k true
$ app -k false
# Set via flag presence:
# Set insecure = Some(true)
$ app -k
# Set insecure = None (flag is absent)
$ app
The reason for having ability to set None is to implement layered configs. (For example, environment + yaml file) I could not find any way to do this. Asking community for help. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Something like this can be done. I'm going to step through the principles behind it. The derive reference states that any builder method can be used as an attribute. If we look at how basic flags are turned into builder methods, we see
Looking at However, keep in mind the note
To be fair, if you will never accept a positional value, a subcommand, or the flag doesn't accept a hyphenated value, this could possibly be skipped. So the end result would look something like #[derive(Debug, Args, Clone, Default, PartialEq, Eq)]
pub struct Config {
#[clap(
short = 'k', long, env = "APP_INSECURE",
num_args = 0..=1, default_missing_value = "true", require_equals = true
)]
pub insecure: Option<bool>
}
```rust |
Beta Was this translation helpful? Give feedback.
If you don't care about
None
but ok withfalse
, you can doIf you really need
None
, then that is the way to do it. The one change I would make is I would donum_args=0