Skip to content
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

Specify default text for optional arguments in help #1695

Open
emilazy opened this issue Feb 14, 2020 · 3 comments
Open

Specify default text for optional arguments in help #1695

emilazy opened this issue Feb 14, 2020 · 3 comments
Labels
A-help Area: documentation, including docs.rs, readme, examples, etc... C-enhancement Category: Raise on the bar on expectations S-waiting-on-decision Status: Waiting on a go/no-go before implementing

Comments

@emilazy
Copy link
Contributor

emilazy commented Feb 14, 2020

Describe your use case

Often, the default values of arguments are determined based on runtime computation, e.g. a --threads parameter defaulting to the number of available threads on the system, or a --config option defaulting to an OS-specific path in the user's home directory (e.g. $XDG_CONFIG_HOME/app-name/config.ron on Linux). It's beneficial for usability to be able to summarize the default arguments, e.g. Number of threads to use [default: # of threads on system] or Path to configuration [default: $XDG_CONFIG_HOME/app-name/config.ron].

At present, there's no API to give a (purely cosmetic) default option text for an optional argument.

Currently, I just do:

#[derive(Clap)]
struct Opts {
    /// Option description [default: $XDG_DATA_HOME/...]
    pub path: Option<PathBuf>,
}

but this isn't quite correct (e.g. it doesn't format the default with Colorizer::good).

Arguably this is an X-Y problem and the real feature request is for "computed defaults" (related to #1694), but I think even with such a mechanism there's value in being able to summarize the default path (rather than e.g. inlining volatile variables like $XDG_RUNTIME_DIR in the help text).

Describe the solution you'd like

#[derive(Clap)]
struct Opts {
    /// Option description
    #[clap(default_help = "$XDG_DATA_HOME/...")]
    pub path: Option<PathBuf>,
}
@pksunkara pksunkara added C: args A-help Area: documentation, including docs.rs, readme, examples, etc... labels Feb 14, 2020
@pksunkara pksunkara added this to the 3.1 milestone Feb 14, 2020
@pksunkara
Copy link
Member

I actually removed the colorizing for default values in #1824. I wanted to rethink quite a bit on how to display all kinds of things like these. But, it's for v3.1

@epage epage removed this from the 3.1 milestone Dec 8, 2021
@epage epage added C-enhancement Category: Raise on the bar on expectations S-waiting-on-mentor Status: Needs elaboration on the details before doing a 'Call for participation' and removed C: args labels Dec 8, 2021
@epage epage added S-waiting-on-decision Status: Waiting on a go/no-go before implementing and removed S-waiting-on-mentor Status: Needs elaboration on the details before doing a 'Call for participation' labels Jan 20, 2022
@epage
Copy link
Member

epage commented Jan 20, 2022

One proposal for #2389 includes exposing some level of styling to the user.

#3008 explores pulling defaulting logic out of the core of clap into helpers that you can pass into App and Arg. #3321 then brings up the idea of those traits offering a hint(&self, app: &App) -> Option<&str> function. This would allow creating a no-op default trait impl that just adds information to the help.

Since both of these are not solid enough as alternatives yet, still keeping this open to ensure the use case gets covered by something

@9999years
Copy link
Contributor

9999years commented Oct 25, 2023

It sounds like there's interest for this, so I might prepare a PR.

Related issues:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-help Area: documentation, including docs.rs, readme, examples, etc... C-enhancement Category: Raise on the bar on expectations S-waiting-on-decision Status: Waiting on a go/no-go before implementing
Projects
None yet
Development

No branches or pull requests

4 participants