00001
00015 #ifndef _DLRUTILITIES_OPTIONPARSER_H_
00016 #define _DLRUTILITIES_OPTIONPARSER_H_
00017
00018 #include <map>
00019 #include <string>
00020 #include <vector>
00021 #include <dlrUtilities/optionDescription.h>
00022 #include <dlrUtilities/stringManipulation.h>
00023
00024 namespace dlr {
00025
00026 namespace utilities {
00027
00028
00125 class OptionParser {
00126 public:
00127
00167 OptionParser(bool allowExtraArguments = true,
00168 bool allowStackedShortOptions = true,
00169 bool allowOptionishArguments = false);
00170
00171
00202 explicit
00203 OptionParser(int exitCode,
00204 bool handleMinusMinusHelp = true,
00205 bool printUsage = true,
00206 bool allowExtraArguments = true,
00207 bool allowStackedShortOptions = true,
00208 bool allowOptionishArguments = false);
00209
00210
00214 ~OptionParser();
00215
00216
00245 void
00246 addOption(const std::string& name,
00247 const std::string& shortVersion,
00248 const std::string& longVersion,
00249 const std::string& docString,
00250 bool allowPartialMatch = true);
00251
00252
00292 void
00293 addOptionWithValue(const std::string& name,
00294 const std::string& shortVersion,
00295 const std::string& longVersion,
00296 const std::string& defaultValue,
00297 const std::string& docString,
00298 bool requireArgument = true,
00299 bool allowPartialMatch = true,
00300 bool allowOptionishValue = false);
00301
00302
00340 void
00341 addPositionalArgument(const std::string& name,
00342 const std::string& docString,
00343 bool isRequired = false,
00344 const std::string& defaultValue = "");
00345
00346
00368 template<class Type>
00369 Type
00370 convertValue(const std::string& name);
00371
00372
00394 template<class Type>
00395 Type
00396 convertValue(const std::string& name, int valueIndex);
00397
00398
00414 size_t
00415 getCount(const std::string& name) const;
00416
00417
00430 std::vector<std::string>
00431 getExtraPositionalArguments();
00432
00433
00449 std::string
00450 getOptionsDescription();
00451
00452
00466 std::string
00467 getUsage();
00468
00469
00491 std::string
00492 getValue(const std::string& name);
00493
00494
00517 std::string
00518 getValue(const std::string& name, int valueIndex);
00519
00520
00532 template<class Type>
00533 Type
00534 getValue(const std::string& name, type_tag<Type>);
00535
00536
00551 template<class Type>
00552 Type
00553 getValue(const std::string& name, int valueIndex, type_tag<Type>);
00554
00555
00579 void
00580 parseCommandLine(int argc, char* argv[]);
00581
00582
00583 private:
00584
00585
00586 enum ErrorBehavior {ExitOnError, ThrowOnError, UsageAndExitOnError};
00587
00588
00589
00590 bool
00591 findOptionDescription(const std::string& argument,
00592 OptionDescription& optionDescription,
00593 size_t& typedLength,
00594 bool& isShortMatch);
00595
00596
00597 void
00598 recordPositionalArgument(const std::string& argument);
00599
00600
00601
00602 bool m_allowExtraArguments;
00603 bool m_allowOptionishArguments;
00604 bool m_allowStackedShortOptions;
00605 ErrorBehavior m_errorBehavior;
00606 int m_exitCode;
00607 std::vector<std::string> m_extraArgumentValues;
00608 bool m_handleHelp;
00609 size_t m_numberOfPosArgRequired;
00610 size_t m_numberOfPosArgParsed;
00611 std::map<std::string, size_t> m_optionCounts;
00612 std::map<std::string, OptionDescription> m_optionDescriptions;
00613 std::map< std::string, std::vector<std::string> > m_optionValues;
00614 std::vector<std::string> m_positionalArgumentNames;
00615 std::vector<std::string> m_positionalArgumentDefaultValues;
00616 std::vector<std::string> m_positionalArgumentDocs;
00617 std::string m_programName;
00618
00619 };
00620
00621 }
00622
00623 }
00624
00625
00626
00627 namespace dlr {
00628
00629 namespace utilities {
00630
00631
00632 template<class Type>
00633 Type
00634 OptionParser::
00635 convertValue(const std::string& name)
00636 {
00637 return convertString<Type>(this->getValue(name));
00638 }
00639
00640
00641 template<class Type>
00642 Type
00643 OptionParser::
00644 convertValue(const std::string& name, int valueIndex)
00645 {
00646 return convertString<Type>(this->getValue(name, valueIndex));
00647 }
00648
00649
00650 template<class Type>
00651 Type
00652 OptionParser::
00653 getValue(const std::string& name, type_tag<Type>)
00654 {
00655 return this->convertValue<Type>(name);
00656 }
00657
00658
00659 template<class Type>
00660 Type
00661 OptionParser::
00662 getValue(const std::string& name, int valueIndex, type_tag<Type>)
00663 {
00664 return this->convertValue<Type>(name, valueIndex);
00665 }
00666
00667
00668 }
00669
00670 }
00671
00672 #endif // #ifndef _DLRUTILITES_OPTIONPARSER_H_