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