Mon, 28 Aug 2000 13:15:36 +0200
trunk: changeset 25
Parser error handling fixes
| 5 | 1 | /* |
| 2 | * libtu/optparser.h | |
| 3 | * | |
| 9 | 4 | * Copyright (c) Tuomo Valkonen 1999-2000. |
| 5 | 5 | * See the included file LICENSE for details. |
| 6 | */ | |
| 7 | ||
| 8 | 8 | #ifndef LIBTU_OPTPARSER_H |
| 9 | #define LIBTU_OPTPARSER_H | |
| 5 | 10 | |
| 11 | #include "types.h" | |
| 12 | ||
| 13 | ||
| 12 | 14 | #define OPT_ID_NOSHORT_FLAG 0x10000 |
| 15 | #define OPT_ID_RESERVED_FLAG 0x20000 | |
| 16 | ||
| 17 | #define OPT_ID(X) ((X)|OPT_ID_NOSHORT_FLAG) | |
| 18 | #define OPT_ID_RESERVED(X) ((X)|OPT_ID_RESERVED_FLAG) | |
| 5 | 19 | |
| 20 | /* OPTP_CHAIN is the normal behavior, i.e. single-letter options can be | |
| 21 | * "chained" together: 'lr -lR'. Use for normal command line programs. | |
| 22 | * OPTP_MIDLONG allows '-display foo' -like args but disables chaining | |
| 23 | * of single-letter options. X programs should probably use this. | |
| 24 | * OPTP_IMMEDIATE allows immediate arguments (-I/usr/include) (and disables | |
| 25 | * chaining and midlong options). | |
| 26 | * OPTP_NO_DASH is the same as OPTP_CHAIN but allows the dash to be omitted | |
| 27 | * for 'tar xzf foo' -like behavior. | |
| 28 | * Long '--foo=bar' options are supported in all of the modes. | |
| 29 | */ | |
| 30 | ||
| 31 | enum{ | |
| 32 | OPTP_CHAIN=0, | |
| 12 | 33 | OPTP_DEFAULT=0, |
| 5 | 34 | OPTP_MIDLONG=1, |
| 35 | OPTP_IMMEDIATE=2, | |
| 36 | OPTP_NO_DASH=3 | |
| 37 | }; | |
| 38 | ||
| 39 | enum{ | |
| 40 | OPT_ARG=1, /* option has an argument */ | |
| 41 | OPT_OPT_ARG=3 /* option may have an argument */ | |
| 42 | }; | |
| 43 | ||
| 44 | ||
| 12 | 45 | typedef struct _OptParserOpt{ |
| 5 | 46 | int optid; |
| 47 | const char *longopt; | |
| 48 | int flags; | |
| 12 | 49 | const char *argname; |
| 50 | const char *descr; | |
| 5 | 51 | } OptParserOpt; |
| 52 | ||
| 53 | ||
| 12 | 54 | typedef struct _OptParserCommonInfo{ |
| 55 | const char *version; | |
| 56 | const char *usage_tmpl; | |
| 57 | const char *about; | |
| 58 | } OptParserCommonInfo; | |
| 59 | ||
| 60 | ||
| 5 | 61 | enum{ |
| 62 | OPT_ID_END=0, | |
| 63 | OPT_ID_ARGUMENT=1, | |
| 64 | ||
| 65 | E_OPT_INVALID_OPTION=-1, | |
| 66 | E_OPT_INVALID_CHAIN_OPTION=-2, | |
| 67 | E_OPT_SYNTAX_ERROR=-3, | |
| 68 | E_OPT_MISSING_ARGUMENT=-4, | |
| 69 | E_OPT_UNEXPECTED_ARGUMENT=-5 | |
| 70 | }; | |
| 71 | ||
| 72 | ||
| 73 | extern void optparser_init(int argc, char *const argv[], int mode, | |
| 12 | 74 | const OptParserOpt *opts, |
| 75 | const OptParserCommonInfo *cinfo); | |
| 76 | ||
| 5 | 77 | extern int optparser_get_opt(); |
| 78 | extern const char* optparser_get_arg(); | |
| 79 | extern void optparser_print_error(); | |
| 80 | ||
| 8 | 81 | #endif /* LIBTU_OPTPARSER_H */ |