bpaf icon indicating copy to clipboard operation
bpaf copied to clipboard

`--version` and `--help` permit invalid arguments

Open ysndr opened this issue 2 years ago • 1 comments

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

ysndr avatar Sep 04 '23 12:09 ysndr

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?

pacak avatar Sep 04 '23 19:09 pacak