getopts icon indicating copy to clipboard operation
getopts copied to clipboard

Add a way of adding "help" flag which works with required options

Open keiichiw opened this issue 3 years ago • 0 comments

I tried to add a --help flag with optflag and a required option. I expected my_command --help worked, but it didn't because Options::parse() failed due to the lack of the required option.

let mut opts = Options::new();
opts.optflag("h", "help", "Description");
opts.reqopt("r", "required", "Description", "TEST");

let result = opts.parse(&["--help"]);
// Failed with "Required option 'required' missing"!
assert!(result.is_ok());

So, how about have a method to add a help flag, which is an optional and makes parse() work without a required options if it's passed?

let mut opts = Options::new();
opts.helpflag("Description");
opts.reqopt("r", "required", "Description", "TEST");
let result = opts.parse(&["--help"]);
assert!(result.is_ok());

keiichiw avatar Dec 15 '21 02:12 keiichiw