|
Packit |
4e1bf9 |
/* Arg_parser - POSIX/GNU command line argument parser. (C version)
|
|
Packit |
4e1bf9 |
Copyright (C) 2006-2017 Antonio Diaz Diaz.
|
|
Packit |
4e1bf9 |
|
|
Packit |
4e1bf9 |
This library is free software. Redistribution and use in source and
|
|
Packit |
4e1bf9 |
binary forms, with or without modification, are permitted provided
|
|
Packit |
4e1bf9 |
that the following conditions are met:
|
|
Packit |
4e1bf9 |
|
|
Packit |
4e1bf9 |
1. Redistributions of source code must retain the above copyright
|
|
Packit |
4e1bf9 |
notice, this list of conditions and the following disclaimer.
|
|
Packit |
4e1bf9 |
|
|
Packit |
4e1bf9 |
2. Redistributions in binary form must reproduce the above copyright
|
|
Packit |
4e1bf9 |
notice, this list of conditions and the following disclaimer in the
|
|
Packit |
4e1bf9 |
documentation and/or other materials provided with the distribution.
|
|
Packit |
4e1bf9 |
|
|
Packit |
4e1bf9 |
This library is distributed in the hope that it will be useful,
|
|
Packit |
4e1bf9 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
4e1bf9 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
Packit |
4e1bf9 |
*/
|
|
Packit |
4e1bf9 |
|
|
Packit |
4e1bf9 |
/* Arg_parser reads the arguments in 'argv' and creates a number of
|
|
Packit |
4e1bf9 |
option codes, option arguments and non-option arguments.
|
|
Packit |
4e1bf9 |
|
|
Packit |
4e1bf9 |
In case of error, 'ap_error' returns a non-null pointer to an error
|
|
Packit |
4e1bf9 |
message.
|
|
Packit |
4e1bf9 |
|
|
Packit |
4e1bf9 |
'options' is an array of 'struct ap_Option' terminated by an element
|
|
Packit |
4e1bf9 |
containing a code which is zero. A null name means a short-only
|
|
Packit |
4e1bf9 |
option. A code value outside the unsigned char range means a
|
|
Packit |
4e1bf9 |
long-only option.
|
|
Packit |
4e1bf9 |
|
|
Packit |
4e1bf9 |
Arg_parser normally makes it appear as if all the option arguments
|
|
Packit |
4e1bf9 |
were specified before all the non-option arguments for the purposes
|
|
Packit |
4e1bf9 |
of parsing, even if the user of your program intermixed option and
|
|
Packit |
4e1bf9 |
non-option arguments. If you want the arguments in the exact order
|
|
Packit |
4e1bf9 |
the user typed them, call 'ap_init' with 'in_order' = true.
|
|
Packit |
4e1bf9 |
|
|
Packit |
4e1bf9 |
The argument '--' terminates all options; any following arguments are
|
|
Packit |
4e1bf9 |
treated as non-option arguments, even if they begin with a hyphen.
|
|
Packit |
4e1bf9 |
|
|
Packit |
4e1bf9 |
The syntax for optional option arguments is '-<short_option><argument>'
|
|
Packit |
4e1bf9 |
(without whitespace), or '--<long_option>=<argument>'.
|
|
Packit |
4e1bf9 |
*/
|
|
Packit |
4e1bf9 |
|
|
Packit |
4e1bf9 |
#ifdef __cplusplus
|
|
Packit |
4e1bf9 |
extern "C" {
|
|
Packit |
4e1bf9 |
#endif
|
|
Packit |
4e1bf9 |
|
|
Packit |
4e1bf9 |
enum ap_Has_arg { ap_no, ap_yes, ap_maybe };
|
|
Packit |
4e1bf9 |
|
|
Packit |
4e1bf9 |
struct ap_Option
|
|
Packit |
4e1bf9 |
{
|
|
Packit |
4e1bf9 |
int code; /* Short option letter or code ( code != 0 ) */
|
|
Packit |
4e1bf9 |
const char * name; /* Long option name (maybe null) */
|
|
Packit |
4e1bf9 |
enum ap_Has_arg has_arg;
|
|
Packit |
4e1bf9 |
};
|
|
Packit |
4e1bf9 |
|
|
Packit |
4e1bf9 |
|
|
Packit |
4e1bf9 |
struct ap_Record
|
|
Packit |
4e1bf9 |
{
|
|
Packit |
4e1bf9 |
int code;
|
|
Packit |
4e1bf9 |
char * argument;
|
|
Packit |
4e1bf9 |
};
|
|
Packit |
4e1bf9 |
|
|
Packit |
4e1bf9 |
|
|
Packit |
4e1bf9 |
struct Arg_parser
|
|
Packit |
4e1bf9 |
{
|
|
Packit |
4e1bf9 |
struct ap_Record * data;
|
|
Packit |
4e1bf9 |
char * error;
|
|
Packit |
4e1bf9 |
int data_size;
|
|
Packit |
4e1bf9 |
int error_size;
|
|
Packit |
4e1bf9 |
};
|
|
Packit |
4e1bf9 |
|
|
Packit |
4e1bf9 |
|
|
Packit |
4e1bf9 |
char ap_init( struct Arg_parser * const ap,
|
|
Packit |
4e1bf9 |
const int argc, const char * const argv[],
|
|
Packit |
4e1bf9 |
const struct ap_Option options[], const char in_order );
|
|
Packit |
4e1bf9 |
|
|
Packit |
4e1bf9 |
void ap_free( struct Arg_parser * const ap );
|
|
Packit |
4e1bf9 |
|
|
Packit |
4e1bf9 |
const char * ap_error( const struct Arg_parser * const ap );
|
|
Packit |
4e1bf9 |
|
|
Packit |
4e1bf9 |
/* The number of arguments parsed (may be different from argc) */
|
|
Packit |
4e1bf9 |
int ap_arguments( const struct Arg_parser * const ap );
|
|
Packit |
4e1bf9 |
|
|
Packit |
4e1bf9 |
/* If ap_code( i ) is 0, ap_argument( i ) is a non-option.
|
|
Packit |
4e1bf9 |
Else ap_argument( i ) is the option's argument (or empty). */
|
|
Packit |
4e1bf9 |
int ap_code( const struct Arg_parser * const ap, const int i );
|
|
Packit |
4e1bf9 |
|
|
Packit |
4e1bf9 |
const char * ap_argument( const struct Arg_parser * const ap, const int i );
|
|
Packit |
4e1bf9 |
|
|
Packit |
4e1bf9 |
#ifdef __cplusplus
|
|
Packit |
4e1bf9 |
}
|
|
Packit |
4e1bf9 |
#endif
|