2 process command line, GNU style.
4 this is (Copyleft) 1996, Han-Wen Nienhuys, <hanwen@cs.uu.nl>
10 #include "getopt-long.hh"
11 #include "international.hh"
14 Getopt_long::argument_to_i()
17 if (!optional_argument_ch_C_
18 || sscanf (optional_argument_ch_C_, "%ld", &l) != 1)
19 report (E_ILLEGALARG);
24 const Long_option_init *
25 Getopt_long::parselong()
27 char const *optnm = arg_value_ch_a_a_[array_index_i_] + 2 ;
30 char const *endopt = strchr (optnm, '=');
31 int searchlen = (endopt) ? endopt - optnm : strlen (optnm);
34 for (int i=0; i< table_len_i_; i++)
36 char const *ln = option_a_[i].longname;
38 if (ln && !strncmp (ln, optnm, searchlen))
40 found_option_l_ = option_a_+i;
47 report (E_UNKNOWNOPTION);
51 argument_index_i_ = 0;
54 if (found_option_l_->take_arg)
57 optional_argument_ch_C_ = endopt +1; // a '='
60 optional_argument_ch_C_ = arg_value_ch_a_a_[array_index_i_];
63 if (!optional_argument_ch_C_)
69 optional_argument_ch_C_ = 0;
71 report (E_NOARGEXPECT);
74 return found_option_l_;
78 Long_option_init::str () const
82 str += "-" + shortname;
83 if (shortname && longname)
86 str += String ("`--") + longname + "'";
90 // report an error, GNU style.
92 Getopt_long::report (Errorcod c)
95 if (!error_ostream_l_)
98 String str = arg_value_ch_a_a_[0];
103 str += _f ("option `%s\' requires an argument",
104 found_option_l_->str ());
107 str += _f ("option `%s\' doesn't allow an argument",
108 found_option_l_->str ());
110 case E_UNKNOWNOPTION:
111 str += _f ("unrecognized option: `%s\'",
112 String (argument_index_i_
113 ? String ("-" + _f("%c",arg_value_ch_a_a_[array_index_i_][argument_index_i_]))
114 : String (arg_value_ch_a_a_[array_index_i_])));
117 str += _f ("invalid argument `%s\' to option `%s'",
118 optional_argument_ch_C_, found_option_l_->str ());
122 *error_ostream_l_ << str << endl;
126 const Long_option_init *
127 Getopt_long::parseshort()
129 char c=arg_value_ch_a_a_[array_index_i_][argument_index_i_];
133 for (int i=0; i < table_len_i_; i++)
134 if (option_a_[i].shortname == c)
136 found_option_l_ = option_a_+i;
140 if (!found_option_l_)
142 report (E_UNKNOWNOPTION);
147 if (!found_option_l_->take_arg)
149 optional_argument_ch_C_ = 0;
150 return found_option_l_;
152 optional_argument_ch_C_ = arg_value_ch_a_a_[array_index_i_] + argument_index_i_;
155 argument_index_i_ = 0;
157 if (!optional_argument_ch_C_[0])
159 optional_argument_ch_C_ = arg_value_ch_a_a_[array_index_i_];
162 if (!optional_argument_ch_C_)
164 report (E_ARGEXPECT);
167 return found_option_l_;
170 const Long_option_init *
171 Getopt_long::operator()()
180 if (argument_index_i_)
183 const char * argument_C = arg_value_ch_a_a_[array_index_i_];
185 if (argument_C[0] != '-')
188 if (argument_C[1] == '-') {// what to do with "command -- bla"
198 argument_index_i_ = 1;
208 Getopt_long::Getopt_long (int c, char **v, Long_option_init *lo)
211 error_ostream_l_ = &cerr;
212 arg_value_ch_a_a_ = v;
213 argument_count_i_ = c;
215 argument_index_i_ = 0;
217 // reached end of option table?
219 for (int i = 0; option_a_[i].longname ||option_a_[i].shortname; i++)
224 Getopt_long::ok() const
226 return array_index_i_ < argument_count_i_;
233 while (array_index_i_ < argument_count_i_
234 && !arg_value_ch_a_a_[array_index_i_][argument_index_i_])
237 argument_index_i_ = 0;
242 Getopt_long::current_arg()
244 if (array_index_i_ >= argument_count_i_)
246 char const * a = arg_value_ch_a_a_[array_index_i_];
247 return a + argument_index_i_;
251 Getopt_long::get_next_arg()
253 char const * a = current_arg();
257 argument_index_i_= 0;