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: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this library. If not, see <http://www.gnu.org/licenses/>.
17 As a special exception, you may use this file as part of a free
18 software library without restriction. Specifically, if other files
19 instantiate templates or use macros or inline functions from this
20 file, or you compile this file and link it with other files to
21 produce an executable, this file does not by itself cause the
22 resulting executable to be covered by the GNU General Public
23 License. This exception does not however invalidate any other
24 reasons why the executable file might be covered by the GNU General
28 /* Arg_parser reads the arguments in 'argv' and creates a number of
29 option codes, option arguments and non-option arguments.
31 In case of error, 'ap_error' returns a non-null pointer to an error
34 'options' is an array of 'struct ap_Option' terminated by an element
35 containing a code which is zero. A null name means a short-only
36 option. A code value outside the unsigned char range means a
39 Arg_parser normally makes it appear as if all the option arguments
40 were specified before all the non-option arguments for the purposes
41 of parsing, even if the user of your program intermixed option and
42 non-option arguments. If you want the arguments in the exact order
43 the user typed them, call 'ap_init' with 'in_order' = true.
45 The argument '--' terminates all options; any following arguments are
46 treated as non-option arguments, even if they begin with a hyphen.
48 The syntax for optional option arguments is '-<short_option><argument>'
49 (without whitespace), or '--<long_option>=<argument>'.
56 enum ap_Has_arg { ap_no, ap_yes, ap_maybe };
60 int code; /* Short option letter or code ( code != 0 ) */
61 const char * name; /* Long option name (maybe null) */
62 enum ap_Has_arg has_arg;
75 struct ap_Record * data;
82 char ap_init( struct Arg_parser * const ap,
83 const int argc, const char * const argv[],
84 const struct ap_Option options[], const char in_order );
86 void ap_free( struct Arg_parser * const ap );
88 const char * ap_error( const struct Arg_parser * const ap );
90 /* The number of arguments parsed (may be different from argc) */
91 int ap_arguments( const struct Arg_parser * const ap );
93 /* If ap_code( i ) is 0, ap_argument( i ) is a non-option.
94 Else ap_argument( i ) is the option's argument (or empty). */
95 int ap_code( const struct Arg_parser * const ap, const int i );
97 const char * ap_argument( const struct Arg_parser * const ap, const int i );