]> cygwin.com Git - cygwin-apps/setup.git/blame - libgetopt++/src/OptionSet.cc
2002-04-23 Robert Collins <rbtcollins@hotmail.com>
[cygwin-apps/setup.git] / libgetopt++ / src / OptionSet.cc
CommitLineData
7419f059
RC
1/*
2 * Copyright (c) 2002 Robert Collins.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * A copy of the GNU General Public License can be found at
10 * http://www.gnu.org/
11 *
12 * Written by Robert Collins <robertc@hotmail.com>
13 *
14 */
15
16#if HAVE_CONFIG_H
17#include "autoconf.h"
18#endif
19#include "getopt++/OptionSet.h"
20#include "getopt++/Option.h"
21
1ae8e28d
RC
22#include <iostream>
23
7419f059
RC
24OptionSet::OptionSet () : options(0), optCount (0) {}
25OptionSet::~OptionSet ()
26{
27 delete[] options;
28}
29
30void
31OptionSet::Init()
32{
33 options = 0;
34 optCount = 0;
35}
36
1ae8e28d 37bool OptionSet::Process (int argc, char **argv, OptionSet *defaultOptionSet)
7419f059
RC
38{
39 if (argc == 1)
40 {
41// log (LOG_TIMESTAMP, "No command line options pass\n");
42 return true;
43 }
44 if (optCount == 0)
45 {
46// log (LOG_TIMESTAMP,
47// "%d Command line options passed, and no options registered\n",
48// argc);
49 return false;
50 }
51// log (LOG_TIMESTAMP, "Process command line options\n");
52 struct option longopts[optCount + 1];
290fdf73 53 string
7419f059
RC
54 shortopts;
55 for (int i = 0; i < optCount; ++i)
56 {
57 Option *anOption = options[i];
58 shortopts += anOption->shortOption ();
59 longopts[i] = anOption->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[optCount] = 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 (int i = 0; i < optCount; ++i)
87 {
88 if (longopts[i].val == lastoption && !longopts[i].flag)
89 options[i]->Process (optarg);
90 }
91 }
92 }
1ae8e28d
RC
93 if (optind < argc && optind > 0 && defaultOptionSet)
94 return defaultOptionSet->Process (argc - optind, &argv[optind]);
7419f059
RC
95#if HAVE_STRING___H
96 delete[]opts;
97#endif
98 return true;
99}
100
101//FIXME: check for conflicts.
102void
103OptionSet::Register (Option * anOption)
104{
105 Option **t = new Option *[optCount + 1];
106 for (int i = 0; i < optCount; ++i)
107 t[i] = options[i];
108 t[optCount++] = anOption;
109 delete[]options;
110 options = t;
111}
1ae8e28d
RC
112
113/* Show the options on the left, the short description on the right.
114 * descriptions must be < 40 characters in length
115 */
116void
117OptionSet::ParameterUsage (ostream &aStream)
118{
119 for (int i = 0; i < optCount; ++i)
120 {
121 Option *anOption = options[i];
290fdf73 122 string output = string() + " -" + anOption->shortOption ()[0];
1ae8e28d
RC
123 output += " --" ;
124 output += anOption->longOption ().name;
290fdf73
RC
125 output += string (40 - output.size(), ' ');
126 string helpmsg = anOption->shortHelp();
18b5620f
RC
127 while (helpmsg.size() > 40)
128 {
129 // TODO: consider using a line breaking class here.
130 int pos = helpmsg.substr(0,40).find_last_of(" ");
131 output += helpmsg.substr(0,pos);
132 helpmsg.erase (0,pos+1);
133 aStream << output << endl;
290fdf73 134 output = string (40, ' ');
18b5620f
RC
135 }
136 output += helpmsg;
1ae8e28d
RC
137 aStream << output << endl;
138 }
139}
This page took 0.037804 seconds and 5 git commands to generate.