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

set_verbose does not interact with help text #23

Open
chrysn opened this issue Mar 22, 2021 · 2 comments
Open

set_verbose does not interact with help text #23

chrysn opened this issue Mar 22, 2021 · 2 comments
Labels
bug Not as expected

Comments

@chrysn
Copy link

chrysn commented Mar 22, 2021

The new set_default mechanism (and the combination with --quiet) allows setting the default level to warn -- something I highly appreciate.

The help text of --verbose, however, is built earlier than the set_default is evaluated (much earlier, in fact -- at build time). This means that programs that use set_default with anything but the built-in default Error get inconsistent help messages, ie. always

By default, it'll only report errors. Passing -v one time also prints
warnings, -vv enables info logging, -vvv debug, and -vvvv trace.

and not, say

By default, it'll only report errors and warnings. Passing -v one time
enables info logging, -vv debug, and -vvv trace.

I'm not well enough versed in structopt to propose any thought-through way toward resolution. One possibility would to add a generic argument to Verbosity that'd indicate the default and serve as help text; could look like

#[derive(StructOpt, Debug)]
struct CliArgsWithVerbosity {
    ...,
    #[structopt(flatten)]
    verbose: clap_verbosity_flag::Verbosity<DefaultVerbosity=clap_verbosity_flag::DefaultWarning>,
}

and where the DefaultWarning struct provides something like

impl VerbosityDefault for DefaultWarning {
    const fn level() -> Option<Level> {
        Some(log::Level::Warn)
    }
    const fn text() -> &'static str {
        "By default, it'll only report errors and warnings [...]"
    }
}

but as said, I don't know whether using that text would be doable with structopt.

@epage epage added the bug Not as expected label Mar 25, 2021
@epage
Copy link
Member

epage commented Mar 25, 2021

Once we bump our MSRV to 1.51, we could use const-generics but there isn't a way to customize the help string based on that.

At best, we can make the help string more generic.

@chrysn
Copy link
Author

chrysn commented Mar 25, 2021

The current min-const-generics would require that there's a numeric level (and not an enum log::Level let alone Optionlog::Level) for some time -- and if it needs to go though a conversion (Verbosyty<DefaultVerbosity=default_level_from(Some(log::level::Warning))>), a "type-level enum" aka implementation-of-a-custom-trait might be more ergonomic still.

epage added a commit to epage/clap-verbosity-flag that referenced this issue Sep 27, 2024
…-deny-action-2.x

chore(deps): Update EmbarkStudios/cargo-deny-action action to v2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Not as expected
Projects
None yet
Development

No branches or pull requests

2 participants