Skip to content

Commit 8f7d9f0

Browse files
author
Daniel Kroening
committed
use optional<size_t> instead of -1 in cmdlinet
1 parent 10d0042 commit 8f7d9f0

File tree

4 files changed

+82
-56
lines changed

4 files changed

+82
-56
lines changed

src/goto-cc/goto_cc_cmdline.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ bool goto_cc_cmdlinet::prefix_in_list(
6565

6666
std::size_t goto_cc_cmdlinet::get_optnr(const std::string &opt_string)
6767
{
68-
int optnr;
68+
optionalt<std::size_t> optnr;
6969
cmdlinet::optiont option;
7070

7171
if(has_prefix(opt_string, "--")) // starts with -- ?
@@ -107,13 +107,13 @@ std::size_t goto_cc_cmdlinet::get_optnr(const std::string &opt_string)
107107
}
108108

109109
// new?
110-
if(optnr==-1)
110+
if(!optnr.has_value())
111111
{
112112
options.push_back(option);
113113
return options.size()-1;
114114
}
115115

116-
return optnr;
116+
return *optnr;
117117
}
118118

119119
void goto_cc_cmdlinet::add_infile_arg(const std::string &arg)

src/goto-cc/ms_cl_cmdline.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ void ms_cl_cmdlinet::process_cl_option(const std::string &s)
425425
if(std::string(s, 1, std::string::npos)==ms_cl_flags[j])
426426
{
427427
cmdlinet::optiont option;
428-
int optnr;
428+
optionalt<std::size_t> optnr;
429429

430430
if(s.size()==2)
431431
{
@@ -442,13 +442,13 @@ void ms_cl_cmdlinet::process_cl_option(const std::string &s)
442442
optnr=getoptnr(option.optstring);
443443
}
444444

445-
if(optnr==-1)
445+
if(!optnr.has_value())
446446
{
447447
options.push_back(option);
448448
optnr=options.size()-1;
449449
}
450450

451-
options[optnr].isset=true;
451+
options[*optnr].isset=true;
452452
return;
453453
}
454454
}
@@ -461,7 +461,7 @@ void ms_cl_cmdlinet::process_cl_option(const std::string &s)
461461
{
462462
cmdlinet::optiont option;
463463

464-
int optnr;
464+
optionalt<std::size_t> optnr;
465465

466466
if(ms_cl_prefix.size()==1)
467467
{
@@ -478,14 +478,14 @@ void ms_cl_cmdlinet::process_cl_option(const std::string &s)
478478
optnr=getoptnr(option.optstring);
479479
}
480480

481-
if(optnr==-1)
481+
if(!optnr.has_value())
482482
{
483483
options.push_back(option);
484484
optnr=options.size()-1;
485485
}
486486

487-
options[optnr].isset=true;
488-
options[optnr].values.push_back(
487+
options[*optnr].isset=true;
488+
options[*optnr].values.push_back(
489489
std::string(s, ms_cl_prefix.size()+1, std::string::npos));
490490

491491
return;

src/util/cmdline.cpp

Lines changed: 67 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -26,92 +26,114 @@ void cmdlinet::clear()
2626

2727
bool cmdlinet::isset(char option) const
2828
{
29-
int i=getoptnr(option);
30-
if(i<0)
29+
auto i=getoptnr(option);
30+
if(i.has_value())
31+
return options[*i].isset;
32+
else
3133
return false;
32-
return options[i].isset;
3334
}
3435

3536
bool cmdlinet::isset(const char *option) const
3637
{
37-
int i=getoptnr(option);
38-
if(i<0)
38+
auto i=getoptnr(option);
39+
if(i.has_value())
40+
return options[*i].isset;
41+
else
3942
return false;
40-
return options[i].isset;
4143
}
4244

4345
std::string cmdlinet::get_value(char option) const
4446
{
45-
int i=getoptnr(option);
46-
if(i<0)
47-
return "";
48-
if(options[i].values.empty())
47+
auto i=getoptnr(option);
48+
49+
if(i.has_value())
50+
{
51+
if(options[*i].values.empty())
52+
return "";
53+
else
54+
return options[*i].values.front();
55+
}
56+
else
4957
return "";
50-
return options[i].values.front();
5158
}
5259

5360
void cmdlinet::set(const std::string &option)
5461
{
55-
int i=getoptnr(option);
56-
if(i<0)
57-
return; // ignore
58-
options[i].isset=true;
62+
auto i=getoptnr(option);
63+
64+
if(i.has_value())
65+
options[*i].isset=true;
66+
67+
// otherwise ignore
5968
}
6069

6170
void cmdlinet::set(const std::string &option, const std::string &value)
6271
{
63-
int i=getoptnr(option);
64-
if(i<0)
65-
return; // ignore
66-
options[i].isset=true;
67-
options[i].values.push_back(value);
72+
auto i=getoptnr(option);
73+
74+
if(i.has_value())
75+
{
76+
options[*i].isset=true;
77+
options[*i].values.push_back(value);
78+
}
79+
80+
// otherwise ignore
6881
}
6982

7083
static std::list<std::string> immutable_empty_list;
7184

7285
const std::list<std::string> &cmdlinet::get_values(char option) const
7386
{
74-
int i=getoptnr(option);
75-
if(i<0)
87+
auto i=getoptnr(option);
88+
89+
if(i.has_value())
90+
return options[*i].values;
91+
else
7692
return immutable_empty_list;
77-
return options[i].values;
7893
}
7994

8095
std::string cmdlinet::get_value(const char *option) const
8196
{
82-
int i=getoptnr(option);
83-
if(i<0)
84-
return "";
85-
if(options[i].values.empty())
97+
auto i=getoptnr(option);
98+
99+
if(i.has_value())
100+
{
101+
if(options[*i].values.empty())
102+
return "";
103+
else
104+
return options[*i].values.front();
105+
}
106+
else
86107
return "";
87-
return options[i].values.front();
88108
}
89109

90110
const std::list<std::string> &cmdlinet::get_values(
91111
const std::string &option) const
92112
{
93-
int i=getoptnr(option);
94-
if(i<0)
113+
auto i=getoptnr(option);
114+
115+
if(i.has_value())
116+
return options[*i].values;
117+
else
95118
return immutable_empty_list;
96-
return options[i].values;
97119
}
98120

99-
int cmdlinet::getoptnr(char option) const
121+
optionalt<std::size_t> cmdlinet::getoptnr(char option) const
100122
{
101123
for(std::size_t i=0; i<options.size(); i++)
102124
if(options[i].optchar==option)
103125
return i;
104126

105-
return -1;
127+
return optionalt<std::size_t>();
106128
}
107129

108-
int cmdlinet::getoptnr(const std::string &option) const
130+
optionalt<std::size_t> cmdlinet::getoptnr(const std::string &option) const
109131
{
110132
for(std::size_t i=0; i<options.size(); i++)
111133
if(options[i].optstring==option)
112134
return i;
113135

114-
return -1;
136+
return optionalt<std::size_t>();
115137
}
116138

117139
bool cmdlinet::parse(int argc, const char **argv, const char *optstring)
@@ -165,7 +187,7 @@ bool cmdlinet::parse(int argc, const char **argv, const char *optstring)
165187
args.push_back(argv[i]);
166188
else
167189
{
168-
int optnr;
190+
optionalt<std::size_t> optnr;
169191

170192
if(argv[i][1]!=0 && argv[i][2]==0)
171193
optnr=getoptnr(argv[i][1]); // single-letter option -X
@@ -177,29 +199,31 @@ bool cmdlinet::parse(int argc, const char **argv, const char *optstring)
177199
// We first try single-letter.
178200
optnr=getoptnr(argv[i][1]);
179201

180-
if(optnr<0) // try multi-letter
202+
if(!optnr.has_value()) // try multi-letter
181203
optnr=getoptnr(argv[i]+1);
182204
}
183205

184-
if(optnr<0)
206+
if(!optnr.has_value())
185207
{
186208
unknown_arg=argv[i];
187209
return true;
188210
}
189-
options[optnr].isset=true;
190-
if(options[optnr].hasval)
211+
212+
options[*optnr].isset=true;
213+
214+
if(options[*optnr].hasval)
191215
{
192-
if(argv[i][2]==0 || options[optnr].islong)
216+
if(argv[i][2]==0 || options[*optnr].islong)
193217
{
194218
i++;
195219
if(i==argc)
196220
return true;
197221
if(argv[i][0]=='-' && argv[i][1]!=0)
198222
return true;
199-
options[optnr].values.push_back(argv[i]);
223+
options[*optnr].values.push_back(argv[i]);
200224
}
201225
else
202-
options[optnr].values.push_back(argv[i]+2);
226+
options[*optnr].values.push_back(argv[i]+2);
203227
}
204228
}
205229
}

src/util/cmdline.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Author: Daniel Kroening, [email protected]
1414
#include <list>
1515
#include <string>
1616

17+
#include "optional.h"
18+
1719
class cmdlinet
1820
{
1921
public:
@@ -53,13 +55,13 @@ class cmdlinet
5355
hasval(false),
5456
islong(false),
5557
optchar(0)
56-
{}
58+
{}
5759
};
5860

5961
std::vector<optiont> options;
6062

61-
int getoptnr(char option) const;
62-
int getoptnr(const std::string &option) const;
63+
optionalt<std::size_t> getoptnr(char option) const;
64+
optionalt<std::size_t> getoptnr(const std::string &option) const;
6365
};
6466

6567
#endif // CPROVER_UTIL_CMDLINE_H

0 commit comments

Comments
 (0)