Format and improve documentation of internal grob properties.
[lilypond.git] / flower / getopt-long.cc
blob2cb537c67b39724afa5d06e57f8697e3e7115277
1 /*
2 process command line, GNU style.
4 this is Copyleft (c) 1996--2007 Han-Wen Nienhuys, <hanwen@xs4all.nl>
5 */
7 #include "getopt-long.hh"
9 #include <cstring>
10 #include <cassert>
11 #include <cstdlib>
13 #include "config.hh"
14 #include "international.hh"
16 #if !HAVE_GETTEXT
17 inline char *
18 gettext (char const *s)
20 return (char *)s;
22 #else
23 #include <libintl.h>
24 #endif
26 long
27 Getopt_long::get_argument_index ()
29 long l;
30 if (!optional_argument_str0_
31 || sscanf (optional_argument_str0_, "%ld", &l) != 1)
32 report (E_ILLEGALARG);
34 return l;
37 const Long_option_init *
38 Getopt_long::parselong ()
40 char const *optnm = arg_value_char_a_a_[array_index_] + 2;
41 assert (*optnm);
43 char const *endopt = strchr (optnm, '=');
44 int searchlen = (endopt) ? endopt - optnm : strlen (optnm);
46 found_option_ = 0;
47 for (int i = 0; i < table_len_; i++)
49 char const *ln = option_a_[i].longname_str0_;
51 if (ln && !strncmp (ln, optnm, searchlen))
53 found_option_ = option_a_ + i;
54 break;
58 if (!found_option_)
60 report (E_UNKNOWNOPTION);
61 return 0;
63 array_index_++;
64 argument_index_ = 0;
66 if (found_option_->take_arg_str0_)
68 if (endopt)
69 optional_argument_str0_ = endopt +1; // a '='
70 else
72 optional_argument_str0_ = arg_value_char_a_a_[array_index_];
73 array_index_++;
75 if (!optional_argument_str0_)
76 report (E_ARGEXPECT);
78 else
80 optional_argument_str0_ = 0;
81 if (endopt)
82 report (E_NOARGEXPECT);
85 return found_option_;
88 string
89 Long_option_init::to_string () const
91 string str;
92 if (shortname_char_)
93 str += "-" + shortname_char_;
94 if (shortname_char_ && longname_str0_)
95 str += ", ";
96 if (longname_str0_)
97 str += string ("`--") + longname_str0_ + "'";
98 return str;
101 string
102 Long_option_init::str_for_help () const
104 string s;
105 if (shortname_char_)
106 s = "-" + ::to_string (shortname_char_);
107 else
108 s = " ";
110 s = s + ((shortname_char_ && longname_str0_) ? ", " : " ");
112 if (longname_str0_)
113 s = s + "--" + longname_str0_;
115 if (take_arg_str0_)
117 if (longname_str0_)
118 s = s + "=";
119 else
120 s = s + " ";
122 s = s + gettext (take_arg_str0_);
124 return s;
127 // report an error, GNU style.
128 void
129 Getopt_long::report (Errorcod c)
131 error_ = c;
132 if (!error_out_)
133 return;
135 string str = arg_value_char_a_a_[0];
136 str += ": ";
137 switch (c)
139 case E_ARGEXPECT:
140 str += _f ("option `%s' requires an argument",
141 found_option_->to_string ());
142 break;
143 case E_NOARGEXPECT:
144 str += _f ("option `%s' does not allow an argument",
145 found_option_->to_string ());
146 break;
147 case E_UNKNOWNOPTION:
148 str += _f ("unrecognized option: `%s'",
149 string (argument_index_
150 ? string ("-" + string (1, arg_value_char_a_a_[array_index_][argument_index_]))
151 : string (arg_value_char_a_a_[array_index_])));
152 break;
153 case E_ILLEGALARG:
154 str += _f ("invalid argument `%s' to option `%s'",
155 optional_argument_str0_, found_option_->to_string ());
156 break;
157 default:
158 assert (false);
160 fprintf (error_out_, "%s\n", str.c_str ());
161 exit (2);
164 const Long_option_init *
165 Getopt_long::parseshort ()
167 char c = arg_value_char_a_a_[array_index_][argument_index_];
168 found_option_ = 0;
169 assert (c);
171 for (int i = 0; i < table_len_; i++)
172 if (option_a_[i].shortname_char_ == c)
174 found_option_ = option_a_ + i;
175 break;
178 if (!found_option_)
180 report (E_UNKNOWNOPTION);
181 return 0;
184 argument_index_++;
185 if (!found_option_->take_arg_str0_)
187 optional_argument_str0_ = 0;
188 return found_option_;
190 optional_argument_str0_ = arg_value_char_a_a_[array_index_] + argument_index_;
192 array_index_++;
193 argument_index_ = 0;
195 if (!optional_argument_str0_[0])
197 optional_argument_str0_ = arg_value_char_a_a_[array_index_];
198 array_index_++;
200 if (!optional_argument_str0_)
201 report (E_ARGEXPECT);
203 return found_option_;
206 const Long_option_init *
207 Getopt_long::operator () ()
209 if (!ok ())
210 return 0;
212 next ();
213 if (!ok ())
214 return 0;
216 if (argument_index_)
217 return parseshort ();
219 char const *argument = arg_value_char_a_a_[array_index_];
221 if (argument[0] != '-')
222 return 0;
224 if (argument[1] == '-') {// what to do with "command -- bla"
225 if (argument[2])
226 return parselong ();
227 else
228 return 0;
230 else
232 if (argument[ 1 ])
234 argument_index_ = 1;
235 return parseshort ();
237 else
238 return 0;
242 Getopt_long::Getopt_long (int c, char **v, Long_option_init *lo)
244 option_a_ = lo;
245 error_out_ = stderr;
246 arg_value_char_a_a_ = v;
247 argument_count_ = c;
248 array_index_ = 1;
249 argument_index_ = 0;
251 // reached end of option table?
252 table_len_ = 0;
253 for (int i = 0; option_a_[i].longname_str0_ || option_a_[i].shortname_char_; i++)
254 table_len_++;
257 bool
258 Getopt_long::ok () const
260 return array_index_ < argument_count_;
263 void
264 Getopt_long::next ()
266 error_ = E_NOERROR;
267 while (array_index_ < argument_count_
268 && !arg_value_char_a_a_[array_index_][argument_index_])
270 array_index_++;
271 argument_index_ = 0;
275 char const *
276 Getopt_long::current_arg ()
278 if (array_index_ >= argument_count_)
279 return 0;
280 char const *a = arg_value_char_a_a_[array_index_];
281 return a + argument_index_;
284 char const *
285 Getopt_long::get_next_arg ()
287 char const *a = current_arg ();
288 if (a)
290 array_index_++;
291 argument_index_ = 0;
293 return a;
296 const int EXTRA_SPACES = 5;
298 string
299 Long_option_init::table_string (Long_option_init *l)
301 string tabstr = "";
303 int wid = 0;
304 for (int i = 0; l[i].shortname_char_ || l[i].longname_str0_; i++)
305 wid = max (int(wid), int(l[i].str_for_help ().length ()));
307 for (int i = 0; l[i].shortname_char_ || l[i].longname_str0_; i++)
309 string s = " " + l[i].str_for_help ();
310 s += string (wid - s.length () + EXTRA_SPACES, ' ');
312 string help_text (gettext (l[i].help_str0_));
313 replace_all (help_text, "\n",
314 "\n" + string (wid + EXTRA_SPACES + 2, ' '));
315 tabstr += s + help_text + "\n";
318 return tabstr;
322 Long_option_init::compare (Long_option_init const &a, Long_option_init const &b)
324 if (a.shortname_char_ && b.shortname_char_ && a.shortname_char_- b.shortname_char_)
325 return a.shortname_char_ - b.shortname_char_;
327 if (b.shortname_char_ && a.longname_str0_)
329 char s[2] = {b.shortname_char_, 0};
330 return strcmp (a.longname_str0_, s);
332 if (a.shortname_char_ && b.longname_str0_)
334 char s[2] = {a.shortname_char_, 0};
335 return strcmp (s, b.longname_str0_);
338 return strcmp (a.longname_str0_, b.longname_str0_);