How to make StructOpt error on unknown or misspelled flags?

I am using StructOpt to parse my CLI arguments for my CLI application. I want the application to crash when user provides an unknown or misspelled flags, for example: --details is valid, but --detail is not. How do I do it?

It appears that StructOpt::from_args() already prints out an error message and exits the program in the event of an error. This error message explains the usage of the program based on the argument struct. If you want some custom error handling you could read the command line arguments, call StructOpt::from_iter_safe(), and then handle the clap::Error yourself. Note that invocation of the help and version flags at command line is treated as an error though (since they terminate the program prematurely), so you probably want to handle those differently.

2 Likes

Thanks for your answer. It makes me wonder why from_args doesn't just do it for me.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.