Using Cli default values, when using my crate as library #6128
-
Hello, I am new to Rust and want to use clap for a project that will be a lib and a bin crate. When using my crate as library, it would be nice to be able to use the default values from the CLI, which are set with Here is a small example project with one failing Integration Tests to show the usage as library crate and what default value I would expect: How to use the CLI default values for implementing the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Currently, this is unsupported. You can possibly work around it by having functions for each shared default and doing We were mirroring An unstable Rust feature allows for setting default values on fields directly. We'll need to evaluate how to integrate that into clap as that moves towards stabilization. |
Beta Was this translation helpful? Give feedback.
Currently, this is unsupported. You can possibly work around it by having functions for each shared default and doing
#[arg(default_value_t = foo_default())]
.We were mirroring
serde
in its behavior with itsdefault
field attribute which uses the field'sDefault::default
.serde
also offers adefault
container attribute but that makes all fields defaulted which wouldn't be appropriate in clap. This is being tracked in #3116.An unstable Rust feature allows for setting default values on fields directly. We'll need to evaluate how to integrate that into clap as that moves towards stabilization.