File: command_line.hpp

package info (click to toggle)
polybar 3.7.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,108 kB
  • sloc: cpp: 30,424; python: 3,750; sh: 284; makefile: 83
file content (78 lines) | stat: -rw-r--r-- 1,982 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#pragma once

#include <map>

#include "common.hpp"
#include "errors.hpp"

POLYBAR_NS

namespace command_line {
  DEFINE_ERROR(argument_error);
  DEFINE_ERROR(value_error);

  class option;
  using choices = vector<string>;
  using options = vector<option>;
  using values = std::map<string, string>;
  using posargs = vector<string>;

  // class definition : option {{{

  class option {
   public:
    string flag;
    string flag_long;
    string desc;
    string token;
    const choices values;

    explicit option(string&& flag, string&& flag_long, string&& desc, string&& token = "", const choices&& c = {})
        : flag(forward<string>(flag))
        , flag_long(forward<string>(flag_long))
        , desc(forward<string>(desc))
        , token(forward<string>(token))
        , values(forward<const choices>(c)) {}
  };

  // }}}
  // class definition : parser {{{

  class parser {
   public:
    using make_type = unique_ptr<parser>;
    static make_type make(string&& scriptname, const options&& opts);

    explicit parser(string&& synopsis, const options&& opts);

    void usage() const;

    void process_input(const vector<string>& values);

    bool has(const string& option) const;
    bool has(size_t index) const;
    string get(string opt) const;
    string get(size_t index) const;
    bool compare(string opt, const string& val) const;
    bool compare(size_t index, const string& val) const;

   protected:
    auto is_short(const string& option, const string& opt_short) const;
    auto is_long(const string& option, const string& opt_long) const;
    auto is(const string& option, string opt_short, string opt_long) const;

    auto parse_value(string input, const string& input_next, choices values) const;
    void parse(const string& input, const string& input_next = "");

   private:
    string m_synopsis{};
    const options m_opts;
    values m_optvalues{};
    posargs m_posargs{};
    bool m_skipnext{false};
  };

  // }}}
}

POLYBAR_NS_END