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

--version and --help permit invalid arguments #288

Open
ysndr opened this issue Sep 4, 2023 · 1 comment
Open

--version and --help permit invalid arguments #288

ysndr opened this issue Sep 4, 2023 · 1 comment

Comments

@ysndr
Copy link
Contributor

ysndr commented Sep 4, 2023

Most unix utilities require valid arguments passed to them, even in the presence of --help or --version, e.g. git:

$ git --badarg --version
unknown option: --badarg
usage: git [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           [--super-prefix=<path>] [--config-env=<name>=<envvar>]
           <command> [<args>]

$ git --badarg --help
unknown option: --badarg
usage: git [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           [--super-prefix=<path>] [--config-env=<name>=<envvar>]
           <command> [<args>]

Doing the same with bpaf, does not catch any bad arguments:

use bpaf::*;

#[allow(dead_code)]
#[derive(Debug, Bpaf)]
#[bpaf(options, version)]
pub struct Options {
    /// Message to print in a big friendly letters
    #[bpaf(positional("MESSAGE"))]
    message: String,
}

fn main() {
    println!("{:?}", options().run())
}
cargo run -- --badarg --version
Version: 0.1.0
@pacak
Copy link
Owner

pacak commented Sep 4, 2023

Behavior for --help is partially intentional, behavior for --version is mostly because it uses the same mechanism as --help.

Main motivation for --help is often I find myself wondering "how do I use this again?" for apps that don't have shell completion. With this behavior I simply slap --help at the end and get the info, without it this means getting the command line to a state here it can be parsed, checking help and then restoring the original.

This behavior is not unique either: cargo (clap?) is undecided if it supports invalid arguments along with help or not: cargo --help --nosuch works, cargo --nosuch --help fails to work. ghc (Haskell compiler) works either way, gcc complains about invalid argument but produces help message anyway.

I'll fix the --verbose behavior when I'm around it since there's no reason to accept invalid names along with it. Any particular reason you want the same behavior for help?

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

No branches or pull requests

2 participants