Skip to content

Commit

Permalink
Merge pull request #427 from perillo/improve-getopt-errors
Browse files Browse the repository at this point in the history
configuration: improve getopt_long error reporting
  • Loading branch information
SimonKagstrom authored Mar 17, 2024
2 parents 0f07787 + 32012bc commit 49635e7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
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

0 comments on commit 49635e7

Please sign in to comment.