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)
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++) {
33 char const *ln = option_a_[i].longname;
35 if (ln && !strncmp(ln, optnm, searchlen)) {
36 found_option_l_ = option_a_+i;
41 if (!found_option_l_) {
42 report(E_UNKNOWNOPTION);
46 argument_index_i_ = 0;
49 if (found_option_l_->take_arg) {
51 optional_argument_ch_C_ = endopt +1; // a '='
53 optional_argument_ch_C_ = arg_value_ch_a_a_[array_index_i_];
56 if (!optional_argument_ch_C_)
60 optional_argument_ch_C_ = 0;
62 report(E_NOARGEXPECT);
65 return found_option_l_;
70 Long_option_init::printon(ostream &errorout)const
73 errorout <<"-" << shortname;
74 if (shortname && longname)
77 errorout << "`--" << longname << "'";
80 // report an error, GNU style.
82 Getopt_long::report(Errorcod c)
85 if (!error_ostream_l_)
88 *error_ostream_l_ << arg_value_ch_a_a_[0] << ": ";
91 *error_ostream_l_<< "option ";
92 found_option_l_->printon(*error_ostream_l_);
93 *error_ostream_l_ << "requires an argument"<<endl;
96 *error_ostream_l_ << "option `--" <<
97 found_option_l_->longname << "' does not allow an argument"<<endl;
100 case E_UNKNOWNOPTION:
101 *error_ostream_l_ << "unrecognized option ";
102 if (argument_index_i_)
103 *error_ostream_l_ << "-" << arg_value_ch_a_a_[array_index_i_][argument_index_i_] << endl;
105 *error_ostream_l_ << arg_value_ch_a_a_[array_index_i_] << endl;
109 *error_ostream_l_ << "illegal argument `" << optional_argument_ch_C_ << "\'to option ";
110 found_option_l_->printon(*error_ostream_l_);
111 *error_ostream_l_ << '\n';
118 const Long_option_init *
119 Getopt_long::parseshort()
121 char c=arg_value_ch_a_a_[array_index_i_][argument_index_i_];
125 for (int i=0; i < table_len_i_; i++)
126 if (option_a_[i].shortname == c) {
127 found_option_l_ = option_a_+i;
131 if (!found_option_l_){
132 report(E_UNKNOWNOPTION);
137 if (!found_option_l_->take_arg){
138 optional_argument_ch_C_ = 0;
139 return found_option_l_;
141 optional_argument_ch_C_ = arg_value_ch_a_a_[array_index_i_] + argument_index_i_;
144 argument_index_i_ = 0;
146 if (!optional_argument_ch_C_[0]) {
147 optional_argument_ch_C_ = arg_value_ch_a_a_[array_index_i_];
150 if (!optional_argument_ch_C_) {
154 return found_option_l_;
157 const Long_option_init *
158 Getopt_long::operator()()
165 if (argument_index_i_)
168 const char * argument_C = arg_value_ch_a_a_[array_index_i_];
170 if (argument_C[0] != '-')
173 if (argument_C[1] == '-') {// what to do with "command -- bla"
179 if (argument_C[ 1 ]) {
180 argument_index_i_ = 1;
188 Getopt_long::Getopt_long(int c, char **v, Long_option_init *lo)
191 error_ostream_l_ = &cerr;
192 arg_value_ch_a_a_ = v;
193 argument_count_i_ = c;
195 argument_index_i_ = 0;
197 // reached end of option table?
199 for (int i = 0; option_a_[i].longname ||option_a_[i].shortname; i++)
204 Getopt_long::ok()const
206 return array_index_i_ < argument_count_i_;
213 while (array_index_i_ < argument_count_i_
214 && !arg_value_ch_a_a_[array_index_i_][argument_index_i_]) {
216 argument_index_i_ = 0;
221 Getopt_long::current_arg()
223 if (array_index_i_ >= argument_count_i_)
225 char const * a = arg_value_ch_a_a_[array_index_i_];
226 return a + argument_index_i_;
230 Getopt_long::get_next_arg()
232 char const * a = current_arg();
235 argument_index_i_= 0;