2 process command line, GNU style.
4 this is Copyleft (c) 1996--2005 Han-Wen Nienhuys, <hanwen@xs4all.nl>
7 #include "getopt-long.hh"
17 #include "string-convert.hh"
21 gettext (char const *s)
30 Getopt_long::get_argument_index ()
33 if (!optional_argument_str0_
34 || sscanf (optional_argument_str0_, "%ld", &l) != 1)
35 report (E_ILLEGALARG);
40 const Long_option_init *
41 Getopt_long::parselong ()
43 char const *optnm = arg_value_char_a_a_[array_index_] + 2;
46 char const *endopt = strchr (optnm, '=');
47 int searchlen = (endopt) ? endopt - optnm : strlen (optnm);
50 for (int i = 0; i < table_len_; i++)
52 char const *ln = option_a_[i].longname_str0_;
54 if (ln && !strncmp (ln, optnm, searchlen))
56 found_option_ = option_a_ + i;
63 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_)
205 report (E_ARGEXPECT);
207 return found_option_;
210 const Long_option_init *
211 Getopt_long::operator () ()
221 return parseshort ();
223 char const *argument = arg_value_char_a_a_[array_index_];
225 if (argument[0] != '-')
228 if (argument[1] == '-') {// what to do with "command -- bla"
239 return parseshort ();
246 Getopt_long::Getopt_long (int c, char **v, Long_option_init *lo)
250 arg_value_char_a_a_ = v;
255 // reached end of option table?
257 for (int i = 0; option_a_[i].longname_str0_ || option_a_[i].shortname_char_; i++)
262 Getopt_long::ok () const
264 return array_index_ < argument_count_;
271 while (array_index_ < argument_count_
272 && !arg_value_char_a_a_[array_index_][argument_index_])
280 Getopt_long::current_arg ()
282 if (array_index_ >= argument_count_)
284 char const *a = arg_value_char_a_a_[array_index_];
285 return a + argument_index_;
289 Getopt_long::get_next_arg ()
291 char const *a = current_arg ();
300 const int EXTRA_SPACES = 5;
303 Long_option_init::table_string (Long_option_init *l)
308 for (int i = 0; l[i].shortname_char_ || l[i].longname_str0_; i++)
309 wid = max (wid, l[i].str_for_help ().length ());
311 for (int i = 0; l[i].shortname_char_ || l[i].longname_str0_; i++)
313 String s = " " + l[i].str_for_help ();
314 s += String_convert::char_string (' ', wid - s.length () + EXTRA_SPACES);
316 String help_text (gettext (l[i].help_str0_));
317 help_text.substitute ("\n", "\n" + String_convert::char_string (' ', wid + EXTRA_SPACES + 2));
318 tabstr += s + help_text + "\n";
325 Long_option_init::compare (Long_option_init const &a, Long_option_init const &b)
327 if (a.shortname_char_ && b.shortname_char_ && a.shortname_char_- b.shortname_char_)
328 return a.shortname_char_ - b.shortname_char_;
330 if (b.shortname_char_ && a.longname_str0_)
332 char s[2] = {b.shortname_char_, 0};
333 return strcmp (a.longname_str0_, s);
335 if (a.shortname_char_ && b.longname_str0_)
337 char s[2] = {a.shortname_char_, 0};
338 return strcmp (s, b.longname_str0_);
341 return strcmp (a.longname_str0_, b.longname_str0_);