Open 3D Engine AzCore API Reference 23.10.0
O3DE is an open-source, fully-featured, high-fidelity, modular 3D engine for building games and simulations, available to every industry.
AZ::CommandLine Class Reference

#include <CommandLine.h>

Classes

struct  CommandArgument
 

Public Types

using ParamContainer = AZStd::vector< AZStd::string >
 
using ArgumentVector = AZStd::vector< CommandArgument >
 

Public Member Functions

 AZ_CLASS_ALLOCATOR (CommandLine, AZ::SystemAllocator)
 
 CommandLine (AZStd::string_view commandLineOptionPrefix)
 
void Parse (int argc, char **argv)
 
void Parse (AZStd::span< const AZStd::string_view > commandLine)
 
void Parse (const ParamContainer &commandLine)
 
void Dump (ParamContainer &commandLineDumpOutput) const
 
bool HasSwitch (AZStd::string_view switchName) const
 
AZStd::size_t GetNumSwitchValues (AZStd::string_view switchName) const
 
const AZStd::stringGetSwitchValue (AZStd::string_view switchName) const
 
const AZStd::stringGetSwitchValue (AZStd::string_view switchName, AZStd::size_t index) const
 
AZStd::size_t GetNumMiscValues () const
 
const AZStd::stringGetMiscValue (AZStd::size_t index) const
 
bool empty () const
 
auto size () const -> ArgumentVector::size_type
 
auto begin () -> ArgumentVector::iterator
 
auto begin () const -> ArgumentVector::const_iterator
 
auto cbegin () const -> ArgumentVector::const_iterator
 
auto end () -> ArgumentVector::iterator
 
auto end () const -> ArgumentVector::const_iterator
 
auto cend () const -> ArgumentVector::const_iterator
 
auto rbegin () -> ArgumentVector::reverse_iterator
 
auto rbegin () const -> ArgumentVector::const_reverse_iterator
 
auto crbegin () const -> ArgumentVector::const_reverse_iterator
 
auto rend () -> ArgumentVector::reverse_iterator
 
auto rend () const -> ArgumentVector::const_reverse_iterator
 
auto crend () const -> ArgumentVector::const_reverse_iterator
 

Detailed Description

Given a commandline, this allows uniform parsing of it into parameter values and extra values. When parsed, the commandline becomes a series of "options" or "extra values" For example, a option may be specified as either /optionName=value1[,value2...] /optionname value[,value2...] –optionname value[,value2...] or other combinations of the above You may NOT use a colon as a option separator since file names may easily contain them. any additional parameters which are not associated with any option are considered "misc values"

NOTE: "flags" options which doesn't specify a value need care when using This is because the Parse() methods don't accept external metadata indicating actions to perform on flag arguments unlike python argparse https://docs.python.org/3/library/argparse.html#action For example the command line of app.exe --verbose <PositionalArg> would parse <PositionalArg> as the value of the "verbose" flag The workaround is to either

  1. specify the flag options at the end of the command line app.exe <PositionalArg> --verbose
  2. specify second option right after the previous flag option with and add a value to it app.exe --verbose --count 2 <PositionalArg>
  3. specify a value for the 'verbose'option here only the 1 is parse as part of the verbose option, the <PositionalArg> separated by whitespace is a new argument app.exe --verbose 1 <PositionalArg> or app.exe --verbose=1 <PositionalArg>

Constructor & Destructor Documentation

◆ CommandLine()

AZ::CommandLine::CommandLine ( AZStd::string_view  commandLineOptionPrefix)

Initializes a CommandLine instance which uses the provided commandLineOptionPreix for parsing switches

Member Function Documentation

◆ Dump()

void AZ::CommandLine::Dump ( ParamContainer commandLineDumpOutput) const

Will dump command line parameters from the CommandLine in a format such that switches are prefixed with the option prefix followed by their value(s) which are comma separated The result of this function can be supplied to Parse() to re-create an equivalent command line object Ex, If the command line has the current list of parsed miscellaneous values and switches of MiscValue = ["Foo", "Bat"] Switches = ["GameFolder" : [], "RemoteIp" : ["10.0.0.1"], "ScanFolders" : ["\a\b\c", "\d\e\f"] CommandLineOptionPrefix = "-/"

Then the resulting dumped value would be Dump = ["Foo", "Bat", "-GameFolder", "-RemoteIp", "10.0.0.1", "-ScanFolders", "\a\b\c", "-ScanFolders", "\d\e\f"]

◆ GetNumSwitchValues()

AZStd::size_t AZ::CommandLine::GetNumSwitchValues ( AZStd::string_view  switchName) const

Get the number of values for the given switch.

Returns
0 if the switch does not exist, otherwise the total number of values that appear after that switch

◆ GetSwitchValue() [1/2]

const AZStd::string & AZ::CommandLine::GetSwitchValue ( AZStd::string_view  switchName) const

Get the actual value of a switch

Parameters
switchNameThe switch to search for
Returns
The last value of the switch. This is follows the standard command line workflow that the last one wins

◆ GetSwitchValue() [2/2]

const AZStd::string & AZ::CommandLine::GetSwitchValue ( AZStd::string_view  switchName,
AZStd::size_t  index 
) const

Get the actual value of a switch

Parameters
switchNameThe switch to search for
indexThe 0-based index to retrieve the switch value for
Returns
The value at that index. This will Assert if you attempt to index out of bounds

◆ HasSwitch()

bool AZ::CommandLine::HasSwitch ( AZStd::string_view  switchName) const

Determines whether a switch is present in the command line

◆ Parse() [1/2]

void AZ::CommandLine::Parse ( AZStd::span< const AZStd::string_view commandLine)

Parses each element of the command line as parameter. Unlike the ARGC/ARGV version above, this function doesn't skip over the first parameter It allows for round trip conversion with the Dump() method

◆ Parse() [2/2]

void AZ::CommandLine::Parse ( int  argc,
char **  argv 
)

Construct a command line parser. It will load parameters from the given ARGC/ARGV parameters instead of process command line. Skips over the first parameter as it assumes it is the executable name


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