ezEngine  Release 25.03
ezCommandLineUtils Class Reference

This is a helper class to parse command lines. More...

#include <CommandLineUtils.h>

Public Types

enum  ArgMode { UseArgcArgv, PreferOsArgs }
 

Public Member Functions

void SetCommandLine (ezUInt32 uiArgc, const char **pArgv, ArgMode mode=UseArgcArgv)
 Initializes ezCommandLineUtils from the parameter arguments that were passed to the application.
 
void SetCommandLine (ezUInt32 uiArgc, char **pArgv, ArgMode mode=UseArgcArgv)
 Overload for non-const argv versions.
 
void SetCommandLine (ezArrayPtr< ezString > commands)
 Initializes ezCommandLineUtils from a list of already split up commands.
 
const ezDynamicArray< ezString > & GetCommandLineArray () const
 Returns the split up command line.
 
ezString GetCommandLineString () const
 Assembles the original command line from the split up string representation.
 
ezUInt32 GetParameterCount () const
 Returns the total number of command line parameters (excluding the program path, which is often passed as the first parameter).
 
const ezStringGetParameter (ezUInt32 uiParam) const
 Returns the n-th parameter string that was passed to the application.
 
ezInt32 GetOptionIndex (ezStringView sOption, bool bCaseSensitive=false) const
 Returns the index at which the given option string can be found in the parameter list. More...
 
bool HasOption (ezStringView sOption, bool bCaseSensitive=false) const
 Returns whether the requested option is specified, at all.
 
ezUInt32 GetStringOptionArguments (ezStringView sOption, bool bCaseSensitive=false) const
 Returns how many arguments follow behind the option with the name szOption. More...
 
ezStringView GetStringOption (ezStringView sOption, ezUInt32 uiArgument=0, ezStringView sDefault={}, bool bCaseSensitive=false) const
 Returns the n-th parameter to the command line option with the name szOption. More...
 
const ezString GetAbsolutePathOption (ezStringView sOption, ezUInt32 uiArgument=0, ezStringView 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. More...
 
bool GetBoolOption (ezStringView sOption, bool bDefault=false, bool bCaseSensitive=false) const
 Returns a boolean interpretation of the option szOption or bDefault if it cannot be found. More...
 
ezInt32 GetIntOption (ezStringView sOption, ezInt32 iDefault=0, bool bCaseSensitive=false) const
 Returns an integer interpretation of the option szOption or iDefault if it cannot be found. More...
 
ezUInt32 GetUIntOption (ezStringView sOption, ezUInt32 uiDefault=0, bool bCaseSensitive=false) const
 Same as GetIntOption() but assumes the value is a uint32.
 
double GetFloatOption (ezStringView sOption, double fDefault=0.0, bool bCaseSensitive=false) const
 Returns a float interpretation of the option szOption or fDefault if it cannot be found. More...
 
void InjectCustomArgument (ezStringView sArgument)
 This allows to append an argument programmatically, that wasn't actually set through the command line. More...
 

Static Public Member Functions

static ezCommandLineUtilsGetGlobalInstance ()
 Returns one global instance of ezCommandLineUtils.
 
static void SplitCommandLineString (const char *szCommandString, bool bAddExecutableDir, ezDynamicArray< ezString > &out_args, ezDynamicArray< const char * > &out_argsV)
 Splits a string into the classic argc/argv string. More...
 

Detailed Description

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()

Member Enumeration Documentation

◆ ArgMode

Enumerator
UseArgcArgv 

Use the passed in argc/argv values as they are passed in.

PreferOsArgs 

On Windows, ignore argc/argv and instead query the global arguments from the OS. Necessary to properly support Unicode strings in arguments.

Member Function Documentation

◆ GetAbsolutePathOption()

const ezString ezCommandLineUtils::GetAbsolutePathOption ( ezStringView  sOption,
ezUInt32  uiArgument = 0,
ezStringView  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().

Note
This function always returns absolute or rooted paths, never relative ones. If relative paths are supposed to be allowed, use GetStringOption() instead.

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.

◆ GetBoolOption()

bool ezCommandLineUtils::GetBoolOption ( ezStringView  sOption,
bool  bDefault = false,
bool  bCaseSensitive = false 
) const

Returns a boolean interpretation of the option szOption or bDefault if it cannot be found.

Parameters
szOptionThe name of the option to search for. All option-names must start with a hyphen.
bDefaultThe default value to use when no other value can be derived.
bCaseSensitiveWhether it should be searched case-sensitive for the option with name szOption.
Returns
If an option with the name szOption can be found, which has no parameters, it is interpreted as 'true'. If there is one parameter following, it is interpreted using ezConversionUtils::StringToBool(). If that conversion fails, bDefault is returned.

◆ GetFloatOption()

double ezCommandLineUtils::GetFloatOption ( ezStringView  sOption,
double  fDefault = 0.0,
bool  bCaseSensitive = false 
) const

Returns a float interpretation of the option szOption or fDefault if it cannot be found.

Parameters
szOptionThe name of the option to search for. All option-names must start with a hyphen.
fDefaultThe default value to use when no other value can be derived.
bCaseSensitiveWhether it should be searched case-sensitive for the option with name szOption.
Returns
If an option with the name szOption can be found, and there is one parameter following, it is interpreted using ezConversionUtils::StringToFloat(). If that conversion fails or there is no such option or no parameter follows it, fDefault is returned.

◆ GetIntOption()

ezInt32 ezCommandLineUtils::GetIntOption ( ezStringView  sOption,
ezInt32  iDefault = 0,
bool  bCaseSensitive = false 
) const

Returns an integer interpretation of the option szOption or iDefault if it cannot be found.

Parameters
szOptionThe name of the option to search for. All option-names must start with a hyphen.
iDefaultThe default value to use when no other value can be derived.
bCaseSensitiveWhether it should be searched case-sensitive for the option with name szOption.
Returns
If an option with the name szOption can be found, and there is one parameter following, it is interpreted using ezConversionUtils::StringToInt(). If that conversion fails or there is no such option or no parameter follows it, iDefault is returned.

◆ GetOptionIndex()

ezInt32 ezCommandLineUtils::GetOptionIndex ( ezStringView  sOption,
bool  bCaseSensitive = false 
) const

Returns the index at which the given option string can be found in the parameter list.

Parameters
szOptionThe name of the command line option. Must start with a hyphen (-)
bCaseSensitiveWhether the option name szOption shall be searched case sensitive.
Returns
-1 When no option with the given name is found. Otherwise the index at which the option can be found. This can be passed to GetParameter() or GetStringOptionArguments().

◆ GetStringOption()

ezStringView ezCommandLineUtils::GetStringOption ( ezStringView  sOption,
ezUInt32  uiArgument = 0,
ezStringView  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.

◆ GetStringOptionArguments()

ezUInt32 ezCommandLineUtils::GetStringOptionArguments ( ezStringView  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.

◆ InjectCustomArgument()

void ezCommandLineUtils::InjectCustomArgument ( ezStringView  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.

◆ SplitCommandLineString()

void ezCommandLineUtils::SplitCommandLineString ( const char *  szCommandString,
bool  bAddExecutableDir,
ezDynamicArray< ezString > &  out_args,
ezDynamicArray< const char * > &  out_argsV 
)
static

Splits a string into the classic argc/argv string.

Useful for platforms where command line args come in as a single string.

Parameters
addExecutableDirAdds executable path as first parameter (just as it would normally be in 'int main(argc, argv)').

The documentation for this class was generated from the following files: