![]() |
XII Release 0.1.0
|
This is a helper class to parse command lines. More...
#include <CommandLineUtils.h>
Public Types | |
enum | ArgMode { UseArgcArgv , PreferOsArgs } |
Public Member Functions | |
void | SetCommandLine (xiiUInt32 uiArgc, const char **pArgv, ArgMode mode=UseArgcArgv) |
Initializes xiiCommandLineUtils from the parameter arguments that were passed to the application. | |
void | SetCommandLine (xiiUInt32 uiArgc, char **pArgv, ArgMode mode=UseArgcArgv) |
Overload for non-const argv versions. | |
void | SetCommandLine (xiiArrayPtr< xiiString > commands) |
Initializes xiiCommandLineUtils from a list of already split up commands. | |
void | SetCommandLine () |
Initializes xiiCommandLineUtils by querying the command line parameters directly from the OS. | |
const xiiDynamicArray< xiiString > & | GetCommandLineArray () const |
Returns the split up command line. | |
xiiString | GetCommandLineString () const |
Assembles the original command line from the split up string representation. | |
xiiUInt32 | GetParameterCount () const |
Returns the total number of command line parameters (excluding the program path, which is often passed as the first parameter). | |
const xiiString & | GetParameter (xiiUInt32 uiParam) const |
Returns the n-th parameter string that was passed to the application. | |
xiiInt32 | GetOptionIndex (xiiStringView sOption, bool bCaseSensitive=false) const |
Returns the index at which the given option string can be found in the parameter list. | |
bool | HasOption (xiiStringView sOption, bool bCaseSensitive=false) const |
Returns whether the requested option is specified, at all. | |
xiiUInt32 | GetStringOptionArguments (xiiStringView sOption, bool bCaseSensitive=false) const |
Returns how many arguments follow behind the option with the name szOption. | |
xiiStringView | GetStringOption (xiiStringView sOption, xiiUInt32 uiArgument=0, xiiStringView sDefault={}, bool bCaseSensitive=false) const |
Returns the n-th parameter to the command line option with the name szOption. | |
const xiiString | GetAbsolutePathOption (xiiStringView sOption, xiiUInt32 uiArgument=0, xiiStringView sDefault={}, bool bCaseSensitive=false) const |
Similar to GetStringOption() but assumes that the strings represent paths and concatenates the current working directory if a relative path is given. | |
bool | GetBoolOption (xiiStringView sOption, bool bDefault=false, bool bCaseSensitive=false) const |
Returns a boolean interpretation of the option szOption or bDefault if it cannot be found. | |
xiiInt32 | GetIntOption (xiiStringView sOption, xiiInt32 iDefault=0, bool bCaseSensitive=false) const |
Returns an integer interpretation of the option szOption or iDefault if it cannot be found. | |
xiiUInt32 | GetUIntOption (xiiStringView sOption, xiiUInt32 uiDefault=0, bool bCaseSensitive=false) const |
Same as GetIntOption() but assumes the value is a uint32. | |
double | GetFloatOption (xiiStringView sOption, double fDefault=0.0, bool bCaseSensitive=false) const |
Returns a float interpretation of the option szOption or fDefault if it cannot be found. | |
void | InjectCustomArgument (xiiStringView sArgument) |
This allows to append an argument programmatically, that wasn't actually set through the command line. | |
Static Public Member Functions | |
static xiiCommandLineUtils * | GetGlobalInstance () |
Returns one global instance of xiiCommandLineUtils. | |
static void | SplitCommandLineString (const char *szCommandString, bool bAddExecutableDir, xiiDynamicArray< xiiString > &out_args, xiiDynamicArray< const char * > &out_argsV) |
Splits a string into the classic argc/argv string. | |
This is a helper class to parse command lines.
Initialize it using SetCommandLine(). Then query for command line options using GetStringOption(), GetBoolOption(), GetIntOption() or GetFloatOption()
const xiiString xiiCommandLineUtils::GetAbsolutePathOption | ( | xiiStringView | sOption, |
xiiUInt32 | uiArgument = 0, | ||
xiiStringView | sDefault = {}, | ||
bool | bCaseSensitive = false ) const |
Similar to GetStringOption() but assumes that the strings represent paths and concatenates the current working directory if a relative path is given.
To check how many arguments are available, use GetStringOptionArguments().
If szDefault is empty and the user did not provide this option, then the result will also be the empty string. If szDefault is a relative path, it will be concatenated with the CWD just as any user provided option would.
bool xiiCommandLineUtils::GetBoolOption | ( | xiiStringView | sOption, |
bool | bDefault = false, | ||
bool | bCaseSensitive = false ) const |
Returns a boolean interpretation of the option szOption or bDefault if it cannot be found.
szOption | The name of the option to search for. All option-names must start with a hyphen. |
bDefault | The default value to use when no other value can be derived. |
bCaseSensitive | Whether it should be searched case-sensitive for the option with name szOption. |
double xiiCommandLineUtils::GetFloatOption | ( | xiiStringView | sOption, |
double | fDefault = 0.0, | ||
bool | bCaseSensitive = false ) const |
Returns a float interpretation of the option szOption or fDefault if it cannot be found.
szOption | The name of the option to search for. All option-names must start with a hyphen. |
fDefault | The default value to use when no other value can be derived. |
bCaseSensitive | Whether it should be searched case-sensitive for the option with name szOption. |
xiiInt32 xiiCommandLineUtils::GetIntOption | ( | xiiStringView | sOption, |
xiiInt32 | iDefault = 0, | ||
bool | bCaseSensitive = false ) const |
Returns an integer interpretation of the option szOption or iDefault if it cannot be found.
szOption | The name of the option to search for. All option-names must start with a hyphen. |
iDefault | The default value to use when no other value can be derived. |
bCaseSensitive | Whether it should be searched case-sensitive for the option with name szOption. |
xiiInt32 xiiCommandLineUtils::GetOptionIndex | ( | xiiStringView | sOption, |
bool | bCaseSensitive = false ) const |
Returns the index at which the given option string can be found in the parameter list.
szOption | The name of the command line option. Must start with a hyphen (-) |
bCaseSensitive | Whether the option name szOption shall be searched case sensitive. |
xiiStringView xiiCommandLineUtils::GetStringOption | ( | xiiStringView | sOption, |
xiiUInt32 | uiArgument = 0, | ||
xiiStringView | sDefault = {}, | ||
bool | bCaseSensitive = false ) const |
Returns the n-th parameter to the command line option with the name szOption.
If the option does not exist or does not have that many parameters, szDefault is returned.
xiiUInt32 xiiCommandLineUtils::GetStringOptionArguments | ( | xiiStringView | sOption, |
bool | bCaseSensitive = false ) const |
Returns how many arguments follow behind the option with the name szOption.
Everything that does not start with a hyphen is considered to be an additional parameter for the option.
void xiiCommandLineUtils::InjectCustomArgument | ( | xiiStringView | sArgument | ) |
This allows to append an argument programmatically, that wasn't actually set through the command line.
This can be useful when the command-line is a method to configure something, which might be hidden away in a plugin, and we have no other easy way to configure it.
Be aware that each call to this function is like one command line argument. Therefore to add "-arg test", call it two times, once with "-arg", once with "test". To add a string with spaces, call it once, but do not wrap the string in artificial quotes.
void xiiCommandLineUtils::SetCommandLine | ( | ) |
Initializes xiiCommandLineUtils by querying the command line parameters directly from the OS.
This function is not available on all platforms.
|
static |
Splits a string into the classic argc/argv string.
Useful for platforms where command line args come in as a single string.
addExecutableDir | Adds executable path as first parameter (just as it would normally be in 'int main(argc, argv)'). |