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