ProgramArguments.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /////////////////////////////////////////////////////////////////////////////////////////
  2. // This code contains NVIDIA Confidential Information and is disclosed
  3. // under the Mutual Non-Disclosure Agreement.
  4. //
  5. // Notice
  6. // ALL NVIDIA DESIGN SPECIFICATIONS AND CODE ("MATERIALS") ARE PROVIDED "AS IS" NVIDIA MAKES
  7. // NO REPRESENTATIONS, WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
  8. // THE MATERIALS, AND EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTIES OF NONINFRINGEMENT,
  9. // MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
  10. //
  11. // NVIDIA Corporation assumes no responsibility for the consequences of use of such
  12. // information or for any infringement of patents or other rights of third parties that may
  13. // result from its use. No license is granted by implication or otherwise under any patent
  14. // or patent rights of NVIDIA Corporation. No third party distribution is allowed unless
  15. // expressly authorized by NVIDIA. Details are subject to change without notice.
  16. // This code supersedes and replaces all information previously supplied.
  17. // NVIDIA Corporation products are not authorized for use as critical
  18. // components in life support devices or systems without express written approval of
  19. // NVIDIA Corporation.
  20. //
  21. // Copyright (c) 2014-2016 NVIDIA Corporation. All rights reserved.
  22. //
  23. // NVIDIA Corporation and its licensors retain all intellectual property and proprietary
  24. // rights in and to this software and related documentation and any modifications thereto.
  25. // Any use, reproduction, disclosure or distribution of this software and related
  26. // documentation without an express license agreement from NVIDIA Corporation is
  27. // strictly prohibited.
  28. //
  29. /////////////////////////////////////////////////////////////////////////////////////////
  30. #ifndef SAMPLES_COMMON_PROGRAMARGUMENTS_HPP__
  31. #define SAMPLES_COMMON_PROGRAMARGUMENTS_HPP__
  32. #include <map>
  33. #include <string>
  34. #include <vector>
  35. // EXAMPLE USECASE:
  36. //
  37. // ProgramArguments arguments(
  38. // { ProgramArguments::Option_t("optional_option", "with default value"),
  39. // ProgramArguments::Option_t("required_option")
  40. // }
  41. // );
  42. //
  43. // if (!arguments.parse(argc, argv)) exit(0); // Exit if not all require arguments are provided
  44. // printf("Program Arguments:\n%s\n", arguments.printList());
  45. //
  46. class ProgramArguments
  47. {
  48. public:
  49. struct Option_t {
  50. Option_t(const char *option_, const char *default_value_, const char *help_)
  51. {
  52. option = option_;
  53. default_value = default_value_;
  54. help = help_;
  55. required = false;
  56. parsed = false;
  57. value = default_value;
  58. }
  59. Option_t(const char *option_, std::nullptr_t, const char *help_)
  60. {
  61. option = option_;
  62. default_value = "";
  63. help = help_;
  64. required = true;
  65. parsed = false;
  66. value = "";
  67. }
  68. Option_t(const char *option_, const char *default_value_)
  69. {
  70. option = option_;
  71. default_value = default_value_;
  72. required = false;
  73. parsed = false;
  74. value = default_value;
  75. }
  76. Option_t(const char *option_)
  77. {
  78. option = option_;
  79. default_value = "";
  80. required = true;
  81. parsed = false;
  82. }
  83. std::string option;
  84. std::string default_value;
  85. std::string help;
  86. bool required;
  87. bool parsed;
  88. std::string value;
  89. };
  90. public:
  91. ProgramArguments() {}
  92. ProgramArguments(int argc, const char **argv, const std::vector<Option_t>& options, const char* description = nullptr);
  93. ProgramArguments(const std::vector<Option_t>& options);
  94. ~ProgramArguments();
  95. bool parse(const int argc, const char **argv);
  96. bool has(const char *name) const;
  97. bool enabled(const char *name) const;
  98. void addOption(const Option_t &newOption);
  99. const std::string &get(const char *name) const;
  100. void set(const char* option, const char* vaule);
  101. // Will be shown before the help text
  102. void setDescription(const char* description);
  103. /// Displays a message with info about the possible parameters
  104. void printHelp() const;
  105. /// Returns a string with info about the parameter values
  106. std::string printList() const;
  107. /// Returns all the parsed parameters as a single string
  108. std::string parameterString() const;
  109. private:
  110. static std::string m_empty;
  111. std::string m_description = {};
  112. std::map<std::string, Option_t> arguments;
  113. };
  114. #endif // SAMPLES_COMMON_PROGRAMARGUMENTS_HPP__