2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1996--2015 Han-Wen Nienhuys, <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 #include "getopt-long.hh"
27 #include "international.hh"
31 gettext (char const *s)
42 Getopt_long::get_argument_index ()
45 if (!optional_argument_str0_
46 || sscanf (optional_argument_str0_, "%ld", &l) != 1)
47 report (E_ILLEGALARG);
52 const Long_option_init *
53 Getopt_long::parselong ()
55 char const *optnm = arg_value_char_a_a_[array_index_] + 2;
58 char const *endopt = strchr (optnm, '=');
59 size_t searchlen = (endopt) ? (size_t) (endopt - optnm) : strlen (optnm);
62 for (int i = 0; i < table_len_; i++)
64 char const *ln = option_a_[i].longname_str0_;
66 if (ln && !strncmp (ln, optnm, searchlen))
68 found_option_ = option_a_ + i;
75 report (E_UNKNOWNOPTION);
81 if (found_option_->take_arg_str0_)
84 optional_argument_str0_ = endopt + 1; // a '='
87 optional_argument_str0_ = arg_value_char_a_a_[array_index_];
90 if (!optional_argument_str0_)
95 optional_argument_str0_ = 0;
97 report (E_NOARGEXPECT);
100 return found_option_;
104 Long_option_init::to_string () const
108 str += string ("-") + shortname_char_;
109 if (shortname_char_ && longname_str0_)
112 str += string ("--") + longname_str0_;
117 Long_option_init::str_for_help () const
121 s = "-" + ::to_string (shortname_char_);
125 s = s + ((shortname_char_ && longname_str0_) ? ", " : " ");
128 s = s + "--" + longname_str0_;
137 s = s + gettext (take_arg_str0_);
142 // report an error, GNU style.
144 Getopt_long::report (Errorcod c)
150 string str = arg_value_char_a_a_[0];
155 str += _f ("option `%s' requires an argument",
156 found_option_->to_string ());
159 str += _f ("option `%s' does not allow an argument",
160 found_option_->to_string ());
162 case E_UNKNOWNOPTION:
163 str += _f ("unrecognized option: `%s'",
164 string (argument_index_
165 ? string ("-" + string (1, arg_value_char_a_a_[array_index_][argument_index_]))
166 : string (arg_value_char_a_a_[array_index_])));
169 str += _f ("invalid argument `%s' to option `%s'",
170 optional_argument_str0_, found_option_->to_string ());
175 fprintf (error_out_, "%s\n", str.c_str ());
179 const Long_option_init *
180 Getopt_long::parseshort ()
182 char c = arg_value_char_a_a_[array_index_][argument_index_];
186 for (int i = 0; i < table_len_; i++)
187 if (option_a_[i].shortname_char_ == c)
189 found_option_ = option_a_ + i;
195 report (E_UNKNOWNOPTION);
200 if (!found_option_->take_arg_str0_)
202 optional_argument_str0_ = 0;
203 return found_option_;
205 optional_argument_str0_ = arg_value_char_a_a_[array_index_] + argument_index_;
210 if (!optional_argument_str0_[0])
212 optional_argument_str0_ = arg_value_char_a_a_[array_index_];
215 if (!optional_argument_str0_)
216 report (E_ARGEXPECT);
218 return found_option_;
221 const Long_option_init *
222 Getopt_long::operator () ()
232 return parseshort ();
234 char const *argument = arg_value_char_a_a_[array_index_];
236 if (argument[0] != '-')
239 if (argument[1] == '-') // what to do with "command -- bla"
251 return parseshort ();
258 Getopt_long::Getopt_long (int c, char **v, Long_option_init *lo)
262 arg_value_char_a_a_ = v;
267 // reached end of option table?
269 for (int i = 0; option_a_[i].longname_str0_ || option_a_[i].shortname_char_; i++)
274 Getopt_long::ok () const
276 return array_index_ < argument_count_;
283 while (array_index_ < argument_count_
284 && !arg_value_char_a_a_[array_index_][argument_index_])
292 Getopt_long::current_arg ()
294 if (array_index_ >= argument_count_)
296 char const *a = arg_value_char_a_a_[array_index_];
297 return a + argument_index_;
301 Getopt_long::get_next_arg ()
303 char const *a = current_arg ();
312 const int EXTRA_SPACES = 5;
315 Long_option_init::table_string (Long_option_init *l)
320 for (int i = 0; l[i].shortname_char_ || l[i].longname_str0_; i++)
321 wid = std::max (wid, l[i].str_for_help ().length ());
323 for (int i = 0; l[i].shortname_char_ || l[i].longname_str0_; i++)
325 string s = " " + l[i].str_for_help ();
326 s += string (wid - s.length () + EXTRA_SPACES, ' ');
328 string help_text (gettext (l[i].help_str0_));
329 replace_all (&help_text, "\n",
330 "\n" + string (wid + EXTRA_SPACES + 2, ' '));
331 tabstr += s + help_text + "\n";
338 Long_option_init::compare (Long_option_init const &a, Long_option_init const &b)
340 if (a.shortname_char_ && b.shortname_char_ && a.shortname_char_ - b.shortname_char_)
341 return a.shortname_char_ - b.shortname_char_;
343 if (b.shortname_char_ && a.longname_str0_)
345 char s[2] = {b.shortname_char_, 0};
346 return strcmp (a.longname_str0_, s);
348 if (a.shortname_char_ && b.longname_str0_)
350 char s[2] = {a.shortname_char_, 0};
351 return strcmp (s, b.longname_str0_);
354 return strcmp (a.longname_str0_, b.longname_str0_);