CommandLine.hpp
Go to the documentation of this file.
1 // @formatter:off
2 //
3 // Balau core C++ library
4 //
5 // Copyright (C) 2008 Bora Software (contact@borasoftware.com)
6 //
7 // Licensed under the Boost Software License - Version 1.0 - August 17th, 2003.
8 // See the LICENSE file for the full license text.
9 //
10 
16 
17 #ifndef COM_BORA_SOFTWARE__BALAU_APPLICATION__COMMAND_LINE
18 #define COM_BORA_SOFTWARE__BALAU_APPLICATION__COMMAND_LINE
19 
21 #include <Balau/Type/StdTypes.hpp>
22 #include <Balau/Util/Strings.hpp>
23 
24 #include <cstdlib>
25 #include <iomanip>
26 #include <map>
27 
28 namespace Balau {
29 
33 enum class CommandLineStyle {
40 
47 
54  Detect
55 };
56 
60 template <typename KeyT> class CommandLine {
67  : style(style_) {}
68 
72  public: CommandLine(const CommandLine & ) = default;
73 
77  public: CommandLine(CommandLine && ) noexcept = default;
78 
82  public: CommandLine & operator = (const CommandLine & ) = default;
83 
87  public: CommandLine & operator = (CommandLine && ) noexcept = default;
88 
90 
100  public: CommandLine & withOption(KeyT key,
101  const std::string & shortSwitch,
102  const std::string & longSwitch,
103  bool hasValue,
104  const std::string & documentation) {
105  keysBySwitch[shortSwitch] = key;
106  keysBySwitch[longSwitch] = key;
107  optionsByKey[key] = Option(key, shortSwitch, longSwitch, hasValue, documentation);
108  return *this;
109  }
110 
119  public: CommandLine & withOption(KeyT key,
120  const std::string & shortSwitch,
121  bool hasValue,
122  const std::string & documentation) {
123  keysBySwitch[shortSwitch] = key;
124  optionsByKey[key] = Option(key, shortSwitch, "", hasValue, documentation);
125  return *this;
126  }
127 
135  public: CommandLine & withHelpOption(KeyT helpKey,
136  const std::string & shortSwitch,
137  const std::string & longSwitch,
138  const std::string & helpDocumentation) {
139  keysBySwitch[shortSwitch] = helpKey;
140  keysBySwitch[longSwitch] = helpKey;
141  optionsByKey[helpKey] = Option(helpKey, shortSwitch, longSwitch, false, helpDocumentation);
142  return *this;
143  }
144 
149  commandLineHasFinalValue = HasFinalValue::Yes;
150  return *this;
151  }
152 
157  commandLineHasFinalValue = HasFinalValue::Maybe;
158  return *this;
159  }
160 
162 
174  public: void parse(int argc, char * argv[], bool ignoreFirst) {
175  std::vector<std::string> args(static_cast<unsigned long>(argc));
176 
177  for (int m = 0; m < argc; m++) {
178  args[m] = std::string(argv[m]);
179  }
180 
181  parse(args, ignoreFirst);
182  }
183 
195  public: void parse(int argc, const char * argv[], bool ignoreFirst) {
196  std::vector<std::string> args(static_cast<unsigned long>(argc));
197 
198  for (int m = 0; m < argc; m++) {
199  args[m] = std::string(argv[m]);
200  }
201 
202  parse(args, ignoreFirst);
203  }
204 
219  public: void parse(const std::vector<std::string> & args, bool ignoreFirst) {
221 
222  parsedValuesByKey.clear();
223 
224  if (args.empty() || (ignoreFirst && args.size() == 1)) {
225  if (commandLineHasFinalValue == HasFinalValue::Yes) {
227  }
228 
229  return; // No arguments provided.. nothing to do.
230  }
231 
232  const CommandLineStyle derivedStyle = style == CommandLineStyle::Detect
233  ? determineStyle(args[ignoreFirst ? 1 : 0], ignoreFirst ? args.size() - 1: args.size())
234  : style;
235 
236  if (derivedStyle == CommandLineStyle::SwitchEqualsValue) {
237  parseSwitchEqualsValue(derivedStyle, args, ignoreFirst);
238  } else {
239  parseSwitchSpaceValue(derivedStyle, args, ignoreFirst);
240  }
241  }
242 
244 
248  public: bool hasOption(KeyT key) const {
249  return parsedValuesByKey.find(key) != parsedValuesByKey.end();
250  }
251 
252 
258  public: size_t getFinalValueCount() const {
259  return finalValues.size();
260  }
261 
263 
269  public: std::string getOption(KeyT key) const {
271 
272  if (parsedValuesByKey.find(key) == parsedValuesByKey.end()) {
274  }
275 
276  return parsedValuesByKey.at(key);
277  }
278 
282  public: std::string getOptionOrDefault(KeyT key, std::string defaultValue) const {
283  if (parsedValuesByKey.find(key) == parsedValuesByKey.end()) {
284  return defaultValue;
285  }
286 
287  return parsedValuesByKey.at(key);
288  }
289 
291 
298  public: short getOptionAsShort(KeyT key) const {
299  auto parse = [] (const std::string & str) { return std::stoi(str); };
300  return getOptionAs<short, int>(key, "a short integer", parse);
301  }
302 
309  public: unsigned short getOptionAsUnsignedShort(KeyT key) const {
310  auto parse = [] (const std::string & str) { return std::stoull(str); };
311  return getOptionAs<unsigned short, unsigned long long>(key, "an unsigned short integer", parse);
312  }
313 
320  public: int getOptionAsInt(KeyT key) const {
321  auto parse = [] (const std::string & str) { return std::stoi(str); };
322  return getOptionAs<int, int>(key, "an integer", parse);
323  }
324 
331  public: unsigned int getOptionAsUnsignedInt(KeyT key) const {
332  auto parse = [] (const std::string & str) { return std::stoull(str); };
333  return getOptionAs<unsigned int, unsigned long long>(key, "an unsigned integer", parse);
334  }
335 
342  public: long long getOptionAsLong(KeyT key) const {
343  auto parse = [] (const std::string & str) { return std::stoll(str); };
344  return getOptionAs<long long, long long>(key, "a long integer", parse);
345  }
346 
353  public: unsigned long long getOptionAsUnsignedLong(KeyT key) const {
354  auto parse = [] (const std::string & str) { return std::stoull(str); };
355  return getOptionAs<unsigned long long, unsigned long long>(key, "an unsigned long long integer", parse);
356  }
357 
359 
366  public: short getOptionAsShortOrDefault(KeyT key, short defaultValue) const {
367  auto parse = [] (const std::string & str) { return std::stoi(str); };
368  return getOptionAs<short, int>(key, "a short integer", parse, true, defaultValue);
369  }
370 
377  public: unsigned short getOptionAsUnsignedShortOrDefault(KeyT key, unsigned short defaultValue) const {
378  auto parse = [] (const std::string & str) { return std::stoull(str); };
379  return getOptionAs<unsigned short, unsigned long long>(key, "an unsigned short integer", parse, true, defaultValue);
380  }
381 
388  public: int getOptionAsIntOrDefault(KeyT key, int defaultValue) const {
389  auto parse = [] (const std::string & str) { return std::stoi(str); };
390  return getOptionAs<int, int>(key, "an integer", parse, true, defaultValue);
391  }
392 
399  public: unsigned int getOptionAsUnsignedIntOrDefault(KeyT key, unsigned int defaultValue) const {
400  auto parse = [] (const std::string & str) { return std::stoull(str); };
401  return getOptionAs<unsigned int, unsigned long long>(key, "an unsigned integer", parse, true, defaultValue);
402  }
403 
410  public: long long getOptionAsLongOrDefault(KeyT key, long long defaultValue) const {
411  auto parse = [] (const std::string & str) { return std::stoll(str); };
412  return getOptionAs<long long, long long>(key, "a long integer", parse, true, defaultValue);
413  }
414 
421  public: unsigned long long getOptionAsUnsignedLongOrDefault(KeyT key, unsigned long long defaultValue) const {
422  auto parse = [] (const std::string & str) { return std::stoull(str); };
423  return getOptionAs<unsigned long long, unsigned long long>(key, "an unsigned long long integer", parse, true, defaultValue);
424  }
425 
427 
434  public: float getOptionAsFloat(KeyT key) const {
435  auto parse = [] (const std::string & str) { return std::stof(str); };
436  return getOptionAs<float, float>(key, "a single precision floating point value", parse);
437  }
438 
445  public: double getOptionAsDouble(KeyT key) const {
446  auto parse = [] (const std::string & str) { return std::stod(str); };
447  return getOptionAs<double, double>(key, "a double precision floating point value", parse);
448  }
449 
451 
458  public: float getOptionAsFloatOrDefault(KeyT key, float defaultValue) const {
459  auto parse = [] (const std::string & str) { return std::stof(str); };
460  return getOptionAs<float, float>(key, "a single precision floating point value", parse, true, defaultValue);
461  }
462 
469  public: double getOptionAsDoubleOrDefault(KeyT key, double defaultValue) const {
470  auto parse = [] (const std::string & str) { return std::stod(str); };
471  return getOptionAs<double, double>(key, "a double precision floating point value", parse, true, defaultValue);
472  }
473 
475 
481  public: std::string getFinalValue(size_t index = 0) const {
482  if (commandLineHasFinalValue == HasFinalValue::No) {
483  ThrowBalauException(Exception::OptionNotFoundException, "This parser does not have final values.");
484  }
485 
486  if (finalValues.size() <= index) {
489  , ::toString("This parser does not have a final value with index ", index)
490  );
491  }
492 
493  return finalValues[index];
494  }
495 
502  public: float getFinalValueAsFloat(size_t index = 0) const {
503  auto parse = [] (const std::string & str) { return std::stof(str); };
504  return getFinalValueAs<float>("a single precision floating point value", parse, index);
505  }
506 
513  public: double getFinalValueAsDouble(size_t index = 0) const {
514  auto parse = [] (const std::string & str) { return std::stod(str); };
515  return getFinalValueAs<double>("a double precision floating point value", parse, index);
516  }
517 
519 
523  public: std::string getFinalValueOrDefault(const std::string & defaultValue, size_t index = 0) const {
524  if (finalValues.size() <= index) {
525  return defaultValue;
526  }
527 
528  return finalValues[index];
529  }
530 
537  public: float getFinalValueAsFloatOrDefault(float defaultValue, size_t index = 0) const {
538  auto parse = [] (const std::string & str) { return std::stof(str); };
539  return getFinalValueAs<float>("a single precision floating point value", parse, index, true, defaultValue);
540  }
541 
548  public: double getFinalValueAsDoubleOrDefault(double defaultValue, size_t index = 0) const {
549  auto parse = [] (const std::string & str) { return std::stod(str); };
550  return getFinalValueAs<double>("a double precision floating point value", parse, index, true, defaultValue);
551  }
552 
554 
569  public: std::string getHelpText(size_t indent,
570  size_t totalWidth,
571  CommandLineStyle styleOverride = CommandLineStyle::Detect) const {
572  const CommandLineStyle determinedStyle = styleOverride != CommandLineStyle::Detect
573  ? styleOverride
574  : style != CommandLineStyle::Detect
575  ? style
577 
578  size_t maxSwitchTextWidth = 0;
579 
580  for (const auto & pair : optionsByKey) {
581  const std::string shortSw = determinedStyle == CommandLineStyle::SwitchSpaceValue
582  ? "-" + pair.second.shortSwitch
583  : pair.second.shortSwitch;
584 
585  const std::string longSw = determinedStyle == CommandLineStyle::SwitchSpaceValue
586  ? (pair.second.longSwitch.empty() ? "" : "--" + pair.second.longSwitch)
587  : pair.second.longSwitch;
588 
589  const std::string switches = shortSw + (longSw.empty() ? "" : ", " + longSw);
590 
591  if (maxSwitchTextWidth < switches.length()) {
592  maxSwitchTextWidth = switches.length();
593  }
594  }
595 
596  // Apply a minimum text width if the specified total width is very small.
597  if (totalWidth < maxSwitchTextWidth + 20) {
598  totalWidth = maxSwitchTextWidth + 20;
599  }
600 
601  std::string prefix(indent, ' ');
602  std::string contPrefix(indent + 3 + maxSwitchTextWidth, ' ');
603  std::ostringstream builder;
604 
605  for (const auto & pair : optionsByKey) {
606  const std::string shortSw = determinedStyle == CommandLineStyle::SwitchSpaceValue
607  ? "-" + pair.second.shortSwitch
608  : pair.second.shortSwitch;
609 
610  const std::string longSw = determinedStyle == CommandLineStyle::SwitchSpaceValue
611  ? (pair.second.longSwitch.empty() ? "" : "--" + pair.second.longSwitch)
612  : pair.second.longSwitch;
613 
614  const auto & option = pair.second;
615 
616  const std::string switches = shortSw + (longSw.empty() ? "" : ", " + longSw);
617 
618  builder << prefix << std::left << std::setw((int) maxSwitchTextWidth) << switches << " - ";
619 
620  const std::string & docText = option.documentation;
621  size_t charactersWritten = 0;
622  bool first = true;
623 
624  while (charactersWritten < docText.length()) {
625  const size_t charactersRemaining = docText.length() - charactersWritten;
626 
627  if (charactersRemaining <= totalWidth) {
628  builder << (first ? "" : contPrefix) << docText.substr(charactersWritten) << "\n";
629  charactersWritten = docText.length();
630  } else {
631  builder << (first ? "" : contPrefix) << docText.substr(charactersWritten, totalWidth) << "\n";
632  charactersWritten += totalWidth;
633  }
634 
635  first = false;
636  }
637  }
638 
639  return builder.str();
640  }
641 
643 
644  private: CommandLineStyle determineStyle(const std::string & firstArgument, size_t argCount) const {
645  if (style != CommandLineStyle::Detect) {
646  return style;
647  }
648 
649  if (commandLineHasFinalValue == HasFinalValue::Yes && argCount == 1) {
650  // Doesn't matter.. only the final value argument is present.
652  }
653 
654  return Util::Strings::startsWith(firstArgument, "-")
657  }
658 
659  private: void parseSwitchEqualsValue(const CommandLineStyle & derivedStyle,
660  const std::vector<std::string> & args,
661  bool ignoreFirst) {
663 
664  std::vector<std::string> cleanedArgs;
665  size_t m = ignoreFirst ? 1 : 0;
666 
667  while (m < args.size()) {
668  const std::string & arg = args[m];
669 
670  if (Util::Strings::contains(arg, "=")) {
671  const size_t first = arg.find('=');
672  cleanedArgs.emplace_back(arg.substr(0, first));
673  cleanedArgs.emplace_back(arg.substr(first + 1));
674  } else {
675  cleanedArgs.emplace_back(arg);
676  }
677 
678  m++;
679  }
680 
681  for (size_t m = 0; m < (commandLineHasFinalValue == HasFinalValue::Yes ? cleanedArgs.size() - 1 : cleanedArgs.size()); m++) {
682  const std::string optionSwitch = toString(Util::Strings::trim(cleanedArgs[m]));
683  auto iter = keysBySwitch.find(optionSwitch);
684 
685  if (iter == keysBySwitch.end()) {
687  }
688 
689  const auto & key = iter->second;
690  const auto & option = optionsByKey.at(key);
691 
692  if (option.hasValue) {
693  if (commandLineHasFinalValue == HasFinalValue::Yes && cleanedArgs.size() - m < 2) {
695  } else if (cleanedArgs.size() - m < 1) {
697  }
698 
699  m++;
700  parsedValuesByKey[key] = toString(Util::Strings::trim(cleanedArgs[m]));
701  } else {
702  parsedValuesByKey[key] = "";
703  }
704  }
705 
706  if (commandLineHasFinalValue == HasFinalValue::Yes) {
707  finalValues.push_back(toString(Util::Strings::trim(cleanedArgs.back())));
708  }
709  }
710 
711  private: void parseSwitchSpaceValue(const CommandLineStyle & derivedStyle,
712  const std::vector<std::string> & args,
713  bool ignoreFirst) {
714  size_t m = ignoreFirst ? 1 : 0;
715 
716  for (; m < args.size(); m++) {
718  std::string optionSwitch = toString(Util::Strings::trim(args[m]));
719 
720  // Full switches must start with "--", abbreviated switches must start with "-".
721  // These are removed for the derivedSwitch.
722 
723  if (Util::Strings::startsWith(optionSwitch, "--")) {
724  optionSwitch = optionSwitch.substr(2);
725  } else if (Util::Strings::startsWith(optionSwitch, "-")) {
726  optionSwitch = optionSwitch.substr(1);
727  } else {
728  if (commandLineHasFinalValue == HasFinalValue::No) {
729  ThrowBalauException(Exception::IllegalArgumentException, "Illegal value found when parsing.");
730  }
731 
732  // Not a switch.
733  finalValues.push_back(std::move(optionSwitch));
734  continue;
735  }
736 
737  auto iter = keysBySwitch.find(optionSwitch);
738 
739  if (iter == keysBySwitch.end()) {
741  }
742 
743  const auto & key = iter->second;
744  const auto & option = optionsByKey.at(key);
745 
746  if (option.hasValue) {
747  if (commandLineHasFinalValue == HasFinalValue::Yes && finalValues.empty() && m + 2 == args.size()) {
749  } else if (m + 1 == args.size()) {
751  }
752 
753  m++;
754  parsedValuesByKey[key] = toString(Util::Strings::trim(args[m]));
755  } else {
756  parsedValuesByKey[key] = "";
757  }
758  }
759  }
760 
761  //
762  // Option numeric conversion function.
763  // - T is the required type.
764  // - C is the conversion function type which is subsequently narrowed.
765  //
766  private: template<typename T, typename C> T getOptionAs(KeyT key,
767  const std::string & typeAsString,
768  C parse(const std::string &),
769  bool orDefault = false,
770  T defaultValue = T()) const {
771  if (parsedValuesByKey.find(key) == parsedValuesByKey.end()) {
772  if (orDefault) {
773  return defaultValue;
774  }
775 
777 
780  , std::string("No option with key ") + toString(key) + " was supplied on the command line."
781  );
782  }
783 
784  const std::string str = parsedValuesByKey.at(key);
785 
786  try {
787  const C value = parse(str);
788  return boost::numeric_cast<T>(value);
789  } catch (const std::invalid_argument &) {
791 
793  , std::string(
794  "Option value of key '" + toString(key) + "' is not of type " + typeAsString + ": "
795  + parsedValuesByKey.at(key)
796  )
797  );
798  } catch (...) {
800 
803  , std::string(
804  "Option value of key '" + toString(key) + "' is not in the valid range for " + typeAsString + ": "
805  + parsedValuesByKey.at(key)
806  )
807  );
808  }
809  }
810 
811  //
812  // Final value numeric conversion function.
813  // - T is the required type.
814  //
815  private: template<typename T> T getFinalValueAs(const std::string & typeAsString,
816  T parse(const std::string &),
817  size_t index,
818  bool orDefault = false,
819  T defaultValue = T()) const {
820  if (commandLineHasFinalValue == HasFinalValue::No) {
821  if (orDefault) {
822  return defaultValue;
823  }
824 
825  ThrowBalauException(Exception::OptionNotFoundException, "This parser does not have final values.");
826  }
827 
828  if (finalValues.size() <= index) {
829  if (orDefault) {
830  return defaultValue;
831  }
832 
835  , ::toString("This parser does not have a final value with index ", index)
836  );
837  }
838 
839  try {
840  return parse(finalValues[index]);
841  } catch (const std::invalid_argument &) {
843  Exception::OptionValueException, std::string("Final value is not " + typeAsString + ": " + finalValues[index])
844  );
845  } catch (...) {
848  , std::string("Final value is not in the valid range for " + typeAsString + ": " + finalValues[index])
849  );
850  }
851  }
852 
853  private: struct Option {
854  KeyT key;
855  std::string shortSwitch;
856  std::string longSwitch;
857  bool hasValue;
858  std::string documentation;
859 
860  Option() : hasValue(false) {}
861 
862  Option(KeyT key_,
863  std::string shortSwitch_,
864  std::string longSwitch_,
865  bool hasValue_,
866  std::string documentation_)
867  : key(std::move(key_))
868  , shortSwitch(std::move(shortSwitch_))
869  , longSwitch(std::move(longSwitch_))
870  , hasValue(hasValue_)
871  , documentation(std::move(documentation_)) {}
872 
873  Option & operator = (const Option & copy) = default;
874  };
875 
876  private: enum class HasFinalValue {
877  No, Yes, Maybe
878  };
879 
880  private: CommandLineStyle style;
881  private: std::map<std::string, KeyT> keysBySwitch; // switch, key
882  private: std::map<KeyT, Option> optionsByKey; // key, option info
883  private: std::map<KeyT, std::string> parsedValuesByKey; // key, value
884  private: HasFinalValue commandLineHasFinalValue = HasFinalValue::No;
885  private: std::vector<std::string> finalValues;
886 };
887 
888 } // namespace Balau
889 
890 #endif // COM_BORA_SOFTWARE__BALAU_APPLICATION__COMMAND_LINE
The parser will allow either switch-space-value or switch-equals-value.
Thrown when an invalid command line parameter is specified.
Definition: CommandLineExceptions.hpp:38
float getOptionAsFloat(KeyT key) const
Get the specified option value as a float.
Definition: CommandLine.hpp:434
double getOptionAsDoubleOrDefault(KeyT key, double defaultValue) const
Get the specified option value as a double or return the supplied value if there is no such option...
Definition: CommandLine.hpp:469
#define ThrowBalauException(ExceptionClass,...)
Throw a Balau style exception, with implicit file and line number, and optional stacktrace.
Definition: BalauException.hpp:45
unsigned int getOptionAsUnsignedInt(KeyT key) const
Get the specified option value as an unsigned integer value.
Definition: CommandLine.hpp:331
double getOptionAsDouble(KeyT key) const
Get the specified option value as a double.
Definition: CommandLine.hpp:445
static bool contains(const StringT< CharT, T ... > &str, const SubstrT &substring)
Does the first string contain the second string?
Definition: Strings.hpp:174
bool hasOption(KeyT key) const
Does the command line option set have the specified option?
Definition: CommandLine.hpp:248
Balau::U8String< AllocatorT > toString(const BalauException &e)
Base class toString<AllocatorT> function for Balau exceptions.
Definition: BalauException.hpp:122
long long getOptionAsLong(KeyT key) const
Get the specified option value as a signed long long value.
Definition: CommandLine.hpp:342
void parse(int argc, char *argv[], bool ignoreFirst)
Parse the input arguments.
Definition: CommandLine.hpp:174
CommandLine & withFinalValue()
Specify that the command line arguments include one (SEV style) or one or more (SSV style) final stan...
Definition: CommandLine.hpp:148
The root Balau namespace.
Definition: ApplicationConfiguration.hpp:23
int getOptionAsIntOrDefault(KeyT key, int defaultValue) const
Get the specified option value as a signed int, or the specified default if there is no such option...
Definition: CommandLine.hpp:388
short getOptionAsShortOrDefault(KeyT key, short defaultValue) const
Get the specified option value as a signed short, or the specified default if there is no such option...
Definition: CommandLine.hpp:366
CommandLine & withOption(KeyT key, const std::string &shortSwitch, bool hasValue, const std::string &documentation)
Specify an option that has a short style switch.
Definition: CommandLine.hpp:119
Thrown when a requested option value cannot be coerced into the required type.
Definition: CommandLineExceptions.hpp:62
unsigned long long getOptionAsUnsignedLongOrDefault(KeyT key, unsigned long long defaultValue) const
Get the specified option value as an unsigned long long, or the specified default if there is no such...
Definition: CommandLine.hpp:421
std::string getFinalValue(size_t index=0) const
Get the final value with the specified index as a string or throw if no final value is available...
Definition: CommandLine.hpp:481
CommandLine & withOptionalFinalValue()
Specify that the command line arguments include one (SEV style) or one or more (SSV style) final stan...
Definition: CommandLine.hpp:156
CommandLine(CommandLineStyle style_=CommandLineStyle::Detect)
Create a command line parser with the specified style.
Definition: CommandLine.hpp:66
void parse(const std::vector< std::string > &args, bool ignoreFirst)
Parse the input arguments, which are supplied as a vector.
Definition: CommandLine.hpp:219
Switches that have a leading dash &#39;-&#39; character.
std::string getHelpText(size_t indent, size_t totalWidth, CommandLineStyle styleOverride=CommandLineStyle::Detect) const
Get the help text.
Definition: CommandLine.hpp:569
double getFinalValueAsDouble(size_t index=0) const
Get the final value as a double or throw if no final value is available.
Definition: CommandLine.hpp:513
Thrown when an illegal argument is passed to a function or method.
Definition: BalauException.hpp:138
unsigned short getOptionAsUnsignedShort(KeyT key) const
Get the specified option value as an unsigned short value.
Definition: CommandLine.hpp:309
Core includes, typedefs and functions.
unsigned short getOptionAsUnsignedShortOrDefault(KeyT key, unsigned short defaultValue) const
Get the specified option value as an unsigned short, or the specified default if there is no such opt...
Definition: CommandLine.hpp:377
Utilities for strings.
static bool startsWith(const StringT< CharT, T ... > &str, const PrefixT &prefix)
Does the string start with the specified prefix?
Definition: Strings.hpp:46
static std::string_view trim(const std::string_view &input)
Trim whitespace from the beginning and end of the supplied UTF-8 string.
Definition: Strings.hpp:706
size_t getFinalValueCount() const
Get the number of final values available.
Definition: CommandLine.hpp:258
double getFinalValueAsDoubleOrDefault(double defaultValue, size_t index=0) const
Get the final value as a double or return the supplied default if no final value is available...
Definition: CommandLine.hpp:548
std::string getOption(KeyT key) const
Get the specified option value as a UTF-8 string.
Definition: CommandLine.hpp:269
CommandLine & withOption(KeyT key, const std::string &shortSwitch, const std::string &longSwitch, bool hasValue, const std::string &documentation)
Specify an option that has a short style switch and a long style switch.
Definition: CommandLine.hpp:100
A compact command line argument parser.
Definition: CommandLine.hpp:60
CommandLine & withHelpOption(KeyT helpKey, const std::string &shortSwitch, const std::string &longSwitch, const std::string &helpDocumentation)
Specify the help command line short and long switches.
Definition: CommandLine.hpp:135
unsigned int getOptionAsUnsignedIntOrDefault(KeyT key, unsigned int defaultValue) const
Get the specified option value as an unsigned int, or the specified default if there is no such optio...
Definition: CommandLine.hpp:399
Switches that do not require a leading dash.
float getFinalValueAsFloat(size_t index=0) const
Get the final value as a float or throw if no final value is available.
Definition: CommandLine.hpp:502
std::string getFinalValueOrDefault(const std::string &defaultValue, size_t index=0) const
Get the final value as a string or return the supplied default if no final value is available...
Definition: CommandLine.hpp:523
long long getOptionAsLongOrDefault(KeyT key, long long defaultValue) const
Get the specified option value as a signed long long, or the specified default if there is no such op...
Definition: CommandLine.hpp:410
unsigned long long getOptionAsUnsignedLong(KeyT key) const
Get the specified option value as an unsigned long long value.
Definition: CommandLine.hpp:353
int getOptionAsInt(KeyT key) const
Get the specified option value as a signed integer value.
Definition: CommandLine.hpp:320
void parse(int argc, const char *argv[], bool ignoreFirst)
Parse the input arguments.
Definition: CommandLine.hpp:195
short getOptionAsShort(KeyT key) const
Get the specified option value as a signed short value.
Definition: CommandLine.hpp:298
CommandLineStyle
The command line style that the application uses.
Definition: CommandLine.hpp:33
Thrown when there is no option value available and one is required.
Definition: CommandLineExceptions.hpp:46
Thrown when there is no final value available and one is required.
Definition: CommandLineExceptions.hpp:54
float getOptionAsFloatOrDefault(KeyT key, float defaultValue) const
Get the specified option value as a float or return the supplied value if there is no such option...
Definition: CommandLine.hpp:458
Balau::U8String< AllocatorT > toString(LoggingLevel level)
Print the logging level as a UTF-8 string.
Definition: LoggingLevel.hpp:73
std::string getOptionOrDefault(KeyT key, std::string defaultValue) const
Get the specified option value as a string, or the specified default if there is no such option...
Definition: CommandLine.hpp:282
float getFinalValueAsFloatOrDefault(float defaultValue, size_t index=0) const
Get the final value as a float or return the supplied default if no final value is available...
Definition: CommandLine.hpp:537
Balau exceptions for the command line parser.