poafloc

poafloc - Parser Of Arguments For Lines Of Commands
git clone git://git.dimitrijedobrota.com/poafloc.git
Log | Files | Refs | README | LICENSE

poafloc.h (2825B)


      1 #ifndef POAFLOC_POAFLOC_H
      2 #define POAFLOC_POAFLOC_H
      3 
      4 #ifdef __cplusplus
      5 
      6 #include <cstdio>
      7 
      8 #define MANGLE_ENUM(enumn, name) name
      9 #define ENUM_OPTION Option
     10 #define ENUM_KEY Key
     11 #define ENUM_HELP Help
     12 #define ENUM_PARSE Parse
     13 
     14 extern "C" {
     15 namespace poafloc {
     16 
     17 struct Parser;
     18 typedef Parser poafloc_parser_t;
     19 
     20 #else
     21 
     22 #include <stdio.h>
     23 
     24 #define MANGLE_ENUM(enumn, name) POAFLOC_##enumn##_##name
     25 #define ENUM_OPTION poafloc_option_e
     26 #define ENUM_KEY poafloc_key_e
     27 #define ENUM_HELP poafloc_help_e
     28 #define ENUM_PARSE poafloc_parse_e
     29 
     30 struct __Parser;
     31 typedef struct __Parser poafloc_parser_t;
     32 
     33 #endif
     34 
     35 typedef struct {
     36     char const *name;
     37     int key;
     38     char const *arg;
     39     int flags;
     40     char const *message;
     41     int group;
     42 } poafloc_option_t;
     43 
     44 typedef int (*poafloc_parse_f)(int key, const char *arg, poafloc_parser_t *parser);
     45 
     46 typedef struct {
     47     poafloc_option_t const *options;
     48     poafloc_parse_f parse;
     49     char const *doc;
     50     char const *message;
     51 } poafloc_arg_t;
     52 
     53 enum ENUM_OPTION {
     54     MANGLE_ENUM(OPTION, ARG_OPTIONAL) = 0x1,
     55     MANGLE_ENUM(OPTION, HIDDEN) = 0x2,
     56     MANGLE_ENUM(OPTION, ALIAS) = 0x4,
     57 };
     58 
     59 enum ENUM_KEY {
     60     MANGLE_ENUM(KEY, ARG) = 0,
     61     MANGLE_ENUM(KEY, END) = 0x1000001,
     62     MANGLE_ENUM(KEY, NO_ARGS) = 0x1000002,
     63     MANGLE_ENUM(KEY, INIT) = 0x1000003,
     64     MANGLE_ENUM(KEY, SUCCESS) = 0x1000004,
     65     MANGLE_ENUM(KEY, ERROR) = 0x1000005,
     66 };
     67 
     68 enum ENUM_HELP {
     69 
     70     MANGLE_ENUM(HELP, SHORT_USAGE) = 0x1,
     71     MANGLE_ENUM(HELP, USAGE) = 0x2,
     72     MANGLE_ENUM(HELP, SEE) = 0x4,
     73     MANGLE_ENUM(HELP, LONG) = 0x8,
     74 
     75     MANGLE_ENUM(HELP, EXIT_ERR) = 0x10,
     76     MANGLE_ENUM(HELP, EXIT_OK) = 0x20,
     77 
     78     MANGLE_ENUM(HELP, STD_ERR) = MANGLE_ENUM(HELP, SEE) |
     79                                  MANGLE_ENUM(HELP, EXIT_ERR),
     80 
     81     MANGLE_ENUM(HELP, STD_HELP) = MANGLE_ENUM(HELP, LONG) |
     82                                   MANGLE_ENUM(HELP, EXIT_OK),
     83 
     84     MANGLE_ENUM(HELP, STD_USAGE) = MANGLE_ENUM(HELP, USAGE) |
     85                                    MANGLE_ENUM(HELP, EXIT_ERR),
     86 };
     87 
     88 enum ENUM_PARSE {
     89     MANGLE_ENUM(PARSE, NO_ERRS) = 0x1,
     90     MANGLE_ENUM(PARSE, NO_HELP) = 0x2,
     91     MANGLE_ENUM(PARSE, NO_EXIT) = 0x4,
     92     MANGLE_ENUM(PARSE, SILENT) = 0x8,
     93     MANGLE_ENUM(PARSE, IN_ORDER) = 0x10,
     94 };
     95 
     96 #if !defined __cplusplus || defined WITH_C_BINDINGS
     97 
     98 void *poafloc_parser_input(poafloc_parser_t *parser);
     99 
    100 void poafloc_usage(poafloc_parser_t *parser);
    101 
    102 void poafloc_help(const poafloc_parser_t *state, FILE *stream, unsigned flags);
    103 
    104 int poafloc_parse(const poafloc_arg_t *argp, int argc, char *argv[], unsigned flags,
    105                void *input);
    106 
    107 void poafloc_failure(const poafloc_parser_t *parser, int status, int errnum,
    108                   const char *fmt, ...);
    109 
    110 #endif
    111 
    112 #undef MANGLE_ENUM
    113 #undef ENUM_OPTION
    114 #undef ENUM_KEY
    115 
    116 #ifdef __cplusplus
    117 } // namespace poafloc
    118 } // extern "C"
    119 #endif
    120 
    121 #endif