]> cygwin.com Git - cygwin-apps/setup.git/blob - libgetopt++/src/OptionSet.cc
2003-11-16 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 #include "getopt++/DefaultFormatter.h"
23
24 #include <iostream>
25
26 using namespace std;
27
28 OptionSet::OptionSet () {}
29 OptionSet::~OptionSet ()
30 {
31 }
32
33 void
34 OptionSet::Init()
35 {
36 options = std::vector<Option *> ();
37 }
38
39 bool
40 OptionSet::Process (int argc, char **argv, OptionSet *defaultOptionSet)
41 {
42 if (argc == 1)
43 {
44 // log (LOG_TIMESTAMP, "No command line options pass\n");
45 return true;
46 }
47 if (options.size() == 0)
48 {
49 if (defaultOptionSet)
50 return defaultOptionSet->Process (argc, argv);
51 // log (LOG_TIMESTAMP,
52 // "%d Command line options passed, and no options registered\n",
53 // argc);
54 return false;
55 }
56 // log (LOG_TIMESTAMP, "Process command line options\n");
57 struct option longopts[options.size() + 1];
58 string shortopts;
59 for (std::vector<Option *>::iterator i = options.begin(); i != options.end(); ++i)
60 {
61 shortopts += (*i)->shortOption ();
62 longopts[distance(options.begin(), i)] = (*i)->longOption ();
63 }
64 char const * opts = shortopts.c_str ();
65 {
66 struct option foo = {0, 0, 0, 0};
67 longopts[options.size()] = foo;
68 }
69 // where is this correctly defined? opterr=0;
70 int lastoption;
71 while ((lastoption = getopt_long (argc, argv, opts, longopts, 0)) != -1)
72 {
73 if (lastoption)
74 {
75 if (lastoption == '\?')
76 {
77 //ambigous option
78 #if HAVE_STRING___H
79 delete[]opts;
80 #endif
81 return false;
82 }
83 for (std::vector<Option *>::iterator i = options.begin(); i != options.end(); ++i)
84 {
85 if (longopts[distance(options.begin(), i)].val == lastoption && !longopts[distance(options.begin(), i)].flag)
86 (*i)->Process (optarg);
87 }
88 }
89 }
90
91 if (optind < argc && optind > 0 && defaultOptionSet)
92 return defaultOptionSet->Process (argc - optind, &argv[optind]);
93 #if HAVE_STRING___H
94 delete[]opts;
95 #endif
96 return true;
97 }
98
99 //FIXME: check for conflicts.
100 void
101 OptionSet::Register (Option * anOption)
102 {
103 options.push_back(anOption);
104 }
105
106 void
107 OptionSet::ParameterUsage (ostream &aStream)
108 {
109 for_each (options.begin(), options.end(), DefaultFormatter (aStream));
110 }
111
112 std::vector<Option *> const &
113 OptionSet::optionsInSet() const
114 {
115 return options;
116 }
This page took 0.039405 seconds and 6 git commands to generate.