2 process command line, GNU style.
4 this is (Copyleft) 1996, Han-Wen Nienhuys, <hanwen@stack.nl>
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_;
79 Long_option_init::printon (ostream &errorout) const
82 errorout <<"-" << shortname;
83 if (shortname && longname)
86 errorout << "`--" << longname << "'";
89 // report an error, GNU style.
91 Getopt_long::report (Errorcod c)
94 if (!error_ostream_l_)
97 *error_ostream_l_ << arg_value_ch_a_a_[0] << ": ";
101 *error_ostream_l_<< _("option ");
102 found_option_l_->printon (*error_ostream_l_);
103 *error_ostream_l_ << _("requires an argument")<<endl;
106 *error_ostream_l_ << _("option `--") <<
107 found_option_l_->longname << _("' does not allow an argument")<<endl;
110 case E_UNKNOWNOPTION:
111 *error_ostream_l_ << _("unrecognized option ");
112 if (argument_index_i_)
113 *error_ostream_l_ << "-" << arg_value_ch_a_a_[array_index_i_][argument_index_i_] << endl;
115 *error_ostream_l_ << arg_value_ch_a_a_[array_index_i_] << endl;
119 *error_ostream_l_ << _("illegal argument `") << optional_argument_ch_C_ << _("\'to option ");
120 found_option_l_->printon (*error_ostream_l_);
121 *error_ostream_l_ << '\n';
128 const Long_option_init *
129 Getopt_long::parseshort()
131 char c=arg_value_ch_a_a_[array_index_i_][argument_index_i_];
135 for (int i=0; i < table_len_i_; i++)
136 if (option_a_[i].shortname == c)
138 found_option_l_ = option_a_+i;
142 if (!found_option_l_)
144 report (E_UNKNOWNOPTION);
149 if (!found_option_l_->take_arg)
151 optional_argument_ch_C_ = 0;
152 return found_option_l_;
154 optional_argument_ch_C_ = arg_value_ch_a_a_[array_index_i_] + argument_index_i_;
157 argument_index_i_ = 0;
159 if (!optional_argument_ch_C_[0])
161 optional_argument_ch_C_ = arg_value_ch_a_a_[array_index_i_];
164 if (!optional_argument_ch_C_)
166 report (E_ARGEXPECT);
169 return found_option_l_;
172 const Long_option_init *
173 Getopt_long::operator()()
182 if (argument_index_i_)
185 const char * argument_C = arg_value_ch_a_a_[array_index_i_];
187 if (argument_C[0] != '-')
190 if (argument_C[1] == '-') {// what to do with "command -- bla"
200 argument_index_i_ = 1;
210 Getopt_long::Getopt_long (int c, char **v, Long_option_init *lo)
213 error_ostream_l_ = &cerr;
214 arg_value_ch_a_a_ = v;
215 argument_count_i_ = c;
217 argument_index_i_ = 0;
219 // reached end of option table?
221 for (int i = 0; option_a_[i].longname ||option_a_[i].shortname; i++)
226 Getopt_long::ok() const
228 return array_index_i_ < argument_count_i_;
235 while (array_index_i_ < argument_count_i_
236 && !arg_value_ch_a_a_[array_index_i_][argument_index_i_])
239 argument_index_i_ = 0;
244 Getopt_long::current_arg()
246 if (array_index_i_ >= argument_count_i_)
248 char const * a = arg_value_ch_a_a_[array_index_i_];
249 return a + argument_index_i_;
253 Getopt_long::get_next_arg()
255 char const * a = current_arg();
259 argument_index_i_= 0;