|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | +Module: Command line parsing |
| 4 | +
|
| 5 | +Author: Peter Schrammel |
| 6 | +
|
| 7 | +\*******************************************************************/ |
| 8 | + |
| 9 | +// \file Command line options definition |
| 10 | + |
| 11 | +#include "cmdline_definition.h" |
| 12 | + |
| 13 | +#include <sstream> |
| 14 | + |
| 15 | +#include <util/invariant.h> |
| 16 | +#include <util/json.h> |
| 17 | +#include <util/xml.h> |
| 18 | + |
| 19 | +cmdline_optiont::cmdline_optiont( |
| 20 | + std::string name, |
| 21 | + optionalt<argumentt> argument, |
| 22 | + std::string help_text, |
| 23 | + std::string option_group, |
| 24 | + optionalt<std::string> deprecated) |
| 25 | + : name(name), |
| 26 | + argument(argument), |
| 27 | + help_text(help_text), |
| 28 | + option_group(option_group), |
| 29 | + deprecated(deprecated) |
| 30 | +{ |
| 31 | +} |
| 32 | + |
| 33 | +cmdline_optiont::argumentt::argumentt(std::string name, std::string type) |
| 34 | + : name(name), type(type) |
| 35 | +{ |
| 36 | +} |
| 37 | + |
| 38 | +cmdline_definitiont::cmdline_definitiont( |
| 39 | + std::initializer_list<cmdline_optiont> &&initializer_list) |
| 40 | + : cmdline_options(initializer_list) |
| 41 | +{ |
| 42 | +} |
| 43 | + |
| 44 | +void cmdline_definitiont::concat(const cmdline_definitiont &other) |
| 45 | +{ |
| 46 | + cmdline_options.insert( |
| 47 | + cmdline_options.end(), |
| 48 | + other.cmdline_options.begin(), |
| 49 | + other.cmdline_options.end()); |
| 50 | +} |
| 51 | + |
| 52 | +std::string cmdline_definitiont::to_help_text( |
| 53 | + std::size_t option_width, |
| 54 | + std::size_t total_width) const |
| 55 | +{ |
| 56 | + std::ostringstream os; |
| 57 | + std::string option_group; |
| 58 | + for(const auto &option : cmdline_options) |
| 59 | + { |
| 60 | + // do not print deprecated options |
| 61 | + if(option.deprecated.has_value()) |
| 62 | + continue; |
| 63 | + |
| 64 | + // print option group at the start of a new grouop |
| 65 | + if(option.option_group != option_group) |
| 66 | + { |
| 67 | + option_group = option.option_group; |
| 68 | + os << '\n' << option_group << ":\n"; |
| 69 | + } |
| 70 | + |
| 71 | + // print option with optional argument and auto-wrap help text |
| 72 | + std::string option_text = option.name; |
| 73 | + if(option.name.size() == 1) |
| 74 | + option_text = " -" + option_text; |
| 75 | + else |
| 76 | + option_text = " --" + option_text; |
| 77 | + if(option.argument.has_value()) |
| 78 | + option_text += " " + option.argument->name; |
| 79 | + os << option_text; |
| 80 | + std::size_t padding = option_width; |
| 81 | + if(option_text.size() < option_width) |
| 82 | + padding = option_width - (option_text.size()); |
| 83 | + else |
| 84 | + os << '\n'; |
| 85 | + std::size_t index = 0; |
| 86 | + do |
| 87 | + { |
| 88 | + os << std::string(padding, ' ') |
| 89 | + << option.help_text.substr(index, total_width - option_width) |
| 90 | + << '\n'; |
| 91 | + padding = option_width; |
| 92 | + index += total_width - option_width; |
| 93 | + } while(index < option.help_text.size() - 1); |
| 94 | + } |
| 95 | + return os.str(); |
| 96 | +} |
| 97 | + |
| 98 | +std::string cmdline_definitiont::to_option_string() const |
| 99 | +{ |
| 100 | + std::ostringstream os; |
| 101 | + for(const auto &option : cmdline_options) |
| 102 | + os << "(" + option.name + ")" + (option.argument.has_value() ? ":" : ""); |
| 103 | + return os.str(); |
| 104 | +} |
| 105 | + |
| 106 | +json_arrayt cmdline_definitiont::to_json() const |
| 107 | +{ |
| 108 | + json_arrayt json_array; |
| 109 | + for(const auto &option : cmdline_options) |
| 110 | + { |
| 111 | + json_objectt json_option; |
| 112 | + json_option["name"] = json_stringt(option.name); |
| 113 | + json_option["helpText"] = json_stringt(option.help_text); |
| 114 | + json_option["optionGroup"] = json_stringt(option.option_group); |
| 115 | + if(option.argument.has_value()) |
| 116 | + { |
| 117 | + json_objectt &json_argument = json_option["argument"].make_object(); |
| 118 | + json_argument["name"] = json_stringt(option.argument->name); |
| 119 | + json_argument["type"] = json_stringt(option.argument->type); |
| 120 | + } |
| 121 | + if(option.deprecated.has_value()) |
| 122 | + { |
| 123 | + json_option["deprecated"] = json_stringt(*option.deprecated); |
| 124 | + } |
| 125 | + json_array.push_back(std::move(json_option)); |
| 126 | + } |
| 127 | + return json_array; |
| 128 | +} |
| 129 | + |
| 130 | +xmlt cmdline_definitiont::to_xml() const |
| 131 | +{ |
| 132 | + xmlt xml_cmdline("commandline"); |
| 133 | + for(const auto &option : cmdline_options) |
| 134 | + { |
| 135 | + xmlt &xml_option = xml_cmdline.new_element("option"); |
| 136 | + xml_option.set_attribute("name", option.name); |
| 137 | + xml_option.new_element("help_text").data = option.help_text; |
| 138 | + xml_option.new_element("option_group").data = option.option_group; |
| 139 | + if(option.argument.has_value()) |
| 140 | + { |
| 141 | + xmlt &xml_argument = xml_option.new_element("argument"); |
| 142 | + xml_argument.set_attribute("name", option.argument->name); |
| 143 | + xml_argument.set_attribute("type", option.argument->type); |
| 144 | + } |
| 145 | + if(option.deprecated.has_value()) |
| 146 | + { |
| 147 | + xml_option.new_element("deprecated").data = *option.deprecated; |
| 148 | + } |
| 149 | + } |
| 150 | + return xml_cmdline; |
| 151 | +} |
| 152 | + |
| 153 | +cmdline_definitiont |
| 154 | +operator+(cmdline_definitiont first, const cmdline_definitiont &second) |
| 155 | +{ |
| 156 | + first.concat(second); |
| 157 | + return first; |
| 158 | +} |
0 commit comments