Imported Upstream version 1.11
[platform/upstream/ed.git] / carg_parser.h
1 /*  Arg_parser - POSIX/GNU command line argument parser. (C version)
2     Copyright (C) 2006-2015 Antonio Diaz Diaz.
3
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.
8
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.
13
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/>.
16
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
25     Public License.
26 */
27
28 /*  Arg_parser reads the arguments in 'argv' and creates a number of
29     option codes, option arguments and non-option arguments.
30
31     In case of error, 'ap_error' returns a non-null pointer to an error
32     message.
33
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
37     long-only option.
38
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.
44
45     The argument '--' terminates all options; any following arguments are
46     treated as non-option arguments, even if they begin with a hyphen.
47
48     The syntax for optional option arguments is '-<short_option><argument>'
49     (without whitespace), or '--<long_option>=<argument>'.
50 */
51
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55
56 enum ap_Has_arg { ap_no, ap_yes, ap_maybe };
57
58 struct ap_Option
59   {
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;
63   };
64
65
66 struct ap_Record
67   {
68   int code;
69   char * argument;
70   };
71
72
73 struct Arg_parser
74   {
75   struct ap_Record * data;
76   char * error;
77   int data_size;
78   int error_size;
79   };
80
81
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 );
85
86 void ap_free( struct Arg_parser * const ap );
87
88 const char * ap_error( const struct Arg_parser * const ap );
89
90     /* The number of arguments parsed (may be different from argc) */
91 int ap_arguments( const struct Arg_parser * const ap );
92
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 );
96
97 const char * ap_argument( const struct Arg_parser * const ap, const int i );
98
99 #ifdef __cplusplus
100 }
101 #endif