@@ -250,63 +250,78 @@ void parse_options_start(struct parse_opt_ctx_t *ctx,
250
250
ctx -> flags = flags ;
251
251
}
252
252
253
- int parse_options_end (struct parse_opt_ctx_t * ctx )
254
- {
255
- memmove (ctx -> out + ctx -> cpidx , ctx -> argv , ctx -> argc * sizeof (* ctx -> out ));
256
- ctx -> out [ctx -> cpidx + ctx -> argc ] = NULL ;
257
- return ctx -> cpidx + ctx -> argc ;
258
- }
259
-
260
253
static int usage_with_options_internal (const char * const * ,
261
- const struct option * , int , int );
254
+ const struct option * , int );
262
255
263
- int parse_options (int argc , const char * * argv , const struct option * options ,
264
- const char * const usagestr [], int flags )
256
+ int parse_options_step (struct parse_opt_ctx_t * ctx ,
257
+ const struct option * options ,
258
+ const char * const usagestr [])
265
259
{
266
- struct parse_opt_ctx_t ctx ;
267
-
268
- parse_options_start (& ctx , argc , argv , flags );
269
- for (; ctx .argc ; ctx .argc -- , ctx .argv ++ ) {
270
- const char * arg = ctx .argv [0 ];
260
+ for (; ctx -> argc ; ctx -> argc -- , ctx -> argv ++ ) {
261
+ const char * arg = ctx -> argv [0 ];
271
262
272
263
if (* arg != '-' || !arg [1 ]) {
273
- if (ctx . flags & PARSE_OPT_STOP_AT_NON_OPTION )
264
+ if (ctx -> flags & PARSE_OPT_STOP_AT_NON_OPTION )
274
265
break ;
275
- ctx . out [ctx . cpidx ++ ] = ctx . argv [0 ];
266
+ ctx -> out [ctx -> cpidx ++ ] = ctx -> argv [0 ];
276
267
continue ;
277
268
}
278
269
279
270
if (arg [1 ] != '-' ) {
280
- ctx . opt = arg + 1 ;
281
- if (* ctx . opt == 'h' )
282
- usage_with_options (usagestr , options );
283
- if (parse_short_opt (& ctx , options ) < 0 )
271
+ ctx -> opt = arg + 1 ;
272
+ if (* ctx -> opt == 'h' )
273
+ return parse_options_usage (usagestr , options );
274
+ if (parse_short_opt (ctx , options ) < 0 )
284
275
usage_with_options (usagestr , options );
285
- if (ctx . opt )
276
+ if (ctx -> opt )
286
277
check_typos (arg + 1 , options );
287
- while (ctx . opt ) {
288
- if (* ctx . opt == 'h' )
289
- usage_with_options (usagestr , options );
290
- if (parse_short_opt (& ctx , options ) < 0 )
278
+ while (ctx -> opt ) {
279
+ if (* ctx -> opt == 'h' )
280
+ return parse_options_usage (usagestr , options );
281
+ if (parse_short_opt (ctx , options ) < 0 )
291
282
usage_with_options (usagestr , options );
292
283
}
293
284
continue ;
294
285
}
295
286
296
287
if (!arg [2 ]) { /* "--" */
297
- if (!(ctx . flags & PARSE_OPT_KEEP_DASHDASH )) {
298
- ctx . argc -- ;
299
- ctx . argv ++ ;
288
+ if (!(ctx -> flags & PARSE_OPT_KEEP_DASHDASH )) {
289
+ ctx -> argc -- ;
290
+ ctx -> argv ++ ;
300
291
}
301
292
break ;
302
293
}
303
294
304
295
if (!strcmp (arg + 2 , "help-all" ))
305
- usage_with_options_internal (usagestr , options , 1 , 1 );
296
+ return usage_with_options_internal (usagestr , options , 1 );
306
297
if (!strcmp (arg + 2 , "help" ))
298
+ return parse_options_usage (usagestr , options );
299
+ if (parse_long_opt (ctx , arg + 2 , options ))
307
300
usage_with_options (usagestr , options );
308
- if (parse_long_opt (& ctx , arg + 2 , options ))
309
- usage_with_options (usagestr , options );
301
+ }
302
+ return PARSE_OPT_DONE ;
303
+ }
304
+
305
+ int parse_options_end (struct parse_opt_ctx_t * ctx )
306
+ {
307
+ memmove (ctx -> out + ctx -> cpidx , ctx -> argv , ctx -> argc * sizeof (* ctx -> out ));
308
+ ctx -> out [ctx -> cpidx + ctx -> argc ] = NULL ;
309
+ return ctx -> cpidx + ctx -> argc ;
310
+ }
311
+
312
+ int parse_options (int argc , const char * * argv , const struct option * options ,
313
+ const char * const usagestr [], int flags )
314
+ {
315
+ struct parse_opt_ctx_t ctx ;
316
+
317
+ parse_options_start (& ctx , argc , argv , flags );
318
+ switch (parse_options_step (& ctx , options , usagestr )) {
319
+ case PARSE_OPT_HELP :
320
+ exit (129 );
321
+ case PARSE_OPT_DONE :
322
+ break ;
323
+ default : /* PARSE_OPT_UNKNOWN */
324
+ abort (); /* unreached yet */
310
325
}
311
326
312
327
return parse_options_end (& ctx );
@@ -316,7 +331,7 @@ int parse_options(int argc, const char **argv, const struct option *options,
316
331
#define USAGE_GAP 2
317
332
318
333
int usage_with_options_internal (const char * const * usagestr ,
319
- const struct option * opts , int full , int do_exit )
334
+ const struct option * opts , int full )
320
335
{
321
336
fprintf (stderr , "usage: %s\n" , * usagestr ++ );
322
337
while (* usagestr && * * usagestr )
@@ -401,22 +416,20 @@ int usage_with_options_internal(const char * const *usagestr,
401
416
}
402
417
fputc ('\n' , stderr );
403
418
404
- if (do_exit )
405
- exit (129 );
406
419
return PARSE_OPT_HELP ;
407
420
}
408
421
409
422
void usage_with_options (const char * const * usagestr ,
410
- const struct option * opts )
423
+ const struct option * opts )
411
424
{
412
- usage_with_options_internal (usagestr , opts , 0 , 1 );
413
- exit (129 ); /* make gcc happy */
425
+ usage_with_options_internal (usagestr , opts , 0 );
426
+ exit (129 );
414
427
}
415
428
416
429
int parse_options_usage (const char * const * usagestr ,
417
430
const struct option * opts )
418
431
{
419
- return usage_with_options_internal (usagestr , opts , 0 , 0 );
432
+ return usage_with_options_internal (usagestr , opts , 0 );
420
433
}
421
434
422
435
0 commit comments