wc-duck / getopt Goto Github PK
View Code? Open in Web Editor NEWsimple getopt-implementation
License: Other
simple getopt-implementation
License: Other
If you have an argument that's supposed to have no arguments, the getopt_next function still tries to find one. If that's something that doesn't start with a '-' it's considered an argument and an error is returned ("!"). I would expect that if something is TYPE_NO_ARG, whatever comes after should be ignored.
Example: { "foo", 0, GETOPT_OPTION_TYPE_NO_ARG, nullptr, 'f', "do foo things", ""}
Command line that fails: " --foo banana"
Command line that works: " --foo --banana"
Note that the correct argument is actually found, but disqualified with the comment
/* these types should have no argument, usage error! */
Better to have the doc when you need it and that is in the header itself!
In c++ main signature uses char** not const char**. I got compilation error because char** can not be converted to const char** that is accepted by getopt_create_context.
Currently if I was to define an option as such:
"--my-option"
getopt_create_context returns no error code, however during option parsing the initial two hyphens are stripped away leading to an incorrect string comparison in:
https://github.com/wc-duck/getopt/blob/master/src/getopt.c#L151
Additional validation would be nice here.
If you add this test to the getopt_tests.cpp:
TEST bug_compound_flags()
{
static const getopt_option_t bug_option_list[] =
{
{ "input", 0x0, GETOPT_OPTION_TYPE_REQUIRED, 0x0, 'i', "help input", "input value" },
{ "input-variant", 0x0, GETOPT_OPTION_TYPE_REQUIRED, 0x0, 'I', "help input-variant", "input variant" },
GETOPT_OPTIONS_END
};
const char* argv[] = { "dummy_prog", "--input", "Input Value", "--input-variant", "Input Variant" };
int argc = (int)ARRAY_LENGTH( argv );
getopt_context_t ctx;
int err = getopt_create_context( &ctx, argc, argv, bug_option_list );
ASSERT_EQ( 0, err );
int opt;
int found_input = 0;
int found_input_variant = 0;
static char fail_msg[512];
while( ( opt = getopt_next( &ctx ) ) != -1 )
{
switch( opt )
{
case 'i': { found_input = 1; ASSERT_STR_EQ("Input Value", ctx.current_opt_arg); break; }
case 'I': { found_input_variant = 1; ASSERT_STR_EQ("Input Variant", ctx.current_opt_arg); break; }
default:
{
snprintf(fail_msg, 512, "unexpected op '%c' '%s'", opt, ctx.argv[ctx.current_index]);
FAILm(fail_msg);
}
break;
}
}
ASSERT_EQ(1, found_input);
ASSERT_EQ(1, found_input_variant);
return 0;
}
I had an issue when using this with compound arguments that have overlapping elements. Attempting to write a bug to repro it, I get a new issue, where this will fail to parse, treating "Input Variant" as an argument.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.