Plasma Engine  2.0
Loading...
Searching...
No Matches
CommandLineOptions.h
1#pragma once
2
3#include <Foundation/Basics.h>
4#include <Foundation/Math/Math.h>
5#include <Foundation/Utilities/CommandLineUtils.h>
6#include <Foundation/Utilities/EnumerableClass.h>
7
10
20class PL_FOUNDATION_DLL plCommandLineOption : public plEnumerable<plCommandLineOption>
21{
22 PL_DECLARE_ENUMERABLE_CLASS(plCommandLineOption);
23
24public:
26 {
27 Always,
28 IfHelpRequested
29 };
30
32 enum class LogMode
33 {
34 Never,
35 FirstTime,
36 FirstTimeIfSpecified,
37 Always,
38 AlwaysIfSpecified,
39 };
40
42 static bool IsHelpRequested(const plCommandLineUtils* pUtils = plCommandLineUtils::GetGlobalInstance()); // [tested]
43
47 static plResult RequireOptions(plStringView sRequiredOptions, plString* pMissingOption = nullptr, const plCommandLineUtils* pUtils = plCommandLineUtils::GetGlobalInstance()); // [tested]
48
54 static bool LogAvailableOptions(LogAvailableModes mode, plStringView sGroupFilter = {}, const plCommandLineUtils* pUtils = plCommandLineUtils::GetGlobalInstance()); // [tested]
55
57 static bool LogAvailableOptionsToBuffer(plStringBuilder& out_sBuffer, LogAvailableModes mode, plStringView sGroupFilter = {}, const plCommandLineUtils* pUtils = plCommandLineUtils::GetGlobalInstance()); // [tested]
58
59public:
63 plCommandLineOption(plStringView sSortingGroup) { m_sSortingGroup = sSortingGroup; }
64
66 virtual void GetSortingGroup(plStringBuilder& ref_sOut) const;
67
70 virtual void GetOptions(plStringBuilder& ref_sOut) const = 0;
71
73 void GetSplitOptions(plStringBuilder& out_sAll, plDynamicArray<plStringView>& ref_splitOptions) const;
74
76 virtual void GetParamShortDesc(plStringBuilder& ref_sOut) const = 0;
77
79 virtual void GetParamDefaultValueDesc(plStringBuilder& ref_sOut) const = 0;
80
84 virtual void GetLongDesc(plStringBuilder& ref_sOut) const = 0;
85
87 virtual plStringView GetType() = 0;
88
89protected:
90 plStringView m_sSortingGroup;
91};
92
96
102class PL_FOUNDATION_DLL plCommandLineOptionDoc : public plCommandLineOption
103{
104public:
105 plCommandLineOptionDoc(plStringView sSortingGroup, plStringView sArgument, plStringView sParamShortDesc, plStringView sLongDesc, plStringView sDefaultValue, bool bCaseSensitive = false);
106
107 virtual void GetOptions(plStringBuilder& ref_sOut) const override; // [tested]
108
109 virtual void GetParamShortDesc(plStringBuilder& ref_sOut) const override; // [tested]
110
111 virtual void GetParamDefaultValueDesc(plStringBuilder& ref_sOut) const override; // [tested]
112
113 virtual void GetLongDesc(plStringBuilder& ref_sOut) const override; // [tested]
114
116 virtual plStringView GetType() override { return "Doc"; }
117
119 bool IsOptionSpecified(plStringBuilder* out_pWhich = nullptr, const plCommandLineUtils* pUtils = plCommandLineUtils::GetGlobalInstance()) const; // [tested]
120
121protected:
122 bool ShouldLog(LogMode mode, bool bWasSpecified) const;
123 void LogOption(plStringView sOption, plStringView sValue, bool bWasSpecified) const;
124
125 plStringView m_sArgument;
126 plStringView m_sParamShortDesc;
127 plStringView m_sParamDefaultValue;
128 plStringView m_sLongDesc;
129 bool m_bCaseSensitive = false;
130 mutable bool m_bLoggedOnce = false;
131};
132
136
138class PL_FOUNDATION_DLL plCommandLineOptionBool : public plCommandLineOptionDoc
139{
140public:
141 plCommandLineOptionBool(plStringView sSortingGroup, plStringView sArgument, plStringView sLongDesc, bool bDefaultValue, bool bCaseSensitive = false);
142
144 bool GetOptionValue(LogMode logMode, const plCommandLineUtils* pUtils = plCommandLineUtils::GetGlobalInstance()) const; // [tested]
145
147 void SetDefaultValue(bool value)
148 {
149 m_bDefaultValue = value;
150 }
151
153 bool GetDefaultValue() const { return m_bDefaultValue; }
154
156 virtual plStringView GetType() override { return "Bool"; }
157
158protected:
159 bool m_bDefaultValue = false;
160};
161
165
170class PL_FOUNDATION_DLL plCommandLineOptionInt : public plCommandLineOptionDoc
171{
172public:
173 plCommandLineOptionInt(plStringView sSortingGroup, plStringView sArgument, plStringView sLongDesc, int iDefaultValue, int iMinValue = plMath::MinValue<int>(), int iMaxValue = plMath::MaxValue<int>(), bool bCaseSensitive = false);
174
175 virtual void GetParamDefaultValueDesc(plStringBuilder& ref_sOut) const override; // [tested]
176
177 virtual void GetParamShortDesc(plStringBuilder& ref_sOut) const override; // [tested]
178
180 int GetOptionValue(LogMode logMode, const plCommandLineUtils* pUtils = plCommandLineUtils::GetGlobalInstance()) const; // [tested]
181
183 void SetDefaultValue(plInt32 value)
184 {
185 m_iDefaultValue = value;
186 }
187
189 virtual plStringView GetType() override { return "Int"; }
190
192 plInt32 GetMinValue() const { return m_iMinValue; }
193
195 plInt32 GetMaxValue() const { return m_iMaxValue; }
196
198 plInt32 GetDefaultValue() const { return m_iDefaultValue; }
199
200protected:
201 plInt32 m_iDefaultValue = 0;
202 plInt32 m_iMinValue = 0;
203 plInt32 m_iMaxValue = 0;
204};
205
209
214class PL_FOUNDATION_DLL plCommandLineOptionFloat : public plCommandLineOptionDoc
215{
216public:
217 plCommandLineOptionFloat(plStringView sSortingGroup, plStringView sArgument, plStringView sLongDesc, float fDefaultValue, float fMinValue = plMath::MinValue<float>(), float fMaxValue = plMath::MaxValue<float>(), bool bCaseSensitive = false);
218
219 virtual void GetParamDefaultValueDesc(plStringBuilder& ref_sOut) const override; // [tested]
220
221 virtual void GetParamShortDesc(plStringBuilder& ref_sOut) const override; // [tested]
222
224 float GetOptionValue(LogMode logMode, const plCommandLineUtils* pUtils = plCommandLineUtils::GetGlobalInstance()) const; // [tested]
225
227 void SetDefaultValue(float value)
228 {
229 m_fDefaultValue = value;
230 }
231
233 virtual plStringView GetType() override { return "Float"; }
234
236 float GetMinValue() const { return m_fMinValue; }
237
239 float GetMaxValue() const { return m_fMaxValue; }
240
242 float GetDefaultValue() const { return m_fDefaultValue; }
243
244protected:
245 float m_fDefaultValue = 0;
246 float m_fMinValue = 0;
247 float m_fMaxValue = 0;
248};
249
253
255class PL_FOUNDATION_DLL plCommandLineOptionString : public plCommandLineOptionDoc
256{
257public:
258 plCommandLineOptionString(plStringView sSortingGroup, plStringView sArgument, plStringView sLongDesc, plStringView sDefaultValue, bool bCaseSensitive = false);
259
261 plStringView GetOptionValue(LogMode logMode, const plCommandLineUtils* pUtils = plCommandLineUtils::GetGlobalInstance()) const; // [tested]
262
265 {
266 m_sDefaultValue = sValue;
267 }
268
270 plStringView GetDefaultValue() const { return m_sDefaultValue; }
271
273 virtual plStringView GetType() override { return "String"; }
274
275protected:
276 plStringView m_sDefaultValue;
277};
278
282
284class PL_FOUNDATION_DLL plCommandLineOptionPath : public plCommandLineOptionDoc
285{
286public:
287 plCommandLineOptionPath(plStringView sSortingGroup, plStringView sArgument, plStringView sLongDesc, plStringView sDefaultValue, bool bCaseSensitive = false);
288
290 plString GetOptionValue(LogMode logMode, const plCommandLineUtils* pUtils = plCommandLineUtils::GetGlobalInstance()) const; // [tested]
291
294 {
295 m_sDefaultValue = sValue;
296 }
297
299 plStringView GetDefaultValue() const { return m_sDefaultValue; }
300
302 virtual plStringView GetType() override { return "Path"; }
303
304protected:
305 plStringView m_sDefaultValue;
306};
307
311
321class PL_FOUNDATION_DLL plCommandLineOptionEnum : public plCommandLineOptionDoc
322{
323public:
324 plCommandLineOptionEnum(plStringView sSortingGroup, plStringView sArgument, plStringView sLongDesc, plStringView sEnumKeysAndValues, plInt32 iDefaultValue, bool bCaseSensitive = false);
325
327 plInt32 GetOptionValue(LogMode logMode, const plCommandLineUtils* pUtils = plCommandLineUtils::GetGlobalInstance()) const; // [tested]
328
329 virtual void GetParamShortDesc(plStringBuilder& ref_sOut) const override; // [tested]
330
331 virtual void GetParamDefaultValueDesc(plStringBuilder& ref_sOut) const override; // [tested]
332
334 {
335 plStringView m_Key;
336 plInt32 m_iValue = 0;
337 };
338
340 void GetEnumKeysAndValues(plDynamicArray<EnumKeyValue>& out_keysAndValues) const;
341
343 void SetDefaultValue(plInt32 value)
344 {
345 m_iDefaultValue = value;
346 }
347
349 plInt32 GetDefaultValue() const { return m_iDefaultValue; }
350
352 virtual plStringView GetType() override { return "Enum"; }
353
354protected:
355 plInt32 m_iDefaultValue = 0;
356 plStringView m_sEnumKeysAndValues;
357};
This command line option exposes simple on/off switches.
Definition CommandLineOptions.h:139
bool GetDefaultValue() const
Returns the default value.
Definition CommandLineOptions.h:153
void SetDefaultValue(bool value)
Modifies the default value.
Definition CommandLineOptions.h:147
virtual plStringView GetType() override
Returns "Bool".
Definition CommandLineOptions.h:156
plCommandLineOptionDoc can be used to document a command line option whose logic might be more comple...
Definition CommandLineOptions.h:103
virtual plStringView GetType() override
Returns "Doc".
Definition CommandLineOptions.h:116
An 'enum' option is a string option that only allows certain phrases ('keys').
Definition CommandLineOptions.h:322
plInt32 GetDefaultValue() const
Returns the default value.
Definition CommandLineOptions.h:349
void SetDefaultValue(plInt32 value)
Modifies the default value.
Definition CommandLineOptions.h:343
virtual plStringView GetType() override
Returns "Enum".
Definition CommandLineOptions.h:352
This command line option exposes float values, optionally with a min/max range.
Definition CommandLineOptions.h:215
void SetDefaultValue(float value)
Modifies the default value.
Definition CommandLineOptions.h:227
virtual plStringView GetType() override
Returns "Float".
Definition CommandLineOptions.h:233
float GetDefaultValue() const
Returns the default value.
Definition CommandLineOptions.h:242
float GetMinValue() const
Returns the minimum value.
Definition CommandLineOptions.h:236
float GetMaxValue() const
Returns the maximum value.
Definition CommandLineOptions.h:239
plCommandLineOption (and derived types) are used to define options that the application supports.
Definition CommandLineOptions.h:21
virtual void GetLongDesc(plStringBuilder &ref_sOut) const =0
Returns a proper description of the option.
virtual void GetParamDefaultValueDesc(plStringBuilder &ref_sOut) const =0
Returns a very short string for the options default value. For example "0" or "auto".
virtual void GetParamShortDesc(plStringBuilder &ref_sOut) const =0
Returns a very short description of the option (type). For example "<int>" or "<enum>".
LogAvailableModes
Definition CommandLineOptions.h:26
virtual plStringView GetType()=0
Returns a string indicating the exact implementation type.
plCommandLineOption(plStringView sSortingGroup)
Definition CommandLineOptions.h:63
virtual void GetOptions(plStringBuilder &ref_sOut) const =0
Writes all the supported options (e.g. '-arg') to 'out'. If more than one option is allowed,...
LogMode
Describes whether the value of an option (and whether something went wrong), should be printed to plL...
Definition CommandLineOptions.h:33
This command line option exposes integer values, optionally with a min/max range.
Definition CommandLineOptions.h:171
plInt32 GetMaxValue() const
Returns the maximum value.
Definition CommandLineOptions.h:195
plInt32 GetDefaultValue() const
Returns the default value.
Definition CommandLineOptions.h:198
void SetDefaultValue(plInt32 value)
Modifies the default value.
Definition CommandLineOptions.h:183
virtual plStringView GetType() override
Returns "Int".
Definition CommandLineOptions.h:189
plInt32 GetMinValue() const
Returns the minimum value.
Definition CommandLineOptions.h:192
This command line option exposes absolute paths. If the user provides a relative path,...
Definition CommandLineOptions.h:285
void SetDefaultValue(plStringView sValue)
Modifies the default value.
Definition CommandLineOptions.h:293
plStringView GetDefaultValue() const
Returns the default value.
Definition CommandLineOptions.h:299
virtual plStringView GetType() override
Returns "Path".
Definition CommandLineOptions.h:302
This command line option exposes simple string values.
Definition CommandLineOptions.h:256
virtual plStringView GetType() override
Returns "String".
Definition CommandLineOptions.h:273
void SetDefaultValue(plStringView sValue)
Modifies the default value.
Definition CommandLineOptions.h:264
plStringView GetDefaultValue() const
Returns the default value.
Definition CommandLineOptions.h:270
This is a helper class to parse command lines.
Definition CommandLineUtils.h:13
static plCommandLineUtils * GetGlobalInstance()
Returns one global instance of plCommandLineUtils.
Definition CommandLineUtils.cpp:14
Definition DynamicArray.h:81
Base class to add the ability to another class to enumerate all active instance of it,...
Definition EnumerableClass.h:28
Base class for all logging classes.
Definition Log.h:77
plStringBuilder is a class that is meant for creating and modifying strings.
Definition StringBuilder.h:35
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
constexpr TYPE MaxValue()
Returns the largest possible positive value (that is not infinity).
constexpr TYPE MinValue()
Returns the smallest possible value (that is not -infinity). Usually zero or -MaxValue()....
Definition CommandLineOptions.h:334
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54