]> cygwin.com Git - cygwin-apps/setup.git/blob - libgetopt++/src/OptionSet.cc
2003-03-18 Robert Collins <rbtcollins@hotmail.com>
[cygwin-apps/setup.git] / libgetopt++ / src / OptionSet.cc
1 /*
2 * Copyright (c) 2002 Robert Collins.
3 * Copyright (c) 2003 Robert Collins.
4 *
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.
9 *
10 * A copy of the GNU General Public License can be found at
11 * http://www.gnu.org/
12 *
13 * Written by Robert Collins <robertc@hotmail.com>
14 *
15 */
16
17 #if HAVE_CONFIG_H
18 #include "autoconf.h"
19 #endif
20 #include "getopt++/OptionSet.h"
21 #include "getopt++/Option.h"
22
23 #include <iostream>
24
25 using namespace std;
26
27 OptionSet::OptionSet () {}
28 OptionSet::~OptionSet ()
29 {
30 }
31
32 void
33 OptionSet::Init()
34 {
35 options = std::vector<Option *> ();
36 }
37
38 bool OptionSet::Process (int argc, char **argv, OptionSet *defaultOptionSet)
39 {
40 if (argc == 1)
41 {
42 // log (LOG_TIMESTAMP, "No command line options pass\n");
43 return true;
44 }
45 if (options.size() == 0)
46 {
47 // log (LOG_TIMESTAMP,
48 // "%d Command line options passed, and no options registered\n",
49 // argc);
50 return false;
51 }
52 // log (LOG_TIMESTAMP, "Process command line options\n");
53 struct option longopts[options.size() + 1];
54 string
55 shortopts;
56 for (std::vector<Option *>::iterator i = options.begin(); i != options.end(); ++i)
57 {
58 shortopts += (*i)->shortOption ();
59 longopts[distance(options.begin(), i)] = (*i)->longOption ();
60 }
61 char const *
62 opts = shortopts.c_str ();
63 {
64 struct option
65 foo = {
66 0,
67 0,
68 0,
69 0 };
70 longopts[options.size()] = foo;
71 }
72 // where is this correctly defined? opterr=0;
73 int lastoption;
74 while ((lastoption = getopt_long (argc, argv, opts, longopts, 0)) != -1)
75 {
76 if (lastoption)
77 {
78 if (lastoption == '\?')
79 {
80 //ambigous option
81 #if HAVE_STRING___H
82 delete[]opts;
83 #endif
84 return false;
85 }
86 for (std::vector<Option *>::iterator i = options.begin(); i != options.end(); ++i)
87 {
88 if (longopts[distance(options.begin(), i)].val == lastoption && !longopts[distance(options.begin(), i)].flag)
89 (*i)->Process (optarg);
90 }
91 }
92 }
93 if (optind < argc && optind > 0 && defaultOptionSet)
94 return defaultOptionSet->Process (argc - optind, &argv[optind]);
95 #if HAVE_STRING___H
96 delete[]opts;
97 #endif
98 return true;
99 }
100
101 //FIXME: check for conflicts.
102 void
103 OptionSet::Register (Option * anOption)
104 {
105 options.push_back(anOption);
106 }
107
108 /* Show the options on the left, the short description on the right.
109 * descriptions must be < 40 characters in length
110 */
111 void
112 OptionSet::ParameterUsage (ostream &aStream)
113 {
114 for (std::vector<Option *>::iterator i = options.begin(); i != options.end(); ++i)
115 {
116 Option *anOption = (*i);
117 string output = string() + " -" + anOption->shortOption ()[0];
118 output += " --" ;
119 output += anOption->longOption ().name;
120 output += string (40 - output.size(), ' ');
121 string helpmsg = anOption->shortHelp();
122 while (helpmsg.size() > 40)
123 {
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, ' ');
130 }
131 output += helpmsg;
132 aStream << output << endl;
133 }
134 }
135
136 std::vector<Option *> const &
137 OptionSet::optionsInSet() const
138 {
139 return options;
140 }
This page took 0.045766 seconds and 6 git commands to generate.