2 process command line, GNU style.
4 this is (Copyleft) 1996, Han-Wen Nienhuys, <hanwen@stack.nl>
15 if (sscanf(optarg
, "%ld", &l
) != 1)
22 Getopt_long::parselong()
24 char const *optnm
= argv
[optind
] + 2 ;
27 char *endopt
= strchr(optnm
, '=');
28 int searchlen
= (endopt
) ? endopt
- optnm
: strlen(optnm
);
31 for (int i
=0; i
< table_len
; i
++) {
32 char const *ln
= the_opts
[i
].longname
;
34 if (ln
&& !strncmp(ln
, optnm
, searchlen
)) {
41 report(E_UNKNOWNOPTION
);
50 optarg
= endopt
+1; // a '='
52 optarg
= argv
[optind
];
61 report(E_NOARGEXPECT
);
69 Long_option_init::printon(ostream
&errorout
)
72 errorout
<<"-" << shortname
;
73 if (shortname
&& longname
)
76 errorout
<< "`--" << longname
<< "'";
80 // report an error, GNU style.
82 Getopt_long::report(Errorcod c
)
88 *errorout
<< argv
[0] << ": ";
91 *errorout
<< "option ";
92 beet
->printon(*errorout
);
93 *errorout
<< "requires an argument"<<endl
;
96 *errorout
<< "option `--" <<
97 beet
->longname
<< "' does not allow an argument"<<endl
;
100 case E_UNKNOWNOPTION
:
101 *errorout
<< "unrecognized option ";
103 *errorout
<< "-" << argv
[optind
][optindind
] << endl
;
105 *errorout
<< argv
[optind
] << endl
;
109 *errorout
<< "illegal argument `" << optarg
<< "\'to option ";
110 beet
->printon(*errorout
);
119 Getopt_long::parseshort()
121 char c
=argv
[optind
][optindind
];
125 for (int i
=0; i
< table_len
; i
++)
126 if (the_opts
[i
].shortname
== c
) {
132 report(E_UNKNOWNOPTION
);
137 if (!beet
->take_arg
){
141 optarg
= argv
[optind
] + optindind
;
147 optarg
= argv
[optind
];
158 Getopt_long::operator()()
166 if (argv
[optind
][0] != '-')
169 if (argv
[optind
][1] == '-') {// what to do with "command -- bla"
177 Getopt_long::Getopt_long(int c
, char **v
, Long_option_init
*lo
)
186 // reached end of option table?
188 for (i
= 0; the_opts
[i
].longname
||the_opts
[i
].shortname
; i
++)
198 while (optind
< argc
&& !argv
[optind
][optindind
]) {
202 return (optind
< argc
);
206 Getopt_long::current_arg()
210 char * a
= argv
[optind
];
211 return a
+ optindind
;
215 Getopt_long::get_next_arg()
217 char * a
= current_arg();