]>
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"
28 OptionSet::isOption(std::string::size_type pos
) const
30 return pos
== 1 || pos
== 2;
34 OptionSet::processOne()
36 std::string
&option (argv
[0]);
37 std::string::size_type pos
= option
.find_first_not_of("-");
40 /* Push the non option into storage */
41 if (nonOptionHandler
) {
42 lastResult
= nonOptionHandler
->Process(option
.c_str(), 0);
44 nonoptions
.push_back(option
);
45 lastResult
= Option::Ok
;
48 doOption(option
, pos
);
53 OptionSet::findOption(std::string
&option
, std::string::size_type
const &pos
,
54 Option
*&theOption
, int & prefixIndex
) const
59 for (std::vector
<Option
*>::const_iterator i
= options
.begin(); i
!= options
.end();
62 if (option
[0] == (*i
)->shortOption()[0]) {
68 std::vector
<std::string
> prefixes
= (*i
)->longOptionPrefixes();
69 for (std::vector
<std::string
>::const_iterator j
= prefixes
.begin();
73 std::string prefixedOption
= *j
+ (*i
)->longOption();
74 if (option
.find(prefixedOption
) == 0) {
76 prefixIndex
= j
- prefixes
.begin();
85 OptionSet::doNoArgumentOption(std::string
&option
, std::string::size_type
const &pos
)
87 if (pos
== 1 && option
.size() > 1) {
88 /* Parameter when none allowed */
90 if (option
.find("=") == 1)
91 /* How best to provide failure state ? */
94 argv
.insert(argv
.begin() + 1,"-" + option
.substr(1));
98 if (option
.find("=") != std::string::npos
)
99 /* How best to provide failure state ? */
105 /* TODO: factor this better */
107 OptionSet::doOption(std::string
&option
, std::string::size_type
const &pos
)
109 lastResult
= Option::Failed
;
110 option
.erase(0, pos
);
111 Option
*theOption
= NULL
;
113 findOption(option
, pos
, theOption
, prefixIndex
);
114 char const *optionValue
= NULL
;
117 if (theOption
== NULL
)
120 switch (theOption
->argument()) {
123 if (!doNoArgumentOption (option
, pos
))
127 case Option::Optional
: {
129 if (option
.size() == 1) {
130 /* Value in next argv */
132 if (argv
.size() > 1) {
133 std::string::size_type maybepos
= argv
[1].find_first_not_of("-");
135 if (!isOption(maybepos
)) {
138 argv
.erase(argv
.begin() + 1);
142 /* value if present is in this argv */
144 if (option
.find ("=") == 1) {
146 value
= option
.substr(2);
148 /* no option present */
149 argv
.insert(argv
.begin() + 1,"-" + option
.substr(1));
154 std::string::size_type vpos
= option
.find("=");
156 if (vpos
!= std::string::npos
) {
157 /* How best to provide failure state ? */
159 if (vpos
== option
.size() - 1)
163 value
= option
.substr(vpos
+ 1);
165 /* Value in next argv */
167 if (argv
.size() > 1) {
168 std::string::size_type maybepos
= argv
[1].find_first_not_of("-");
170 if (!isOption(maybepos
)) {
172 argv
.erase(argv
.begin() + 1);
179 optionValue
= value
.c_str();
185 case Option::Required
: {
187 if (option
.size() == 1) {
188 /* Value in next argv */
191 /* but there aren't any */
194 std::string::size_type maybepos
= argv
[1].find_first_not_of("-");
196 if (isOption(maybepos
))
197 /* The next argv is an option */
201 argv
.erase(argv
.begin() + 1);
203 if (option
.find ("=") != 1 || option
.size() < 3)
204 /* no option passed */
207 value
= option
.substr(2);
210 argv
.insert(argv
.begin() + 1,"-" + option
.substr(1));
214 std::string::size_type vpos
= option
.find("=");
216 if (vpos
!= std::string::npos
) {
217 /* How best to provide failure state ? */
219 if (vpos
== option
.size() - 1)
222 value
= option
.substr(vpos
+ 1);
224 /* Value in next argv */
227 /* but there aren't any */
230 std::string::size_type maybepos
= argv
[1].find_first_not_of("-");
232 if (isOption(maybepos
))
233 /* The next argv is an option */
237 argv
.erase(argv
.begin() + 1);
241 optionValue
= value
.c_str();
245 theOption
->setPresent(true);
246 lastResult
= theOption
->Process(optionValue
, prefixIndex
);
249 OptionSet::OptionSet () {}
251 OptionSet::~OptionSet ()
257 options
= std::vector
<Option
*> ();
258 argv
= std::vector
<std::string
> ();
259 nonoptions
= std::vector
<std::string
> ();
260 remainingargv
= std::vector
<std::string
> ();
261 nonOptionHandler
= NULL
;
265 OptionSet::process (Option
*aNonOptionHandler
)
267 nonOptionHandler
= aNonOptionHandler
;
268 if (options
.size() == 0 && nonOptionHandler
== NULL
)
271 while (argv
.size()) {
273 switch (lastResult
) {
279 argv
.erase(argv
.begin());
283 if (argv
.size() > 1) {
284 // dies: copy(argv.begin() + 1, argv.end(), remainingargv.begin());
285 for (std::vector
<std::string
>::iterator i
= argv
.begin() + 1; i
!= argv
.end(); ++i
)
286 remainingargv
.push_back(*i
);
296 OptionSet::Process (int argc
, char **argV
, Option
*nonOptionHandler
)
304 remainingargv
.clear();
306 for (int counter
= 1; counter
< argc
; ++counter
)
307 argv
.push_back(std::string(argV
[counter
]));
309 return process(nonOptionHandler
);
313 OptionSet::Process (std::vector
<std::string
> const &parms
, Option
*nonOptionHandler
)
315 if (parms
.size() == 0)
319 remainingargv
.clear();
320 return process(nonOptionHandler
);
323 //FIXME: check for conflicts.
325 OptionSet::Register (Option
* anOption
)
327 options
.push_back(anOption
);
331 comp_long_option(const Option
*a
, const Option
*b
)
333 return (a
->longOption().compare(b
->longOption()) < 0);
337 OptionSet::ParameterUsage (std::ostream
&aStream
, StrLookup strLookup
)
339 std::sort(options
.begin(), options
.end(), comp_long_option
);
340 for_each (options
.begin(), options
.end(), DefaultFormatter (aStream
, strLookup
));
343 std::vector
<Option
*> const &
344 OptionSet::optionsInSet() const
349 std::vector
<std::string
> const &
350 OptionSet::nonOptions() const
355 std::vector
<std::string
> const &
356 OptionSet::remainingArgv() const
358 return remainingargv
;
This page took 0.052559 seconds and 6 git commands to generate.