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);
69 if (found_option_->take_arg_str0_)
72 optional_argument_str0_ = endopt +1; // a '='
75 optional_argument_str0_ = arg_value_char_a_a_[array_index_];
78 if (!optional_argument_str0_)
83 optional_argument_str0_ = 0;
85 report (E_NOARGEXPECT);
92 Long_option_init::to_string () const
96 str += "-" + shortname_char_;
97 if (shortname_char_ && longname_str0_)
100 str += String ("`--") + longname_str0_ + "'";
105 Long_option_init::str_for_help () const
109 s = "-" + ::to_string (shortname_char_);
113 s = s + ((shortname_char_ && longname_str0_) ? ", " : " ");
116 s = s + "--" + longname_str0_;
125 s = s + gettext (take_arg_str0_);
130 // report an error, GNU style.
132 Getopt_long::report (Errorcod c)
138 String str = arg_value_char_a_a_[0];
143 str += _f ("option `%s' requires an argument",
144 found_option_->to_string ());
147 str += _f ("option `%s' doesn't allow an argument",
148 found_option_->to_string ());
150 case E_UNKNOWNOPTION:
151 str += _f ("unrecognized option: `%s'",
152 String (argument_index_
153 ? String ("-" + String_convert::form_string ("%c",
154 arg_value_char_a_a_[array_index_][argument_index_]))
155 : String (arg_value_char_a_a_[array_index_])));
158 str += _f ("invalid argument `%s' to option `%s'",
159 optional_argument_str0_, found_option_->to_string ());
164 fprintf (error_out_, "%s\n", str.to_str0 ());
168 const Long_option_init *
169 Getopt_long::parseshort ()
171 char c = arg_value_char_a_a_[array_index_][argument_index_];
175 for (int i = 0; i < table_len_; i++)
176 if (option_a_[i].shortname_char_ == c)
178 found_option_ = option_a_ + i;
184 report (E_UNKNOWNOPTION);
189 if (!found_option_->take_arg_str0_)
191 optional_argument_str0_ = 0;
192 return found_option_;
194 optional_argument_str0_ = arg_value_char_a_a_[array_index_] + argument_index_;
199 if (!optional_argument_str0_[0])
201 optional_argument_str0_ = arg_value_char_a_a_[array_index_];
204 if (!optional_argument_str0_)
206 report (E_ARGEXPECT);
209 return found_option_;
212 const Long_option_init *
213 Getopt_long::operator () ()
223 return parseshort ();
225 const char *argument = arg_value_char_a_a_[array_index_];
227 if (argument[0] != '-')
230 if (argument[1] == '-') {// what to do with "command -- bla"
241 return parseshort ();
250 Getopt_long::Getopt_long (int c, char **v, Long_option_init *lo)
254 arg_value_char_a_a_ = v;
259 // reached end of option table?
261 for (int i = 0; option_a_[i].longname_str0_ || option_a_[i].shortname_char_; i++)
266 Getopt_long::ok () const
268 return array_index_ < argument_count_;
275 while (array_index_ < argument_count_
276 && !arg_value_char_a_a_[array_index_][argument_index_])
284 Getopt_long::current_arg ()
286 if (array_index_ >= argument_count_)
288 char const *a = arg_value_char_a_a_[array_index_];
289 return a + argument_index_;
293 Getopt_long::get_next_arg ()
295 char const *a = current_arg ();
304 const int EXTRA_SPACES = 5;
307 Long_option_init::table_string (Long_option_init *l)
309 String argstr = "ARG";
313 for (int i = 0; l[i].shortname_char_ || l[i].longname_str0_; i++)
315 wid = max (wid, l[i].str_for_help ().length ());
318 for (int i = 0; l[i].shortname_char_ || l[i].longname_str0_; i++)
320 String s = " " + l[i].str_for_help ();
321 s += String_convert::char_string (' ', wid - s.length () + EXTRA_SPACES);
323 tabstr += s + gettext (l[i].help_str0_) + "\n";
330 Long_option_init::compare (Long_option_init const &a, Long_option_init const &b)
332 if (a.shortname_char_ && b.shortname_char_ && a.shortname_char_- b.shortname_char_)
333 return a.shortname_char_ - b.shortname_char_;
335 if (b.shortname_char_ && a.longname_str0_)
337 char s[2] = {b.shortname_char_, 0};
338 return strcmp (a.longname_str0_, s);
340 if (a.shortname_char_ && b.longname_str0_)
342 char s[2] = {a.shortname_char_, 0};
343 return strcmp (s, b.longname_str0_);
346 return strcmp (a.longname_str0_, b.longname_str0_);