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

fix(man): Don't show default values for flags, show ellipsis for repeatable arguments #4432

Merged
merged 1 commit into from Oct 31, 2022

Conversation

drivasperez
Copy link
Contributor

Fixes #4410.

The help generator for the main clap package uses the Arg::is_takes_value_set() method to determine whether it should show a default argument. (See Arg::stylize_arg_suffix, on line 4148 of arg.rs.)

I ported the same logic over to clap_mangen, so boolean flag arguments no longer show [default=false] (or [default=0] for repeatable options), which is not valid.

I also noticed that the generated synopsis does not add a ... ellipsis for repeatable flags, so I added that as well.

Finally, I updated the snapshot tests to reflect the above changes.

Example

For the following program (this is essentially clap_mangen/examples/man.rs):

fn main() -> Result<(), std::io::Error> {
    let cmd = Command::new("myapp")
        .version("1.0")
        .author("Kevin K. <kbknapp@gmail.com>:Ola Nordmann <old@nordmann.no>")
        .about("Does awesome things")
        .long_about(
            "With a longer description to help clarify some things.

And a few newlines.",
        )
        .after_help("This is an extra section added to the end of the manpage.")
        .after_long_help("With even more text added.")
        .arg(
            arg!(-c --config <FILE> "Sets a custom config file")
                .long_help("Some more text about how to set a custom config file")
                .default_value("config.toml")
                .env("CONFIG_FILE"),
        )
        .arg(arg!([output] "Sets an output file").default_value("result.txt"))
        .arg(
            arg!(-d --debug ... "Turn debugging information on")
                .env("DEBUG_ON")
                .hide_env(true),
        )
        .subcommand(
            Command::new("test")
                .about("does testing things")
                .arg(arg!(-l --list "Lists test values")),
        );

    Man::new(cmd).render(&mut io::stdout())
}

you previously got

myapp(1)                                                                             General Commands Manual                                                                            myapp(1)

NAME
       myapp - Does awesome things

SYNOPSIS
       myapp [-c|--config] [--thing] [-d|--debug] [-h|--help] [-V|--version] [output] [subcommands]

DESCRIPTION
       With a longer description to help clarify some things.

       And a few newlines.

OPTIONS
       -c, --config=FILE [default: config.toml]
              Some more text about how to set a custom config file
              May also be specified with the CONFIG_FILE environment variable.

       --thing [default: false]
              Does a thing with a flag

       -d, --debug [default: 0]
              Turn debugging information on

       -h, --help
              Print help information (use `-h` for a summary)

       -V, --version
              Print version information

       [output] [default: result.txt]
              Sets an output file

SUBCOMMANDS
       myapp-test(1)
              does testing things

       myapp-help(1)
              Print this message or the help of the given subcommand(s)

EXTRA
       With even more text added.

VERSION
       v1.0

AUTHORS
       Kevin K. <kbknapp@gmail.com>:Ola Nordmann <old@nordmann.no>

                                                                                            myapp 1.0                                                                                   myapp(1)

and now get

myapp(1)                                                                             General Commands Manual                                                                            myapp(1)

NAME
       myapp - Does awesome things

SYNOPSIS
       myapp [-c|--config] [--thing] [-d|--debug]... [-h|--help] [-V|--version] [output] [subcommands]

DESCRIPTION
       With a longer description to help clarify some things.

       And a few newlines.

OPTIONS
       -c, --config=FILE [default: config.toml]
              Some more text about how to set a custom config file
              May also be specified with the CONFIG_FILE environment variable.

       --thing
              Does a thing with a flag

       -d, --debug
              Turn debugging information on

       -h, --help
              Print help information (use `-h` for a summary)

       -V, --version
              Print version information

       [output] [default: result.txt]
              Sets an output file

SUBCOMMANDS
       myapp-test(1)
              does testing things

       myapp-help(1)
              Print this message or the help of the given subcommand(s)

EXTRA
       With even more text added.

VERSION
       v1.0

AUTHORS
       Kevin K. <kbknapp@gmail.com>:Ola Nordmann <old@nordmann.no>

                                                                                            myapp 1.0                                                                                   myapp(1)

@drivasperez drivasperez changed the title clap_mangen: Don't show default values for flags clap_mangen: Don't show default values for flags, show ellipsis for repeatable arguments Oct 30, 2022
Also show ellipsis for repeatable arguments.
@drivasperez
Copy link
Contributor Author

drivasperez commented Oct 31, 2022

It strikes me that clap_mangen also generates an incorrect synopsis for arguments with ArgAction::Append, which require a value and can also appear multiple times.

For the following program:

fn main() {
    let cmd = Command::new("testing clap").arg(
        clap::Arg::new("config")
            .action(clap::ArgAction::Append)
            .help("some config file")
            .short('c')
            .visible_short_alias('C')
            .long("config")
            .visible_alias("conf"),
    );

    clap_mangen::Man::new(cmd.clone())
        .render(&mut std::io::stdout())
        .unwrap();
}

You get the following output:

testing clap(1)                       General Commands Manual                       testing clap(1)

NAME
       testing clap

SYNOPSIS
       testing clap [-c|--config] [-h|--help]

DESCRIPTION
OPTIONS
       -c, --config
              some config file

       -h, --help
              Print help information

                                           testing clap                             testing clap(1)

I think it should be something more like the following:

testing clap [-c|--config=CONFIG]... [-h|--help]

But I'm not sure.

@epage epage changed the title clap_mangen: Don't show default values for flags, show ellipsis for repeatable arguments fix(man): Don't show default values for flags, show ellipsis for repeatable arguments Oct 31, 2022
@epage epage merged commit 5908192 into clap-rs:master Oct 31, 2022
Calder-Ty added a commit to Calder-Ty/clap that referenced this pull request Nov 30, 2022
It was noticed that between clap-rs#4443 and clap-rs#4432, an issue in the behavior
was that the derive api handles tasks with values slightly differently
than the declarative api. Added a test to show parity between
declaritive and derive api.
Calder-Ty added a commit to Calder-Ty/clap that referenced this pull request Nov 30, 2022
It was noticed that between clap-rs#4443 and clap-rs#4432, an issue in the behavior
was that the derive api handles tasks with values slightly differently
than the declarative api. Added a test to show parity between
declaritive and derive api.
Calder-Ty added a commit to Calder-Ty/clap that referenced this pull request Nov 30, 2022
It was noticed that between clap-rs#4443 and clap-rs#4432, an issue in the behavior
was that the derive api handles tasks with values slightly differently
than the declarative api. Added a test to show parity between
declaritive and derive api.
Calder-Ty added a commit to Calder-Ty/clap that referenced this pull request Dec 16, 2022
It was noticed that between clap-rs#4443 and clap-rs#4432, an issue in the behavior
was that the derive api handles tasks with values slightly differently
than the declarative api. Added a test to show parity between
declaritive and derive api.
Calder-Ty added a commit to Calder-Ty/clap that referenced this pull request Dec 16, 2022
It was noticed that between clap-rs#4443 and clap-rs#4432, an issue in the behavior
was that the derive api handles tasks with values slightly differently
than the declarative api. Added a test to show parity between
declaritive and derive api.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generated man pages claim that flag options take values
2 participants