osb/source/core/StarOptionParser.cpp

163 lines
5.4 KiB
C++
Raw Normal View History

2023-06-20 04:33:09 +00:00
#include "StarOptionParser.hpp"
#include "StarIterator.hpp"
namespace Star {
void OptionParser::setCommandName(String commandName) {
m_commandName = move(commandName);
}
void OptionParser::setSummary(String summary) {
m_summary = move(summary);
}
void OptionParser::setAdditionalHelp(String help) {
m_additionalHelp = move(help);
}
void OptionParser::addSwitch(String const& flag, String description) {
if (!m_options.insert(flag, Switch{flag, move(description)}).second)
2023-06-27 10:23:44 +00:00
throw OptionParserException::format("Duplicate switch '-{}' added", flag);
2023-06-20 04:33:09 +00:00
}
void OptionParser::addParameter(String const& flag, String argument, RequirementMode requirementMode, String description) {
if (!m_options.insert(flag, Parameter{flag, move(argument), requirementMode, move(description)}).second)
2023-06-27 10:23:44 +00:00
throw OptionParserException::format("Duplicate flag '-{}' added", flag);
2023-06-20 04:33:09 +00:00
}
void OptionParser::addArgument(String argument, RequirementMode requirementMode, String description) {
m_arguments.append(Argument{move(argument), requirementMode, 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) {
2023-06-27 10:23:44 +00:00
errors.append(strf("No such option '-{}'", flag));
2023-06-20 04:33:09 +00:00
continue;
}
if (option->is<Switch>()) {
result.switches.add(move(flag));
} else {
auto const& parameter = option->get<Parameter>();
if (!it.hasNext()) {
2023-06-27 10:23:44 +00:00
errors.append(strf("Option '-{}' must be followed by an argument", flag));
2023-06-20 04:33:09 +00:00
continue;
}
String val = it.next();
if (parameter.requirementMode != Multiple && result.parameters.contains(flag)) {
2023-06-27 10:23:44 +00:00
errors.append(strf("Option with argument '-{}' specified multiple times", flag));
2023-06-20 04:33:09 +00:00
continue;
}
result.parameters[move(flag)].append(move(val));
}
} else {
result.arguments.append(arg);
2023-06-20 04:33:09 +00:00
}
}
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))
2023-06-27 10:23:44 +00:00
errors.append(strf("Missing required flag with argument '-{}'", pair.first));
2023-06-20 04:33:09 +00:00
}
}
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(
2023-06-27 10:23:44 +00:00
"Too few positional arguments given, expected at least {} got {}", minimumArguments, result.arguments.size()));
2023-06-20 04:33:09 +00:00
if (result.arguments.size() > maximumArguments)
errors.append(strf(
2023-06-27 10:23:44 +00:00
"Too many positional arguments given, expected at most {} got {}", maximumArguments, result.arguments.size()));
2023-06-20 04:33:09 +00:00
return {move(result), move(errors)};
}
void OptionParser::printHelp(std::ostream& os) const {
if (!m_commandName.empty() && !m_summary.empty())
2023-06-27 10:23:44 +00:00
format(os, "{}: {}\n\n", m_commandName, m_summary);
2023-06-20 04:33:09 +00:00
else if (!m_commandName.empty())
2023-06-27 10:23:44 +00:00
format(os, "{}:\n\n", m_commandName);
2023-06-20 04:33:09 +00:00
else if (!m_summary.empty())
2023-06-27 10:23:44 +00:00
format(os, "{}\n\n", m_summary);
2023-06-20 04:33:09 +00:00
String cmdLineText;
for (auto const& p : m_options) {
if (p.second.is<Switch>()) {
2023-06-27 10:23:44 +00:00
cmdLineText += strf(" [-{}]", p.first);
2023-06-20 04:33:09 +00:00
} else {
auto const& parameter = p.second.get<Parameter>();
if (parameter.requirementMode == Optional)
2023-06-27 10:23:44 +00:00
cmdLineText += strf(" [-{} <{}>]", parameter.flag, parameter.argument);
2023-06-20 04:33:09 +00:00
else if (parameter.requirementMode == Required)
2023-06-27 10:23:44 +00:00
cmdLineText += strf(" -{} <{}>", parameter.flag, parameter.argument);
2023-06-20 04:33:09 +00:00
else if (parameter.requirementMode == Multiple)
2023-06-27 10:23:44 +00:00
cmdLineText += strf(" [-{} <{}>]...", parameter.flag, parameter.argument);
2023-06-20 04:33:09 +00:00
}
}
for (auto const& p : m_arguments) {
if (p.requirementMode == Optional)
2023-06-27 10:23:44 +00:00
cmdLineText += strf(" [<{}>]", p.argumentName);
2023-06-20 04:33:09 +00:00
else if (p.requirementMode == Required)
2023-06-27 10:23:44 +00:00
cmdLineText += strf(" <{}>", p.argumentName);
2023-06-20 04:33:09 +00:00
else
2023-06-27 10:23:44 +00:00
cmdLineText += strf(" [<{}>...]", p.argumentName);
2023-06-20 04:33:09 +00:00
}
if (m_commandName.empty())
2023-06-27 10:23:44 +00:00
format(os, "Command Line Usage:{}\n", cmdLineText);
2023-06-20 04:33:09 +00:00
else
2023-06-27 10:23:44 +00:00
format(os, "Command Line Usage: {}{}\n", m_commandName, cmdLineText);
2023-06-20 04:33:09 +00:00
for (auto const& p : m_options) {
if (p.second.is<Switch>()) {
auto const& sw = p.second.get<Switch>();
if (!sw.description.empty())
2023-06-27 10:23:44 +00:00
format(os, " -{}\t- {}\n", sw.flag, sw.description);
2023-06-20 04:33:09 +00:00
}
if (p.second.is<Parameter>()) {
auto const& parameter = p.second.get<Parameter>();
if (!parameter.description.empty())
2023-06-27 10:23:44 +00:00
format(os, " -{} <{}>\t- {}\n", parameter.flag, parameter.argument, parameter.description);
2023-06-20 04:33:09 +00:00
}
}
for (auto const& p : m_arguments) {
if (!p.description.empty())
2023-06-27 10:23:44 +00:00
format(os, " <{}>\t- {}\n", p.argumentName, p.description);
2023-06-20 04:33:09 +00:00
}
if (!m_additionalHelp.empty())
2023-06-27 10:23:44 +00:00
format(os, "\n{}\n", m_additionalHelp);
2023-06-20 04:33:09 +00:00
}
}