Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

configuration: improve getopt_long error reporting #427

Merged
merged 1 commit into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/configuration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ class Configuration : public IConfiguration
/* Hooray for reentrancy... */
optind = 0;
optarg = 0;
opterr = 0;
while (1)
{
int option_index = 0;
Expand All @@ -180,7 +181,7 @@ class Configuration : public IConfiguration
// encountered. This will ensure correct parsing of positional
// arguments without having to use "--" to stop parsing the
// executable arguments.
c = getopt_long(argc, (char **) argv, "+hp:s:l:t:", long_options,
c = getopt_long(argc, (char **) argv, "+:hp:s:l:t:", long_options,
&option_index);

/* No more options */
Expand Down Expand Up @@ -431,10 +432,17 @@ class Configuration : public IConfiguration
}
break;
}
default:
error("Unrecognized option: -%c\n", optopt);
case ':':
error("Option %s requires an argument\n", argv[optind - 1]);
return usage();
case '?':
{
error("Unrecognized option: %s\n", argv[optind - 1]);
return usage();
}
default:
panic("unreachable");
}
}

if (printUsage)
Expand Down
4 changes: 4 additions & 0 deletions tests/tools/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ class TooFewArguments(testbase.KcovTestCase):
def runTest(self):
self.setUp()
rv, output = self.do(testbase.kcov + " " + testbase.outbase + "/kcov")

assert b"Usage: kcov" in output
assert rv == 1

class WrongArguments(testbase.KcovTestCase):
def runTest(self):
self.setUp()
rv, output = self.do(testbase.kcov + " --abc=efg " + testbase.outbase + "/kcov " + testbase.testbuild + "/tests-stripped")

assert b"kcov: error: Unrecognized option: --abc=efg" in output
assert rv == 1

class LookupBinaryInPath(testbase.KcovTestCase):
Expand Down
Loading