Photon Common Library 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
CommandLineArguments.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <string>
6#include <vector>
7#include <optional>
8#include <type_traits>
9
10namespace ph
11{
12
16{
17public:
18 CommandLineArguments(int argc, char* argv[]);
19
23 std::string getProgramName() const;
24
27 bool isEmpty() const;
28
32 std::string retrieveString(const std::string& defaultString = "");
33
37 std::vector<std::string> retrieveStrings(std::size_t numValues);
38
49 std::vector<std::string> retrieveOptionArguments(const std::string& optionPrefix);
50
57 std::vector<std::string> retrieveStrings(
58 const std::string& startingPrefix,
59 const std::string& endingPrefix,
60 bool shouldIncludeStart = true,
61 bool shouldIncludeEnd = true);
62
66 template<typename T>
67 T retrieveInt(T defaultInt = 0);
68
72 template<typename T>
73 T retrieveFloat(T defaultFloat = 0.0f);
74
75 template<typename T>
76 std::optional<T> retrieve();
77
78private:
79 std::string m_programName;
80 std::vector<std::string> m_arguments;
81};
82
83// In-header Implementations:
84
85inline std::string CommandLineArguments::getProgramName() const
86{
87 return m_programName;
88}
89
91{
92 return m_arguments.empty();
93}
94
95template<typename T>
97{
98 static_assert(std::is_integral_v<T>,
99 "expect argument type to be integer");
100
101 auto optInt = retrieve<T>();
102 return optInt ? *optInt : defaultInt;
103}
104
105template<typename T>
107{
108 static_assert(std::is_floating_point_v<T>,
109 "expect argument type to be floating-point");
110
111 auto optFloat = retrieve<T>();
112 return optFloat ? *optFloat : defaultFloat;
113}
114
115template<typename T>
116inline std::optional<T> CommandLineArguments::retrieve()
117{
118 if(isEmpty())
119 {
120 return std::nullopt;
121 }
122
123 std::string argument = m_arguments.front();
124 m_arguments.erase(m_arguments.begin());
125
126 if constexpr(std::is_same_v<T, std::string>)
127 {
128 return argument;
129 }
130 else
131 {
132 static_assert(std::is_integral_v<T> || std::is_floating_point_v<T>,
133 "expect argument type to be integer or floating-point");
134
135 return string_utils::parse_number<T>(argument);
136 }
137}
138
139}// end namespace ph
Helper for parsing command line arguments.
Definition CommandLineArguments.h:16
std::string retrieveString(const std::string &defaultString="")
Get the first argument passed in and remove it from the internal buffer.
Definition CommandLineArguments.cpp:24
CommandLineArguments(int argc, char *argv[])
Definition CommandLineArguments.cpp:9
bool isEmpty() const
Check if there are arguments yet to be parsed.
Definition CommandLineArguments.h:90
std::vector< std::string > retrieveOptionArguments(const std::string &optionPrefix)
Get the arguments for an option. This method assumes that the options specified are of the form "{- |...
Definition CommandLineArguments.cpp:40
std::string getProgramName() const
Get the program name.
Definition CommandLineArguments.h:85
std::optional< T > retrieve()
Definition CommandLineArguments.h:116
T retrieveFloat(T defaultFloat=0.0f)
Get a float from the arguments. Similar to retrieveString(const std::string&), while the result is co...
Definition CommandLineArguments.h:106
T retrieveInt(T defaultInt=0)
Get an integer from the arguments. Similar to retrieveString(const std::string&), while the result is...
Definition CommandLineArguments.h:96
std::vector< std::string > retrieveStrings(std::size_t numValues)
Get the first N arguments passed in and remove them from the internal buffer.
Definition CommandLineArguments.cpp:30
NumberType parse_number(const std::string_view numberStr)
Returns a number by processing its string representation. Accepts all types supported by parse_float(...
Definition string_utils.h:447
The root for all renderer implementations.
Definition assertion.h:9
String manipulation helpers.