1 /* Arg_parser - POSIX/GNU command line argument parser. (C version)
2 Copyright (C) 2006-2015 Antonio Diaz Diaz.
4 This library is free software. Redistribution and use in source and
5 binary forms, with or without modification, are permitted provided
6 that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 /* Arg_parser reads the arguments in 'argv' and creates a number of
21 option codes, option arguments and non-option arguments.
23 In case of error, 'ap_error' returns a non-null pointer to an error
26 'options' is an array of 'struct ap_Option' terminated by an element
27 containing a code which is zero. A null name means a short-only
28 option. A code value outside the unsigned char range means a
31 Arg_parser normally makes it appear as if all the option arguments
32 were specified before all the non-option arguments for the purposes
33 of parsing, even if the user of your program intermixed option and
34 non-option arguments. If you want the arguments in the exact order
35 the user typed them, call 'ap_init' with 'in_order' = true.
37 The argument '--' terminates all options; any following arguments are
38 treated as non-option arguments, even if they begin with a hyphen.
40 The syntax for optional option arguments is '-<short_option><argument>'
41 (without whitespace), or '--<long_option>=<argument>'.
48 enum ap_Has_arg { ap_no, ap_yes, ap_maybe };
52 int code; /* Short option letter or code ( code != 0 ) */
53 const char * name; /* Long option name (maybe null) */
54 enum ap_Has_arg has_arg;
67 struct ap_Record * data;
74 char ap_init( struct Arg_parser * const ap,
75 const int argc, const char * const argv[],
76 const struct ap_Option options[], const char in_order );
78 void ap_free( struct Arg_parser * const ap );
80 const char * ap_error( const struct Arg_parser * const ap );
82 /* The number of arguments parsed (may be different from argc) */
83 int ap_arguments( const struct Arg_parser * const ap );
85 /* If ap_code( i ) is 0, ap_argument( i ) is a non-option.
86 Else ap_argument( i ) is the option's argument (or empty). */
87 int ap_code( const struct Arg_parser * const ap, const int i );
89 const char * ap_argument( const struct Arg_parser * const ap, const int i );