]>
cygwin.com Git - cygwin-apps/setup.git/blob - libgetopt++/src/OptionSet.cc
fff13934926d7b24e6688b4e03bfc98cf05e865e
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
;
106 if (theOption
== NULL
)
109 switch (theOption
->argument()) {
112 if (!doNoArgumentOption (option
, pos
))
116 case Option::Optional
: {
120 if (option
.size() == 1) {
121 /* Value in next argv */
123 if (argv
.size() > 1) {
124 string::size_type maybepos
= argv
[1].find_first_not_of("-");
126 if (!isOption(maybepos
))
129 argv
.erase(argv
.begin() + 1);
132 /* value if present is in this argv */
134 if (option
.find ("=") == 1) {
136 value
= option
.substr(2);
138 /* no option present */
139 argv
.insert(argv
.begin() + 1,"-" + option
.substr(1));
144 string::size_type vpos
= option
.find("=");
146 if (vpos
!= string::npos
) {
147 /* How best to provide failure state ? */
149 if (vpos
== option
.size() - 1)
153 value
= option
.substr(vpos
+ 1);
155 /* Value in next argv */
157 if (argv
.size() > 1) {
158 string::size_type maybepos
= argv
[1].find_first_not_of("-");
160 if (!isOption(maybepos
))
162 argv
.erase(argv
.begin() + 1);
168 optionValue
= value
.c_str();
174 case Option::Required
: {
178 if (option
.size() == 1) {
179 /* Value in next argv */
182 /* but there aren't any */
185 string::size_type maybepos
= argv
[1].find_first_not_of("-");
187 if (isOption(maybepos
))
188 /* The next argv is an option */
192 argv
.erase(argv
.begin() + 1);
194 if (option
.find ("=") != 1 || option
.size() < 3)
195 /* no option passed */
198 value
= option
.substr(2);
201 argv
.insert(argv
.begin() + 1,"-" + option
.substr(1));
205 string::size_type vpos
= option
.find("=");
207 if (vpos
!= string::npos
) {
208 /* How best to provide failure state ? */
210 if (vpos
== option
.size() - 1)
213 value
= option
.substr(vpos
+ 1);
215 /* Value in next argv */
218 /* but there aren't any */
221 string::size_type maybepos
= argv
[1].find_first_not_of("-");
223 if (isOption(maybepos
))
224 /* The next argv is an option */
228 argv
.erase(argv
.begin() + 1);
232 optionValue
= value
.c_str();
236 lastResult
= theOption
->Process(optionValue
);
239 OptionSet::OptionSet () {}
241 OptionSet::~OptionSet ()
247 options
= std::vector
<Option
*> ();
248 argv
= std::vector
<string
> ();
249 nonoptions
= std::vector
<string
> ();
250 remainingargv
= std::vector
<string
> ();
251 nonOptionHandler
= NULL
;
255 OptionSet::process (Option
*aNonOptionHandler
)
257 nonOptionHandler
= aNonOptionHandler
;
258 if (options
.size() == 0 && nonOptionHandler
== NULL
)
261 while (argv
.size()) {
263 switch (lastResult
) {
269 argv
.erase(argv
.begin());
273 if (argv
.size() > 1) {
274 // dies: copy(argv.begin() + 1, argv.end(), remainingargv.begin());
275 for (std::vector
<string
>::iterator i
= argv
.begin() + 1; i
!= argv
.end(); ++i
)
276 remainingargv
.push_back(*i
);
286 OptionSet::Process (int argc
, char **argV
, Option
*nonOptionHandler
)
294 remainingargv
.clear();
296 for (int counter
= 1; counter
< argc
; ++counter
)
297 argv
.push_back(string(argV
[counter
]));
299 return process(nonOptionHandler
);
303 OptionSet::Process (std::vector
<std::string
> const &parms
, Option
*nonOptionHandler
)
305 if (parms
.size() == 0)
309 remainingargv
.clear();
310 return process(nonOptionHandler
);
313 //FIXME: check for conflicts.
315 OptionSet::Register (Option
* anOption
)
317 options
.push_back(anOption
);
321 OptionSet::ParameterUsage (ostream
&aStream
)
323 for_each (options
.begin(), options
.end(), DefaultFormatter (aStream
));
326 std::vector
<Option
*> const &
327 OptionSet::optionsInSet() const
332 std::vector
<string
> const &
333 OptionSet::nonOptions() const
338 std::vector
<string
> const &
339 OptionSet::remainingArgv() const
341 return remainingargv
;
This page took 0.050209 seconds and 5 git commands to generate.