Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/core/StarOptionParser.hpp
blob: 946724ca6c879504a6b92f85abfef53c35bd8c21 (plain)
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
79
80
81
#pragma once

#include "StarString.hpp"
#include "StarVariant.hpp"
#include "StarOrderedMap.hpp"
#include "StarOrderedSet.hpp"

namespace Star {

STAR_EXCEPTION(OptionParserException, StarException);

// Simple command line argument parsing and help printing, only simple single
// dash flags are supported, no flag combining is allowed and all components
// must be separated by a space.
//
// A 'flag' here refers to a component that is preceded by a dash, like -f or
// -quiet.
//
// Three kinds of things are parsed:
// - 'switches' which are flags that do not have a value, like `-q` for quiet
// - 'parameters' are flags with a value that follows, like `-mode full`
// - 'arguments' are everything else, sorted positionally
class OptionParser {
public:
  enum RequirementMode {
    Optional,
    Required,
    Multiple
  };

  struct Options {
    OrderedSet<String> switches;
    StringMap<StringList> parameters;
    StringList arguments;
  };

  void setCommandName(String commandName);
  void setSummary(String summary);
  void setAdditionalHelp(String help);

  void addSwitch(String const& flag, String description = {});
  void addParameter(String const& flag, String argumentName, RequirementMode mode, String description = {});
  void addArgument(String argumentName, RequirementMode mode, String description = {});

  // Parse the given arguments into an options set, returns the options parsed
  // and a list of all the errors encountered while parsing.
  pair<Options, StringList> parseOptions(StringList const& arguments) const;

  // Print help text to the given std::ostream
  void printHelp(std::ostream& os) const;

private:
  struct Switch {
    String flag;
    String description;
  };

  struct Parameter {
    String flag;
    String argument;
    RequirementMode requirementMode;
    String description;
  };

  struct Argument {
    String argumentName;
    RequirementMode requirementMode;
    String description;
  };

  typedef Variant<Switch, Parameter> Option;

  String m_commandName;
  String m_summary;
  String m_additionalHelp;

  OrderedHashMap<String, Option> m_options;
  List<Argument> m_arguments;
};

}