ProgramOptions.hxx icon indicating copy to clipboard operation
ProgramOptions.hxx copied to clipboard

mandatory option

Open peterritter opened this issue 8 years ago • 4 comments

I wonder if it would make sense to be able to declare an option as 'mandatory' via a .mandatory() function. I can of course check availability of an option explicitly, but when there are many options, it gets tedious. A missing mandatory options should just throw an exceptions. That way the program can terminate immediately rather than wasting time with other things until it discovers that a critical option is missing. P.

peterritter avatar Jun 04 '17 14:06 peterritter

This feature is available on the develop branch. Commit: https://github.com/Fytch/ProgramOptions.hxx/commit/e9a7a251ecdc7f65de6e09faaf6ba11a401b3421 Exemplary usage:

auto&& foo = parser[ "foo" ]
    .mandatory();

However: There's a problem in that implementation right now: it reports an error when calling the program with program --help, i.e. in cases where even mandatory options are not required. I thought about this problem and came up with a solution but it seems kind of hacky to me. The solution is to introduce another property flag to make options such as help "terminal". Meaning, if the parser comes across them, he should stop immediately, parsing no more options and printing no errors. What do you think? Is this solution acceptable or is there a better one?

Regards, Josua

Fytch avatar Jun 04 '17 23:06 Fytch

To illustrate the aforementioned issue, consider this example:

#include <ProgramOptions.hxx>
#include <iostream>

int main( int argc, char** argv ) {
	po::parser parser;

	auto&& help = parser[ "help" ]
		.abbreviation( '?' )
		.description( "print this help screen" );

	auto&& files = parser[ "" ]
		.mandatory();

	if( !parser( argc, argv ) )
		return -1;

	if( help.was_set() ) {
		std::cout << parser;
		return 0;
	}

	for( auto iter = files.begin< po::string >(); iter != files.end< po::string >(); ++iter )
		std::cout << *iter << '\n';
}

Cheers

Fytch avatar Jun 04 '17 23:06 Fytch

Hi Josua

I'm really under water with my own development. I'm afraid I can't be of much help at the moment. Sorry I overwhelmed you with suggestions. I just feel strongly about certain things. I am using the cxxopts lib for the moment. Email me directly at [email protected] if you ever want to get in touch. Thanks for the effort! You can mark my cases as closed.

Best Regards, Peter Ritter

On Mon, Jun 5, 2017 at 12:06 AM, Fytch [email protected] wrote:

This feature is available on the develop branch. Commit: e9a7a25 https://github.com/Fytch/ProgramOptions.hxx/commit/e9a7a251ecdc7f65de6e09faaf6ba11a401b3421 Exemplary usage:

auto&& foo = parser[ "foo" ] .mandatory();

However: There's a problem in that implementation right now: it reports an error when calling the program with program --help, i.e. in cases where even mandatory options are not required. I thought about this problem and came up with a solution but it seems kind of hacky to me. The solution is to introduce another property flag to make options such as help "terminal". Meaning, if the parser comes across them, he should stop immediately, parsing no more options and printing no errors. What do you think? Is this solution acceptable or is there a better one?

Regards, Josua

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Fytch/ProgramOptions.hxx/issues/7#issuecomment-306073436, or mute the thread https://github.com/notifications/unsubscribe-auth/AHfWEZlsw3rvtRHzKIl0aFkLmzKa7uU8ks5sAzhagaJpZM4NvYob .

peterritter avatar Jun 08 '17 11:06 peterritter

"required" is the common term used for this instead of "mandatory". I've seen it in POCO, CLI11, etc.

oblitum avatar Jun 10 '17 01:06 oblitum