Skip to content

Commit 6b6f6cb

Browse files
author
Joel Allred
committed
Add message for unknown cbmc option
During command line parsing, if an unknown option is read, store it and output an error message at parse_options_baset::main Add regression tests to check the feature.
1 parent 9ac5dff commit 6b6f6cb

File tree

7 files changed

+31
-0
lines changed

7 files changed

+31
-0
lines changed

regression/cbmc/bad_option/main.c

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
int main() {
2+
return 0;
3+
}

regression/cbmc/bad_option/test.desc

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
main.c
3+
-foo
4+
^EXIT=(64|1)$
5+
^SIGNAL=0$
6+
Unknown option: -foo
7+
--
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
main.c
3+
--trace --foo --refine-strings
4+
^EXIT=(64|1)$
5+
^SIGNAL=0$
6+
Unknown option: --foo
7+
--

src/util/cmdline.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,10 @@ bool cmdlinet::parse(int argc, const char **argv, const char *optstring)
185185
}
186186

187187
if(optnr<0)
188+
{
189+
unknown_arg=argv[i];
188190
return true;
191+
}
189192
options[optnr].isset=true;
190193
if(options[optnr].hasval)
191194
{

src/util/cmdline.h

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class cmdlinet
3333

3434
typedef std::vector<std::string> argst;
3535
argst args;
36+
std::string unknown_arg;
3637

3738
cmdlinet();
3839
virtual ~cmdlinet();

src/util/parse_options.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,20 @@ void parse_options_baset::usage_error()
3737
help();
3838
}
3939

40+
/// Print an error message mentioning the option that was not recognized when
41+
/// parsing the command line.
42+
void parse_options_baset::unknown_option_msg()
43+
{
44+
if(!cmdline.unknown_arg.empty())
45+
std::cerr << "Unknown option: " << cmdline.unknown_arg << "\n";
46+
}
47+
4048
int parse_options_baset::main()
4149
{
4250
if(parse_result)
4351
{
4452
usage_error();
53+
unknown_option_msg();
4554
return EX_USAGE;
4655
}
4756

src/util/parse_options.h

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class parse_options_baset
3131
virtual ~parse_options_baset() { }
3232

3333
private:
34+
void unknown_option_msg();
3435
bool parse_result;
3536
};
3637

0 commit comments

Comments
 (0)