| 1 | /* gopt.h PUBILC DOMAIN 2015 [email protected] */ |
| 2 | |
| 3 | /* <http:///www.purposeful.co.uk/software/gopt> */ |
| 4 | |
| 5 | /* |
| 6 | I, Tom Vajzovic, am the author of this software and its documentation. |
| 7 | I permanently abandon all intellectual property rights in them, including |
| 8 | copyright, trademarks, design rights, database right, patents, and the right |
| 9 | to be identified as the author. |
| 10 | |
| 11 | I am fairly certain that the software does what the documentation says it |
| 12 | does, but I do not guarantee that it does, or that it does what you think it |
| 13 | should. I do not guarantee that it will not have undesirable side effects. |
| 14 | |
| 15 | You are free to use, modify and distribute this software as you please, but |
| 16 | you do so at your own risk. If you do not pass on this warning then you may |
| 17 | be responsible for any problems encountered by those who obtain the software |
| 18 | through you. |
| 19 | */ |
| 20 | |
| 21 | #ifndef GOPT_H_INCLUDED |
| 22 | #define GOPT_H_INCLUDED |
| 23 | |
| 24 | #ifdef __cplusplus |
| 25 | extern "C" { |
| 26 | #endif |
| 27 | |
| 28 | struct option |
| 29 | { |
| 30 | /* input: */ |
| 31 | char short_name; |
| 32 | const char *long_name; |
| 33 | |
| 34 | /* input/output: */ |
| 35 | unsigned int flags; |
| 36 | |
| 37 | /* output: */ |
| 38 | unsigned int count; |
| 39 | char *argument; |
| 40 | }; |
| 41 | |
| 42 | /* values for flags: */ |
| 43 | #define GOPT_ARGUMENT_OPTIONAL 0x000u /* option may or may not have an option-argument */ |
| 44 | #define GOPT_ARGUMENT_FORBIDDEN 0x001u /* option must not have an option-argument */ |
| 45 | #define GOPT_ARGUMENT_REQUIRED 0x002u /* option must have an option-argument */ |
| 46 | #define GOPT_ARGUMENT_NO_HYPHEN 0x004u /* option-argument may not start with hyphen/minus */ |
| 47 | #define GOPT_REPEATABLE 0x008u /* option may be specified more than once */ |
| 48 | #define GOPT_SEEN_SHORT_WITHOUT 0x010u /* short form of option was present without an option-argument in argv */ |
| 49 | #define GOPT_SEEN_SHORT_WITH 0x020u /* short form of option was present with an option-argument in argv */ |
| 50 | #define GOPT_SEEN_LONG_WITHOUT 0x040u /* long form of option was present without an option-argument in argv */ |
| 51 | #define GOPT_SEEN_LONG_WITH 0x080u /* long form of option was present with an option-argument in argv */ |
| 52 | #define GOPT_LAST 0x100u /* this is the last element of the array */ |
| 53 | |
| 54 | int gopt (char **argv, struct option *options); |
| 55 | /* |
| 56 | The function gopt() takes an argument vector argv, which has the same |
| 57 | form as the second argument to main(). It removes from the vector all |
| 58 | options and option-arguments, leaving only the program name, the |
| 59 | operands and a null pointer at the end. It returns the updated |
| 60 | argument count argc. It does not need to know the initial value of |
| 61 | argc because the input vector must be null-terminated. |
| 62 | |
| 63 | It also takes an array of struct option, the elements of which are |
| 64 | used for specifying the options which are to be recognized and for |
| 65 | returning details of which options were present in the argument |
| 66 | vector. |
| 67 | |
| 68 | The members of struct option are used as follows: |
| 69 | |
| 70 | On input, short_name is the single character that follows "-" to make |
| 71 | a short option, or zero if there is no short form of this option. |
| 72 | |
| 73 | On input, long_name is the name of the long option excluding the "--", |
| 74 | or NULL if there is no long form of this option. |
| 75 | |
| 76 | On return short_name and long_name are unaltered. |
| 77 | |
| 78 | count may be uninitialized on input. On return it is set to the |
| 79 | number of times the option was specified. If the option is not |
| 80 | present, this is zero. If an option has both a short and long name, |
| 81 | this is the sum of counts for both forms. |
| 82 | |
| 83 | argument may be uninitialized on input. On return it is set to the |
| 84 | last option-argument specified to this option, or NULL if no |
| 85 | option-argument was specified with the last occurrence of this |
| 86 | option, or the option was never specified. |
| 87 | |
| 88 | flags is the bitwise-or of certain constants. On input flags must |
| 89 | contain exactly one of: |
| 90 | GOPT_ARGUMENT_FORBIDDEN if the option does not take an option-argument |
| 91 | GOPT_ARGUMENT_REQUIRED if it always requires an option-argument |
| 92 | GOPT_ARGUMENT_OPTIONAL if it may or may not take an option-argument |
| 93 | |
| 94 | If flags contains GOPT_ARGUMENT_REQUIRED, it may also contain |
| 95 | GOPT_ARGUMENT_NO_HYPHEN. If the option argument is not allowed to |
| 96 | start with a '-', but the next member of the argument vector does |
| 97 | start with one where an option argument was expected, then this flag |
| 98 | causes it to be interpreted as another option, leaving the argument |
| 99 | to the preceding option NULL. The application can then detect the |
| 100 | NULL and give an error message like "missing option argument" which |
| 101 | may be preferable to treating the next program argument as an option |
| 102 | argument and then giving a less helpful message when the option |
| 103 | argument is found to be invalid. |
| 104 | |
| 105 | flags may also optionally contain GOPT_REPEATABLE. This is ignored |
| 106 | by the option parsing code, but may be used when checking the value |
| 107 | returned in count, where if this flag is specified then the |
| 108 | application will presumably report an error if count is greater than |
| 109 | one. |
| 110 | |
| 111 | On return, the initial value of flags is still present, and |
| 112 | additionally GOPT_SEEN_SHORT_WITH and/or GOPT_SEEN_SHORT_WITHOUT are |
| 113 | set if the short name was used at least once with or without an |
| 114 | option-argument, respectively, and GOPT_SEEN_LONG_WITH and/or |
| 115 | GOPT_SEEN_LONG_WITHOUT are set if the long name was used. |
| 116 | |
| 117 | On input there must be zero or more elements in the options array |
| 118 | initialized as above. These must be followed by one element where |
| 119 | flags is exactly GOPT_LAST. All other members of this element may be |
| 120 | uninitialized on input. On return the count member of this element |
| 121 | will be set to the total number of unrecognized options present. |
| 122 | The short_name member will be set to the first unrecognized short |
| 123 | option, or zero if there were none. The long_name will be set to |
| 124 | point to the start of the name of the first unrecognized long option, |
| 125 | or NULL if there were none. For completeness the argument member |
| 126 | will be set to the option argument of the last unrecognized option |
| 127 | (the same as with other options) but this is probably useless to the |
| 128 | application. |
| 129 | */ |
| 130 | |
| 131 | void gopt_errors (const char *argv0, const struct option *options); |
| 132 | /* |
| 133 | The function gopt_errors() examines the array of struct option |
| 134 | after it has been filled out by gopt(). If the options specified |
| 135 | were invalid it prints an error message starting with argv0 and |
| 136 | terminates the program. |
| 137 | */ |
| 138 | |
| 139 | #ifdef __cplusplus |
| 140 | } |
| 141 | #endif |
| 142 | |
| 143 | #endif /* GOPT_H_INCLUDED */ |