2 process command line, GNU style.
4 this is Copyleft (c) 1996--2005 Han-Wen Nienhuys, <hanwen@cs.uu.nl>
7 #include "getopt-long.hh"
16 #include "string-convert.hh"
20 gettext (char const *s)
29 Getopt_long::get_argument_index ()
32 if (!optional_argument_str0_
33 || sscanf (optional_argument_str0_, "%ld", &l) != 1)
34 report (E_ILLEGALARG);
39 const Long_option_init *
40 Getopt_long::parselong ()
42 char const *optnm = arg_value_char_a_a_[array_index_] + 2;
45 char const *endopt = strchr (optnm, '=');
46 int searchlen = (endopt) ? endopt - optnm : strlen (optnm);
49 for (int i = 0; i < table_len_; i++)
51 char const *ln = option_a_[i].longname_str0_;
53 if (ln && !strncmp (ln, optnm, searchlen))
55 found_option_ = option_a_ + i;
62 report (E_UNKNOWNOPTION);
68 if (found_option_->take_arg_str0_)
71 optional_argument_str0_ = endopt +1; // a '='
74 optional_argument_str0_ = arg_value_char_a_a_[array_index_];
77 if (!optional_argument_str0_)
82 optional_argument_str0_ = 0;
84 report (E_NOARGEXPECT);
91 Long_option_init::to_string () const
95 str += "-" + shortname_char_;
96 if (shortname_char_ && longname_str0_)
99 str += String ("`--") + longname_str0_ + "'";
104 Long_option_init::str_for_help () const
108 s = "-" + ::to_string (shortname_char_);
112 s = s + ((shortname_char_ && longname_str0_) ? ", " : " ");
115 s = s + "--" + longname_str0_;
124 s = s + gettext (take_arg_str0_);
129 // report an error, GNU style.
131 Getopt_long::report (Errorcod c)
137 String str = arg_value_char_a_a_[0];
142 str += _f ("option `%s' requires an argument",
143 found_option_->to_string ());
146 str += _f ("option `%s' doesn't allow an argument",
147 found_option_->to_string ());
149 case E_UNKNOWNOPTION:
150 str += _f ("unrecognized option: `%s'",
151 String (argument_index_
152 ? String ("-" + String_convert::form_string ("%c",
153 arg_value_char_a_a_[array_index_][argument_index_]))
154 : String (arg_value_char_a_a_[array_index_])));
157 str += _f ("invalid argument `%s' to option `%s'",
158 optional_argument_str0_, found_option_->to_string ());
163 fprintf (error_out_, "%s\n", str.to_str0 ());
167 const Long_option_init *
168 Getopt_long::parseshort ()
170 char c = arg_value_char_a_a_[array_index_][argument_index_];
174 for (int i = 0; i < table_len_; i++)
175 if (option_a_[i].shortname_char_ == c)
177 found_option_ = option_a_ + i;
183 report (E_UNKNOWNOPTION);
188 if (!found_option_->take_arg_str0_)
190 optional_argument_str0_ = 0;
191 return found_option_;
193 optional_argument_str0_ = arg_value_char_a_a_[array_index_] + argument_index_;
198 if (!optional_argument_str0_[0])
200 optional_argument_str0_ = arg_value_char_a_a_[array_index_];
203 if (!optional_argument_str0_)
204 report (E_ARGEXPECT);
206 return found_option_;
209 const Long_option_init *
210 Getopt_long::operator () ()
220 return parseshort ();
222 char const *argument = arg_value_char_a_a_[array_index_];
224 if (argument[0] != '-')
227 if (argument[1] == '-') {// what to do with "command -- bla"
238 return parseshort ();
245 Getopt_long::Getopt_long (int c, char **v, Long_option_init *lo)
249 arg_value_char_a_a_ = v;
254 // reached end of option table?
256 for (int i = 0; option_a_[i].longname_str0_ || option_a_[i].shortname_char_; i++)
261 Getopt_long::ok () const
263 return array_index_ < argument_count_;
270 while (array_index_ < argument_count_
271 && !arg_value_char_a_a_[array_index_][argument_index_])
279 Getopt_long::current_arg ()
281 if (array_index_ >= argument_count_)
283 char const *a = arg_value_char_a_a_[array_index_];
284 return a + argument_index_;
288 Getopt_long::get_next_arg ()
290 char const *a = current_arg ();
299 const int EXTRA_SPACES = 5;
302 Long_option_init::table_string (Long_option_init *l)
307 for (int i = 0; l[i].shortname_char_ || l[i].longname_str0_; i++)
308 wid = max (wid, l[i].str_for_help ().length ());
310 for (int i = 0; l[i].shortname_char_ || l[i].longname_str0_; i++)
312 String s = " " + l[i].str_for_help ();
313 s += String_convert::char_string (' ', wid - s.length () + EXTRA_SPACES);
315 String help_text (gettext (l[i].help_str0_));
316 help_text.substitute ("\n", "\n" + String_convert::char_string (' ', wid + EXTRA_SPACES + 2));
317 tabstr += s + help_text + "\n";
324 Long_option_init::compare (Long_option_init const &a, Long_option_init const &b)
326 if (a.shortname_char_ && b.shortname_char_ && a.shortname_char_- b.shortname_char_)
327 return a.shortname_char_ - b.shortname_char_;
329 if (b.shortname_char_ && a.longname_str0_)
331 char s[2] = {b.shortname_char_, 0};
332 return strcmp (a.longname_str0_, s);
334 if (a.shortname_char_ && b.longname_str0_)
336 char s[2] = {a.shortname_char_, 0};
337 return strcmp (s, b.longname_str0_);
340 return strcmp (a.longname_str0_, b.longname_str0_);