]>
cygwin.com Git - cygwin-apps/setup.git/blob - libgetopt++/src/OptionSet.cc
2 * Copyright (c) 2002 Robert Collins.
3 * Copyright (c) 2003 Robert Collins.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * A copy of the GNU General Public License can be found at
13 * Written by Robert Collins <robertc@hotmail.com>
20 #include "getopt++/OptionSet.h"
21 #include "getopt++/Option.h"
22 #include "getopt++/DefaultFormatter.h"
30 OptionSet::isOption(string::size_type pos
) const
32 return pos
== 1 || pos
== 2;
36 OptionSet::processOne()
38 string
&option (argv
[0]);
39 string::size_type pos
= option
.find_first_not_of("-");
42 /* Push the non option into storage */
43 if (nonOptionHandler
) {
44 lastResult
= nonOptionHandler
->Process(option
.c_str());
46 nonoptions
.push_back(option
);
47 lastResult
= Option::Ok
;
50 doOption(option
, pos
);
55 OptionSet::findOption(string
&option
, string::size_type
const &pos
) const
57 Option
*theOption
= NULL
;
59 for (std::vector
<Option
*>::const_iterator i
= options
.begin(); i
!= options
.end();
62 if (option
[0] == (*i
)->shortOption()[0]) {
66 /* pos == 2 : todo - prefix matches */
68 if (option
.find((*i
)->longOption()) == 0) {
77 OptionSet::doNoArgumentOption(std::string
&option
, std::string::size_type
const &pos
)
79 if (pos
== 1 && option
.size() > 1) {
80 /* Parameter when none allowed */
82 if (option
.find("=") == 1)
83 /* How best to provide failure state ? */
86 argv
.insert(argv
.begin() + 1,"-" + option
.substr(1));
90 if (option
.find("=") != string::npos
)
91 /* How best to provide failure state ? */
97 /* TODO: factor this better */
99 OptionSet::doOption(string
&option
, string::size_type
const &pos
)
101 lastResult
= Option::Failed
;
102 option
.erase(0, pos
);
103 Option
*theOption
= findOption(option
, pos
);
104 char const *optionValue
= NULL
;
107 if (theOption
== NULL
)
110 switch (theOption
->argument()) {
113 if (!doNoArgumentOption (option
, pos
))
117 case Option::Optional
: {
119 if (option
.size() == 1) {
120 /* Value in next argv */
122 if (argv
.size() > 1) {
123 string::size_type maybepos
= argv
[1].find_first_not_of("-");
125 if (!isOption(maybepos
))
128 argv
.erase(argv
.begin() + 1);
131 /* value if present is in this argv */
133 if (option
.find ("=") == 1) {
135 value
= option
.substr(2);
137 /* no option present */
138 argv
.insert(argv
.begin() + 1,"-" + option
.substr(1));
143 string::size_type vpos
= option
.find("=");
145 if (vpos
!= string::npos
) {
146 /* How best to provide failure state ? */
148 if (vpos
== option
.size() - 1)
152 value
= option
.substr(vpos
+ 1);
154 /* Value in next argv */
156 if (argv
.size() > 1) {
157 string::size_type maybepos
= argv
[1].find_first_not_of("-");
159 if (!isOption(maybepos
))
161 argv
.erase(argv
.begin() + 1);
167 optionValue
= value
.c_str();
173 case Option::Required
: {
175 if (option
.size() == 1) {
176 /* Value in next argv */
179 /* but there aren't any */
182 string::size_type maybepos
= argv
[1].find_first_not_of("-");
184 if (isOption(maybepos
))
185 /* The next argv is an option */
189 argv
.erase(argv
.begin() + 1);
191 if (option
.find ("=") != 1 || option
.size() < 3)
192 /* no option passed */
195 value
= option
.substr(2);
198 argv
.insert(argv
.begin() + 1,"-" + option
.substr(1));
202 string::size_type vpos
= option
.find("=");
204 if (vpos
!= string::npos
) {
205 /* How best to provide failure state ? */
207 if (vpos
== option
.size() - 1)
210 value
= option
.substr(vpos
+ 1);
212 /* Value in next argv */
215 /* but there aren't any */
218 string::size_type maybepos
= argv
[1].find_first_not_of("-");
220 if (isOption(maybepos
))
221 /* The next argv is an option */
225 argv
.erase(argv
.begin() + 1);
229 optionValue
= value
.c_str();
233 lastResult
= theOption
->Process(optionValue
);
236 OptionSet::OptionSet () {}
238 OptionSet::~OptionSet ()
244 options
= std::vector
<Option
*> ();
245 argv
= std::vector
<string
> ();
246 nonoptions
= std::vector
<string
> ();
247 remainingargv
= std::vector
<string
> ();
248 nonOptionHandler
= NULL
;
252 OptionSet::process (Option
*aNonOptionHandler
)
254 nonOptionHandler
= aNonOptionHandler
;
255 if (options
.size() == 0 && nonOptionHandler
== NULL
)
258 while (argv
.size()) {
260 switch (lastResult
) {
266 argv
.erase(argv
.begin());
270 if (argv
.size() > 1) {
271 // dies: copy(argv.begin() + 1, argv.end(), remainingargv.begin());
272 for (std::vector
<string
>::iterator i
= argv
.begin() + 1; i
!= argv
.end(); ++i
)
273 remainingargv
.push_back(*i
);
283 OptionSet::Process (int argc
, char **argV
, Option
*nonOptionHandler
)
291 remainingargv
.clear();
293 for (int counter
= 1; counter
< argc
; ++counter
)
294 argv
.push_back(string(argV
[counter
]));
296 return process(nonOptionHandler
);
300 OptionSet::Process (std::vector
<std::string
> const &parms
, Option
*nonOptionHandler
)
302 if (parms
.size() == 0)
306 remainingargv
.clear();
307 return process(nonOptionHandler
);
310 //FIXME: check for conflicts.
312 OptionSet::Register (Option
* anOption
)
314 options
.push_back(anOption
);
318 comp_long_option(const Option
*a
, const Option
*b
)
320 return (a
->longOption().compare(b
->longOption()) < 0);
324 OptionSet::ParameterUsage (ostream
&aStream
)
326 std::sort(options
.begin(), options
.end(), comp_long_option
);
327 for_each (options
.begin(), options
.end(), DefaultFormatter (aStream
));
330 std::vector
<Option
*> const &
331 OptionSet::optionsInSet() const
336 std::vector
<string
> const &
337 OptionSet::nonOptions() const
342 std::vector
<string
> const &
343 OptionSet::remainingArgv() const
345 return remainingargv
;
This page took 0.053908 seconds and 6 git commands to generate.