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_)
84 optional_argument_str0_ = 0;
86 report (E_NOARGEXPECT);
93 Long_option_init::to_string () const
97 str += "-" + shortname_char_;
98 if (shortname_char_ && longname_str0_)
101 str += String ("`--") + longname_str0_ + "'";
106 Long_option_init::str_for_help () const
110 s = "-" + ::to_string (shortname_char_);
114 s = s + ((shortname_char_ && longname_str0_) ? ", " : " ");
117 s = s + "--" + longname_str0_;
126 s = s + gettext (take_arg_str0_);
131 // report an error, GNU style.
133 Getopt_long::report (Errorcod c)
139 String str = arg_value_char_a_a_[0];
144 str += _f ("option `%s' requires an argument",
145 found_option_->to_string ());
148 str += _f ("option `%s' doesn't allow an argument",
149 found_option_->to_string ());
151 case E_UNKNOWNOPTION:
152 str += _f ("unrecognized option: `%s'",
153 String (argument_index_
154 ? String ("-" + String_convert::form_string ("%c",
155 arg_value_char_a_a_[array_index_][argument_index_]))
156 : String (arg_value_char_a_a_[array_index_])));
159 str += _f ("invalid argument `%s' to option `%s'",
160 optional_argument_str0_, found_option_->to_string ());
165 fprintf (error_out_, "%s\n", str.to_str0 ());
169 const Long_option_init *
170 Getopt_long::parseshort ()
172 char c = arg_value_char_a_a_[array_index_][argument_index_];
176 for (int i = 0; i < table_len_; i++)
177 if (option_a_[i].shortname_char_ == c)
179 found_option_ = option_a_ + i;
185 report (E_UNKNOWNOPTION);
190 if (!found_option_->take_arg_str0_)
192 optional_argument_str0_ = 0;
193 return found_option_;
195 optional_argument_str0_ = arg_value_char_a_a_[array_index_] + argument_index_;
200 if (!optional_argument_str0_[0])
202 optional_argument_str0_ = arg_value_char_a_a_[array_index_];
205 if (!optional_argument_str0_)
207 report (E_ARGEXPECT);
210 return found_option_;
213 const Long_option_init *
214 Getopt_long::operator() ()
224 return parseshort ();
226 const char *argument = arg_value_char_a_a_[array_index_];
228 if (argument[0] != '-')
231 if (argument[1] == '-') {// what to do with "command -- bla"
242 return parseshort ();
251 Getopt_long::Getopt_long (int c, char **v, Long_option_init *lo)
255 arg_value_char_a_a_ = v;
260 // reached end of option table?
262 for (int i = 0; option_a_[i].longname_str0_ ||option_a_[i].shortname_char_; i++)
268 Getopt_long::ok () const
270 return array_index_ < argument_count_;
277 while (array_index_ < argument_count_
278 && !arg_value_char_a_a_[array_index_][argument_index_])
286 Getopt_long::current_arg ()
288 if (array_index_ >= argument_count_)
290 char const *a = arg_value_char_a_a_[array_index_];
291 return a + argument_index_;
295 Getopt_long::get_next_arg ()
297 char const *a = current_arg ();
306 const int EXTRA_SPACES = 5;
309 Long_option_init::table_string (Long_option_init *l)
311 String argstr = "ARG";
315 for (int i = 0; l[i].shortname_char_ || l[i].longname_str0_; i++)
317 wid = wid >? l[i].str_for_help ().length ();
320 for (int i = 0; l[i].shortname_char_ || l[i].longname_str0_; i++)
322 String s = " " + l[i].str_for_help ();
323 s += String_convert::char_string (' ', wid - s.length () + EXTRA_SPACES);
325 tabstr += s + gettext (l[i].help_str0_) + "\n";
332 Long_option_init::compare (Long_option_init const &a, Long_option_init const &b)
334 if (a.shortname_char_ && b.shortname_char_ && a.shortname_char_- b.shortname_char_)
335 return a.shortname_char_ - b.shortname_char_;
337 if (b.shortname_char_ && a.longname_str0_)
339 char s[2] = {b.shortname_char_, 0};
340 return strcmp (a.longname_str0_, s);
342 if (a.shortname_char_ && b.longname_str0_)
344 char s[2] = {a.shortname_char_, 0};
345 return strcmp (s, b.longname_str0_);
348 return strcmp (a.longname_str0_, b.longname_str0_);