poafloc

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

poafloc.hpp (2973B)


      1 #ifndef POAFLOC_POAFLOC_HPP
      2 #define POAFLOC_POAFLOC_HPP
      3 
      4 #include "poafloc.h"
      5 
      6 #include <cstdarg>
      7 #include <string>
      8 #include <unordered_map>
      9 #include <vector>
     10 
     11 namespace poafloc {
     12 
     13 using option_t = poafloc_option_t;
     14 using arg_t = poafloc_arg_t;
     15 
     16 int parse(const arg_t *argp, int argc, char *argv[], unsigned flags,
     17           void *input) noexcept;
     18 
     19 void usage(const Parser *parser);
     20 void help(const Parser *parser, FILE *stream, unsigned flags);
     21 
     22 void failure(const Parser *parser, int status, int errnum, const char *fmt,
     23              va_list args);
     24 
     25 void failure(const Parser *parser, int status, int errnum, const char *fmt,
     26              ...);
     27 
     28 class Parser {
     29   public:
     30     void *input() const { return m_input; }
     31     const char *name() const { return m_name; }
     32     unsigned flags() const { return m_flags; }
     33 
     34   private:
     35     friend int parse(const arg_t *, int, char **, unsigned, void *) noexcept;
     36     friend void help(const Parser *parser, FILE *stream, unsigned flags);
     37 
     38     Parser(const arg_t *argp, unsigned flags, void *input);
     39     Parser(const Parser &) = delete;
     40     Parser(Parser &&) = delete;
     41     Parser &operator=(const Parser &) = delete;
     42     Parser &operator=(Parser &&) = delete;
     43     ~Parser() noexcept = default;
     44 
     45     int parse(int argc, char *argv[]);
     46 
     47     int handle_unknown(bool shrt, const char *argv);
     48     int handle_missing(bool shrt, const char *argv);
     49     int handle_excess(const char *argv);
     50 
     51     void print_other_usages(FILE *stream) const;
     52     void help(FILE *stream) const;
     53     void usage(FILE *stream) const;
     54     void see(FILE *stream) const;
     55 
     56     static const char *basename(const char *name);
     57 
     58     struct help_entry_t {
     59         help_entry_t(const char *arg, const char *message, int group,
     60                      bool opt = false)
     61             : arg(arg), message(message), group(group), opt(opt) {}
     62 
     63         void push(char sh) { opt_short.push_back(sh); }
     64         void push(const char *lg) { opt_long.push_back(lg); }
     65 
     66         bool operator<(const help_entry_t &rhs) const;
     67 
     68         const char *arg;
     69         const char *message;
     70         int group;
     71         bool opt;
     72 
     73         std::vector<const char *> opt_long;
     74         std::vector<char> opt_short;
     75     };
     76 
     77     class trie_t {
     78       public:
     79         trie_t() = default;
     80         trie_t(const trie_t &) = delete;
     81         trie_t(trie_t &&) = delete;
     82         trie_t &operator=(const trie_t &) = delete;
     83         trie_t &operator=(trie_t &&) = delete;
     84         ~trie_t() noexcept;
     85 
     86         bool insert(const char *option, int key);
     87         int get(const char *option) const;
     88 
     89       private:
     90         static bool is_valid(const char *option);
     91 
     92         trie_t *children[26] = {0};
     93         int count = 0;
     94         int key = 0;
     95         bool terminal = false;
     96     };
     97 
     98     const arg_t *argp;
     99     unsigned m_flags;
    100     void *m_input;
    101 
    102     const char *m_name;
    103 
    104     std::unordered_map<int, const option_t *> options;
    105     std::vector<help_entry_t> help_entries;
    106     trie_t trie;
    107 };
    108 
    109 } // namespace poafloc
    110 
    111 #endif