2 process command line, GNU style.
5 this is (Copyleft) 1996, Han-Wen Nienhuys, <hanwen@stack.nl>
13 Getopt_long::intarg() {
15 if (sscanf(optarg
, "%ld", &l
) != 1)
21 Getopt_long::parselong() {
22 const char *optnm
= argv
[optind
] + 2 ;
25 char *endopt
= strchr(optnm
, '=');
26 int searchlen
= (endopt
) ? endopt
- optnm
: strlen(optnm
);
29 for (int i
=0; i
< table_len
; i
++) {
30 const char *ln
= the_opts
[i
].longname
;
32 if (ln
&& !strncmp(ln
, optnm
, searchlen
)) {
39 report(E_UNKNOWNOPTION
);
48 optarg
= endopt
+1; // a '='
50 optarg
= argv
[optind
];
59 report(E_NOARGEXPECT
);
67 long_option_init::printon(ostream
&errorout
)
70 errorout
<<"-" << shortname
;
71 if (shortname
&& longname
)
74 errorout
<< "`--" << longname
<< "'";
78 // report an error, GNU style.
80 Getopt_long::report(Errorcod c
)
86 *errorout
<< argv
[0] << ": ";
89 *errorout
<< "option ";
90 beet
->printon(*errorout
);
91 *errorout
<< "requires an argument"<<endl
;
94 *errorout
<< "option `--" << beet
->longname
<< "' does not allow an argument"<<endl
;
98 *errorout
<< "unrecognized option ";
100 *errorout
<< "-" << argv
[optind
][optindind
] << endl
;
102 *errorout
<< argv
[optind
] << endl
;
106 *errorout
<< "illegal argument `" << optarg
<< "\'to option ";
107 beet
->printon(*errorout
);
116 Getopt_long::parseshort() {
117 char c
=argv
[optind
][optindind
];
121 for (int i
=0; i
< table_len
; i
++)
122 if (the_opts
[i
].shortname
== c
) {
128 report(E_UNKNOWNOPTION
);
133 if (!beet
->take_arg
){
137 optarg
= argv
[optind
] + optindind
;
143 optarg
= argv
[optind
];
154 Getopt_long::operator()() {
161 if (argv
[optind
][0] != '-')
164 if (argv
[optind
][1] == '-') {// what to do with "command -- bla"
172 Getopt_long::Getopt_long(int c
, char **v
, long_option_init
*lo
) {
180 // reached end of option table?
182 for (i
= 0; the_opts
[i
].longname
||the_opts
[i
].shortname
; i
++)
187 bool Getopt_long::next() {
189 while (optind
< argc
&& !argv
[optind
][optindind
]) {
193 return (optind
< argc
);
197 Getopt_long::current_arg()
201 char * a
= argv
[optind
];
202 return a
+ optindind
;
206 Getopt_long::get_next_arg()
208 char * a
= current_arg();