2 process command line, GNU style.
4 this is (Copyleft) 1996, Han-Wen Nienhuys, <hanwen@stack.nl>
12 Getopt_long::argument_to_i()
15 if (!optional_argument_ch_C_
16 || sscanf (optional_argument_ch_C_, "%ld", &l) != 1)
17 report (E_ILLEGALARG);
22 const Long_option_init *
23 Getopt_long::parselong()
25 char const *optnm = arg_value_ch_a_a_[array_index_i_] + 2 ;
28 char const *endopt = strchr (optnm, '=');
29 int searchlen = (endopt) ? endopt - optnm : strlen (optnm);
32 for (int i=0; i< table_len_i_; i++)
34 char const *ln = option_a_[i].longname;
36 if (ln && !strncmp (ln, optnm, searchlen))
38 found_option_l_ = option_a_+i;
45 report (E_UNKNOWNOPTION);
49 argument_index_i_ = 0;
52 if (found_option_l_->take_arg)
55 optional_argument_ch_C_ = endopt +1; // a '='
58 optional_argument_ch_C_ = arg_value_ch_a_a_[array_index_i_];
61 if (!optional_argument_ch_C_)
67 optional_argument_ch_C_ = 0;
69 report (E_NOARGEXPECT);
72 return found_option_l_;
77 Long_option_init::printon (ostream &errorout)const
80 errorout <<"-" << shortname;
81 if (shortname && longname)
84 errorout << "`--" << longname << "'";
87 // report an error, GNU style.
89 Getopt_long::report (Errorcod c)
92 if (!error_ostream_l_)
95 *error_ostream_l_ << arg_value_ch_a_a_[0] << ": ";
99 *error_ostream_l_<< "option ";
100 found_option_l_->printon (*error_ostream_l_);
101 *error_ostream_l_ << "requires an argument"<<endl;
104 *error_ostream_l_ << "option `--" <<
105 found_option_l_->longname << "' does not allow an argument"<<endl;
108 case E_UNKNOWNOPTION:
109 *error_ostream_l_ << "unrecognized option ";
110 if (argument_index_i_)
111 *error_ostream_l_ << "-" << arg_value_ch_a_a_[array_index_i_][argument_index_i_] << endl;
113 *error_ostream_l_ << arg_value_ch_a_a_[array_index_i_] << endl;
117 *error_ostream_l_ << "illegal argument `" << optional_argument_ch_C_ << "\'to option ";
118 found_option_l_->printon (*error_ostream_l_);
119 *error_ostream_l_ << '\n';
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()()
178 if (argument_index_i_)
181 const char * argument_C = arg_value_ch_a_a_[array_index_i_];
183 if (argument_C[0] != '-')
186 if (argument_C[1] == '-') {// what to do with "command -- bla"
196 argument_index_i_ = 1;
206 Getopt_long::Getopt_long (int c, char **v, Long_option_init *lo)
209 error_ostream_l_ = &cerr;
210 arg_value_ch_a_a_ = v;
211 argument_count_i_ = c;
213 argument_index_i_ = 0;
215 // reached end of option table?
217 for (int i = 0; option_a_[i].longname ||option_a_[i].shortname; i++)
222 Getopt_long::ok()const
224 return array_index_i_ < argument_count_i_;
231 while (array_index_i_ < argument_count_i_
232 && !arg_value_ch_a_a_[array_index_i_][argument_index_i_])
235 argument_index_i_ = 0;
240 Getopt_long::current_arg()
242 if (array_index_i_ >= argument_count_i_)
244 char const * a = arg_value_ch_a_a_[array_index_i_];
245 return a + argument_index_i_;
249 Getopt_long::get_next_arg()
251 char const * a = current_arg();
255 argument_index_i_= 0;