CLI11 icon indicating copy to clipboard operation
CLI11 copied to clipboard

.\app.exe -h won't exit automatically ?

Open tans2MA opened this issue 5 years ago • 5 comments

First, many thanks for this wonderful CLI lib. But I encounter a issue when I first taste. I follow the Readme to use CLI11_PARSE(app, argc, argv) after add some flags and options. When I try run in the command line with -h or --help, it will print help message, but not exit the program. My program is designed to read from stdin if not provide filename as argument, so event with -h it will wait stdin. That behavior strange, while most cli tool I used will only print help message and exit if run with `-h'.

Why CLI11_PARSE won't throw and exit when parsed -h flag, and what's the recommended work around?

tans2MA avatar Sep 30 '20 10:09 tans2MA

Did you put CLI11_PARSE in main? It has a return statement in it, so it needs to be in main. If you look at the readme, it shows you exactly what it expands to:

try {
    app.parse(argc, argv);
} catch (const CLI::ParseError &e) {
    return app.exit(e);
}

If you have a more complicated setup, you can use this instead of CLI11_PARSE(app, argc, argv), they are exactly equivalent.

henryiii avatar Sep 30 '20 12:09 henryiii

I didn't put CLI11_PARSE in main, but check the return code, when not 0(parse fail) return from main. But -h return code 0, it is not treated as fail. I have to manually exit when throw from -h, like this:

	try
	{
		app.parse(argc, argv);
	}
	catch (const CLI::ParseError& e)
	{
        if(e.get_name() == "CallForHelp") {
			app.exit(e);
			exit(0);
        }
		return app.exit(e);
	}

tans2MA avatar Sep 30 '20 14:09 tans2MA

The point of CLI11_PARSE is to catch the throw and return. If you want to catch the throw, just call app.parse directly. CLI11_PARSE should only be considered valid from main. It's perfectly okay to handle this yourself (in 1.x, there may be a change to this going to 2.0 that will not affect CLI11_PARSE users, but it will be easy to adapt to).

Asking for help is not a failure, it should return 0. Succeeding and not asking for help continues program execution.

henryiii avatar Sep 30 '20 22:09 henryiii

If I don't want to continue executing my application, after the user has asked for help, which I would think is the normal thing to do, how to do I determine that help was displayed if I use CLI11_PARSE?

joejustesen avatar Dec 18 '20 18:12 joejustesen

CLI11_PARSE terminates the program if help is called by using a return statement when executed from a main function.

You can use exit(0) if you want to do the exit in another function.

CLI11_PARSE uses the app.exit function and checks the exception object

if(e.get_name() == "CallForHelp") {
            out << help();
            return e.get_exit_code();
        }

        if(e.get_name() == "CallForAllHelp") {
            out << help("", AppFormatMode::All);
            return e.get_exit_code();
        }

the return code in the case of help is 0 unless modified.

phlptp avatar Dec 18 '20 20:12 phlptp