]>
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"
27 OptionSet::OptionSet () {}
28 OptionSet::~OptionSet ()
35 options
= std::vector
<Option
*> ();
38 bool OptionSet::Process (int argc
, char **argv
, OptionSet
*defaultOptionSet
)
42 // log (LOG_TIMESTAMP, "No command line options pass\n");
45 if (options
.size() == 0)
47 // log (LOG_TIMESTAMP,
48 // "%d Command line options passed, and no options registered\n",
52 // log (LOG_TIMESTAMP, "Process command line options\n");
53 struct option longopts
[options
.size() + 1];
56 for (std::vector
<Option
*>::iterator i
= options
.begin(); i
!= options
.end(); ++i
)
58 shortopts
+= (*i
)->shortOption ();
59 longopts
[distance(options
.begin(), i
)] = (*i
)->longOption ();
62 opts
= shortopts
.c_str ();
70 longopts
[options
.size()] = foo
;
72 // where is this correctly defined? opterr=0;
74 while ((lastoption
= getopt_long (argc
, argv
, opts
, longopts
, 0)) != -1)
78 if (lastoption
== '\?')
86 for (std::vector
<Option
*>::iterator i
= options
.begin(); i
!= options
.end(); ++i
)
88 if (longopts
[distance(options
.begin(), i
)].val
== lastoption
&& !longopts
[distance(options
.begin(), i
)].flag
)
89 (*i
)->Process (optarg
);
93 if (optind
< argc
&& optind
> 0 && defaultOptionSet
)
94 return defaultOptionSet
->Process (argc
- optind
, &argv
[optind
]);
101 //FIXME: check for conflicts.
103 OptionSet::Register (Option
* anOption
)
105 options
.push_back(anOption
);
108 /* Show the options on the left, the short description on the right.
109 * descriptions must be < 40 characters in length
112 OptionSet::ParameterUsage (ostream
&aStream
)
114 for (std::vector
<Option
*>::iterator i
= options
.begin(); i
!= options
.end(); ++i
)
116 Option
*anOption
= (*i
);
117 string output
= string() + " -" + anOption
->shortOption ()[0];
119 output
+= anOption
->longOption ().name
;
120 output
+= string (40 - output
.size(), ' ');
121 string helpmsg
= anOption
->shortHelp();
122 while (helpmsg
.size() > 40)
124 // TODO: consider using a line breaking class here.
125 int pos
= helpmsg
.substr(0,40).find_last_of(" ");
126 output
+= helpmsg
.substr(0,pos
);
127 helpmsg
.erase (0,pos
+1);
128 aStream
<< output
<< endl
;
129 output
= string (40, ' ');
132 aStream
<< output
<< endl
;
136 std::vector
<Option
*> const &
137 OptionSet::optionsInSet() const
This page took 0.045766 seconds and 6 git commands to generate.