diff options
author | Michael Brown <mcb30@etherboot.org> | 2006-12-07 03:54:57 +0000 |
---|---|---|
committer | Michael Brown <mcb30@etherboot.org> | 2006-12-07 03:54:57 +0000 |
commit | be0cd1cddd18a29264091fabc69bf2ec7a1f2cd2 (patch) | |
tree | bd696b964c70c46c08fd9dedf2db6ad352789431 /src/include/getopt.h | |
parent | 29db66fb51ff11bb3d03605bc38381185e07ce65 (diff) | |
download | ipxe-be0cd1cddd18a29264091fabc69bf2ec7a1f2cd2.tar.gz |
Added a functional version of getopt() and getopt_long(), ready for use
in our commands.
Diffstat (limited to 'src/include/getopt.h')
-rw-r--r-- | src/include/getopt.h | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/include/getopt.h b/src/include/getopt.h new file mode 100644 index 000000000..e9d3d6471 --- /dev/null +++ b/src/include/getopt.h @@ -0,0 +1,75 @@ +#ifndef _GETOPT_H +#define _GETOPT_H + +/** @file + * + * Parse command-line options + * + */ + +#include <stddef.h> + +enum getopt_argument_requirement { + /** Option does not take an argument */ + no_argument = 0, + /** Option requires an argument */ + required_argument = 1, + /** Option may have an argument */ + optional_argument = 2, +}; + +/** A long option, as used for getopt_long() */ +struct option { + /** Long name of this option */ + const char *name; + /** Option takes an argument + * + * Must be one of @c no_argument, @c required_argument, or @c + * optional_argument. + */ + int has_arg; + /** Location into which to store @c val, or NULL. + * + * See the description for @c val for more details. + */ + int *flag; + /** Value to return + * + * If @c flag is NULL, then this is the value that will be + * returned by getopt_long() when this option is found, and + * should therefore be set to the equivalent short option + * character. + * + * If @c flag is non-NULL, then this value will be written to + * the location pointed to by @flag, and getopt_long() will + * return 0. + */ + int val; +}; + +extern char *optarg; +extern int optind; +extern int optopt; + +extern int getopt_long ( int argc, char * const argv[], const char *optstring, + const struct option *longopts, int *longindex ); + +/** + * Parse command-line options + * + * @v argv Argument count + * @v argv Argument list + * @v optstring Option specification string + * @ret option Option found, or -1 for no more options + * + * See getopt_long() for full details. + */ +static inline int getopt ( int argc, char * const argv[], + const char *optstring ) { + static const struct option no_options[] = { + { NULL, 0, NULL, 0 } + }; + return getopt_long ( argc, argv, optstring, no_options, NULL ); +} + +#endif /* _GETOPT_H */ |