kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

parameters_parser.hh (4449B)


      1 #ifndef parameters_parser_hh_INCLUDED
      2 #define parameters_parser_hh_INCLUDED
      3 
      4 #include "exception.hh"
      5 #include "hash_map.hh"
      6 #include "meta.hh"
      7 #include "array_view.hh"
      8 #include "optional.hh"
      9 #include "string.hh"
     10 #include "format.hh"
     11 #include "function.hh"
     12 
     13 namespace Kakoune
     14 {
     15 
     16 using ParameterList = ConstArrayView<String>;
     17 
     18 struct parameter_error : public runtime_error
     19 {
     20     using runtime_error::runtime_error;
     21 };
     22 
     23 struct unknown_option : public parameter_error
     24 {
     25     unknown_option(StringView name)
     26         : parameter_error(format("unknown option '{}'", name)) {}
     27 };
     28 
     29 struct missing_option_value: public parameter_error
     30 {
     31     missing_option_value(StringView name)
     32         : parameter_error(format("missing value for option '{}'", name)) {}
     33 };
     34 
     35 struct wrong_argument_count : public parameter_error
     36 {
     37     wrong_argument_count() : parameter_error("wrong argument count") {}
     38 };
     39 
     40 class Context;
     41 struct Completions;
     42 using ArgCompleter = Function<Completions (const Context&, StringView, ByteCount)>;
     43 
     44 struct SwitchDesc
     45 {
     46     Optional<ArgCompleter> arg_completer;
     47     String description;
     48 };
     49 
     50 using SwitchMap = HashMap<String, SwitchDesc, MemoryDomain::Commands>;
     51 
     52 String generate_switches_doc(const SwitchMap& opts);
     53 
     54 struct ParameterDesc
     55 {
     56     enum class Flags
     57     {
     58         None = 0,
     59         SwitchesOnlyAtStart   = 0b0001,
     60         SwitchesAsPositional  = 0b0010,
     61         IgnoreUnknownSwitches = 0b0100
     62     };
     63     friend constexpr bool with_bit_ops(Meta::Type<Flags>) { return true; }
     64 
     65     SwitchMap switches;
     66     Flags flags = Flags::None;
     67     size_t min_positionals = 0;
     68     size_t max_positionals = -1;
     69 };
     70 
     71 // ParametersParser provides tools to parse command parameters.
     72 // There are 3 types of parameters:
     73 //  * unnamed options, which are accessed by position (ignoring named ones)
     74 //  * named boolean options, which are enabled using '-name' syntax
     75 //  * named string options,  which are defined using '-name value' syntax
     76 struct ParametersParser
     77 {
     78     // the options defines named options, if they map to true, then
     79     // they are understood as string options, else they are understood as
     80     // boolean option.
     81     ParametersParser(ParameterList params, const ParameterDesc& desc, bool ignore_errors = false);
     82 
     83     enum class State {
     84         Switch,
     85         SwitchArgument,
     86         Positional,
     87     };
     88 
     89     // Return a valid optional if the switch was given, with
     90     // a non empty StringView value if the switch took an argument.
     91     Optional<StringView> get_switch(StringView name) const;
     92 
     93     struct iterator
     94     {
     95         using difference_type = ptrdiff_t;
     96         using value_type = String;
     97         using pointer = String*;
     98         using reference = String&;
     99         using iterator_category = std::forward_iterator_tag;
    100 
    101         iterator(const ParametersParser& parser, size_t index)
    102             : m_parser(parser), m_index(index) {}
    103 
    104         const String& operator*() const { return m_parser[m_index]; }
    105         const String* operator->() const { return &m_parser[m_index]; }
    106 
    107         iterator& operator++() { ++m_index; return *this; }
    108         iterator operator++(int) { auto copy = *this; ++m_index; return copy; }
    109 
    110         bool operator==(const iterator& other) const
    111         {
    112             kak_assert(&m_parser == &other.m_parser);
    113             return m_index == other.m_index;
    114         }
    115 
    116     private:
    117         const ParametersParser& m_parser;
    118         size_t                  m_index;
    119     };
    120 
    121     // positional parameters count
    122     size_t positional_count() const { return m_positional_indices.size(); }
    123 
    124     // access positional parameter by index
    125     const String& operator[] (size_t index) const
    126     {
    127         kak_assert(index < positional_count());
    128         return m_params[m_positional_indices[index]];
    129     }
    130 
    131     ConstArrayView<String> positionals_from(size_t first) const
    132     {
    133         // kak_assert(m_desc.flags & (ParameterDesc::Flags::SwitchesOnlyAtStart | ParameterDesc::Flags::SwitchesAsPositional));
    134         return m_params.subrange(first < m_positional_indices.size() ? m_positional_indices[first] : -1);
    135     }
    136 
    137     iterator begin() const { return iterator(*this, 0); }
    138     iterator end() const { return iterator(*this, m_positional_indices.size()); }
    139 
    140     State state() const { return *m_state; }
    141 
    142 private:
    143     ParameterList m_params;
    144     Vector<size_t, MemoryDomain::Commands> m_positional_indices;
    145     HashMap<String, StringView> m_switches;
    146     Optional<State> m_state;
    147 };
    148 
    149 }
    150 
    151 #endif // parameters_parser_hh_INCLUDED