@@ -26,92 +26,114 @@ void cmdlinet::clear()
2626
2727bool 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
3536bool 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
4345std::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
5360void 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
6170void 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
7083static std::list<std::string> immutable_empty_list;
7184
7285const 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
8095std::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
90110const 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
117139bool 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 }
0 commit comments