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

summaryrefslogtreecommitdiff
path: root/source/core/StarOptionParser.cpp
blob: 3cd32cc14fadae9323336280772e4fb4b558488c (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "StarOptionParser.hpp"
#include "StarIterator.hpp"

namespace Star {

void OptionParser::setCommandName(String commandName) {
  m_commandName = std::move(commandName);
}

void OptionParser::setSummary(String summary) {
  m_summary = std::move(summary);
}

void OptionParser::setAdditionalHelp(String help) {
  m_additionalHelp = std::move(help);
}

void OptionParser::addSwitch(String const& flag, String description) {
  if (!m_options.insert(flag, Switch{flag, std::move(description)}).second)
    throw OptionParserException::format("Duplicate switch '-{}' added", flag);
}

void OptionParser::addParameter(String const& flag, String argument, RequirementMode requirementMode, String description) {
  if (!m_options.insert(flag, Parameter{flag, std::move(argument), requirementMode, std::move(description)}).second)
    throw OptionParserException::format("Duplicate flag '-{}' added", flag);
}

void OptionParser::addArgument(String argument, RequirementMode requirementMode, String description) {
  m_arguments.append(Argument{std::move(argument), requirementMode, std::move(description)});
}

pair<OptionParser::Options, StringList> OptionParser::parseOptions(StringList const& arguments) const {
  Options result;
  StringList errors;
  bool endOfFlags = false;

  auto it = makeSIterator(arguments);
  while (it.hasNext()) {
    auto const& arg = it.next();
    if (arg == "--") {
      endOfFlags = true;
      continue;
    }

    if (!endOfFlags && arg.beginsWith("-")) {
      String flag = arg.substr(1);
      auto option = m_options.maybe(flag);
      if (!option) {
        errors.append(strf("No such option '-{}'", flag));
        continue;
      }

      if (option->is<Switch>()) {
        result.switches.add(std::move(flag));
      } else {
        auto const& parameter = option->get<Parameter>();
        if (!it.hasNext()) {
          errors.append(strf("Option '-{}' must be followed by an argument", flag));
          continue;
        }
        String val = it.next();
        if (parameter.requirementMode != Multiple && result.parameters.contains(flag)) {
          errors.append(strf("Option with argument '-{}' specified multiple times", flag));
          continue;
        }
        result.parameters[std::move(flag)].append(std::move(val));
      }

    } else {
      result.arguments.append(arg);
    }
  }

  for (auto const& pair : m_options) {
    if (pair.second.is<Parameter>()) {
      auto const& na = pair.second.get<Parameter>();
      if (na.requirementMode == Required && !result.parameters.contains(pair.first))
        errors.append(strf("Missing required flag with argument '-{}'", pair.first));
    }
  }

  size_t minimumArguments = 0;
  size_t maximumArguments = 0;
  for (auto const& argument : m_arguments) {
    if ((argument.requirementMode == Optional || argument.requirementMode == Required) && maximumArguments != NPos)
      ++maximumArguments;
    if (argument.requirementMode == Required)
      ++minimumArguments;
    if (argument.requirementMode == Multiple)
      maximumArguments = NPos;
  }
  if (result.arguments.size() < minimumArguments)
    errors.append(strf(
        "Too few positional arguments given, expected at least {} got {}", minimumArguments, result.arguments.size()));
  if (result.arguments.size() > maximumArguments)
    errors.append(strf(
        "Too many positional arguments given, expected at most {} got {}", maximumArguments, result.arguments.size()));

  return {std::move(result), std::move(errors)};
}

void OptionParser::printHelp(std::ostream& os) const {
  if (!m_commandName.empty() && !m_summary.empty())
    format(os, "{}: {}\n\n", m_commandName, m_summary);
  else if (!m_commandName.empty())
    format(os, "{}:\n\n", m_commandName);
  else if (!m_summary.empty())
    format(os, "{}\n\n", m_summary);

  String cmdLineText;

  for (auto const& p : m_options) {
    if (p.second.is<Switch>()) {
      cmdLineText += strf(" [-{}]", p.first);
    } else {
      auto const& parameter = p.second.get<Parameter>();
      if (parameter.requirementMode == Optional)
        cmdLineText += strf(" [-{} <{}>]", parameter.flag, parameter.argument);
      else if (parameter.requirementMode == Required)
        cmdLineText += strf(" -{} <{}>", parameter.flag, parameter.argument);
      else if (parameter.requirementMode == Multiple)
        cmdLineText += strf(" [-{} <{}>]...", parameter.flag, parameter.argument);
    }
  }

  for (auto const& p : m_arguments) {
    if (p.requirementMode == Optional)
      cmdLineText += strf(" [<{}>]", p.argumentName);
    else if (p.requirementMode == Required)
      cmdLineText += strf(" <{}>", p.argumentName);
    else
      cmdLineText += strf(" [<{}>...]", p.argumentName);
  }

  if (m_commandName.empty())
    format(os, "Command Line Usage:{}\n", cmdLineText);
  else
    format(os, "Command Line Usage: {}{}\n", m_commandName, cmdLineText);

  for (auto const& p : m_options) {
    if (p.second.is<Switch>()) {
      auto const& sw = p.second.get<Switch>();
      if (!sw.description.empty())
        format(os, "  -{}\t- {}\n", sw.flag, sw.description);
    }
    if (p.second.is<Parameter>()) {
      auto const& parameter = p.second.get<Parameter>();
      if (!parameter.description.empty())
        format(os, "  -{} <{}>\t- {}\n", parameter.flag, parameter.argument, parameter.description);
    }
  }

  for (auto const& p : m_arguments) {
    if (!p.description.empty())
      format(os, "  <{}>\t- {}\n", p.argumentName, p.description);
  }

  if (!m_additionalHelp.empty())
    format(os, "\n{}\n", m_additionalHelp);
}

}