1b1994897Sopenharmony_ci# libbase components 2b1994897Sopenharmony_ci 3b1994897Sopenharmony_ci## pandargs 4b1994897Sopenharmony_ci 5b1994897Sopenharmony_ci### Description: 6b1994897Sopenharmony_ci 7b1994897Sopenharmony_cipandargs is header-only utility tool that helps to parse command line arguments. It supports several argument types: 8b1994897Sopenharmony_ci- integer 9b1994897Sopenharmony_ci- double 10b1994897Sopenharmony_ci- boolean 11b1994897Sopenharmony_ci- string 12b1994897Sopenharmony_ci- uint32_t 13b1994897Sopenharmony_ci- uint64_t 14b1994897Sopenharmony_ci- list 15b1994897Sopenharmony_ci- compound 16b1994897Sopenharmony_ci 17b1994897Sopenharmony_ciThe more detail description of each type is in "Usage" section below. 18b1994897Sopenharmony_ci 19b1994897Sopenharmony_ciSource code of pandargs contains in `utils/pandargs.h` file. 20b1994897Sopenharmony_ci 21b1994897Sopenharmony_ci### Usage: 22b1994897Sopenharmony_ci 23b1994897Sopenharmony_cipandargs API consists of two major entities: template class `PandArg`, which represents an argument and `PandArgParser` class, which represents a parser. 24b1994897Sopenharmony_ci 25b1994897Sopenharmony_ciInstead of `PandArg`, `PandArgCompound` can be used, which serves for creation of the compound arguments. 26b1994897Sopenharmony_ciIt inherits `PandArg<bool>`. 27b1994897Sopenharmony_ci 28b1994897Sopenharmony_ci#### Arguments 29b1994897Sopenharmony_ci 30b1994897Sopenharmony_ciTo create an argument, it's template constructor should be called. Here is an instance: 31b1994897Sopenharmony_ci 32b1994897Sopenharmony_ci```c++ 33b1994897Sopenharmony_ci // argument name | default value | argument description 34b1994897Sopenharmony_ci panda::PandArg<bool> pab("bool", false, "Sample boolean argument"); 35b1994897Sopenharmony_ci // argument name | argument description | sub-arguments 36b1994897Sopenharmony_ci PandArgCompound arg("compound", "Sample boolean argument", {&sub_bool_arg, &sub_int_arg, &sub_double_arg, &sub_string_arg}); 37b1994897Sopenharmony_ci``` 38b1994897Sopenharmony_ci 39b1994897Sopenharmony_ciConstructor can accept: 40b1994897Sopenharmony_ci- 3 parameters: argument name, default value, description. 41b1994897Sopenharmony_ci- 4 parameters for single list: argument name, default value, description, delimiter. 42b1994897Sopenharmony_ci- 5 parameters for integer args: argument name, default value, description, min value, max value 43b1994897Sopenharmony_ci- PandArgCompound accepts three parameters: argument name, description and list of sub-arguments 44b1994897Sopenharmony_ci 45b1994897Sopenharmony_ciThere is description for them: 46b1994897Sopenharmony_ci- Argument name, is a name, which will appear in a command line. 47b1994897Sopenharmony_ci- Default value is a value argument will have regardless was it parsed or not. 48b1994897Sopenharmony_ci- Argument description will be used to form a help message. 49b1994897Sopenharmony_ci- Delimiter is a character or string that separates the different value if the single argument list 50b1994897Sopenharmony_ci- Min value is the number that the integer argument cannot be less than 51b1994897Sopenharmony_ci- Max value is the number that the integer argument cannot be greater than 52b1994897Sopenharmony_ci- List of sub-arguments has type `std::initializer_list<PandArgBase *>`, and it accepts any non-compound arguments. 53b1994897Sopenharmony_ci Sub-arguments should not be added to the parser via `PandArgParser::Add`. 54b1994897Sopenharmony_ci 55b1994897Sopenharmony_ciTemplate parameter is an argument type. Following values could be passed: 56b1994897Sopenharmony_ci- `int` for integer argument 57b1994897Sopenharmony_ci- `double` for double argument 58b1994897Sopenharmony_ci- `bool` for boolean argument 59b1994897Sopenharmony_ci- `std::string` for string argument 60b1994897Sopenharmony_ci- `uint64_t` for uint64_t argument 61b1994897Sopenharmony_ci- `uint32_t` for uint32_t argument 62b1994897Sopenharmony_ci- `arg_list_t` for list argument 63b1994897Sopenharmony_ci 64b1994897Sopenharmony_ci`arg_list_t` is a type, declared in `pandargs.h` under `panda` namespace, which is an alias for `std::vector<std::string>` type. 65b1994897Sopenharmony_ci 66b1994897Sopenharmony_ci`PandArg` provides following public API: 67b1994897Sopenharmony_ci- `PandArgType GetType()` - returns type of an argument 68b1994897Sopenharmony_ci- `std::string GetName()` - returns name of an argument 69b1994897Sopenharmony_ci- `std::string GetDesc()` - returns description of an argument 70b1994897Sopenharmony_ci- `T GetValue()` - returns value of an argument depending on it's type 71b1994897Sopenharmony_ci- `T GetDefaultValue()` - returns default value of an argument 72b1994897Sopenharmony_ci- `void SetValue(T val)` - set value of an argument 73b1994897Sopenharmony_ci- `ResetDefaultValue()` - set value back to default one 74b1994897Sopenharmony_ci 75b1994897Sopenharmony_ci#### Argument types 76b1994897Sopenharmony_ciThere are three global argument types in pandargs: 77b1994897Sopenharmony_ci- regular arguments 78b1994897Sopenharmony_ci- tail arguments 79b1994897Sopenharmony_ci- remainder arguments 80b1994897Sopenharmony_ci 81b1994897Sopenharmony_ciRegular arguments are typical non-positional arguments like ```--arg=1``` 82b1994897Sopenharmony_ciTail arguments are positional arguments, which should be introduced with help of parser API 83b1994897Sopenharmony_ciRemainder arguments are arguments that come after trailing `--`. All of them are plain std::vector of std::string 84b1994897Sopenharmony_ci 85b1994897Sopenharmony_ciCompound argument is a boolean argument, which can contains sub-arguments, followed by symbol `:`, 86b1994897Sopenharmony_cie.g: `--compiler-dump:folder=ir_dump,compact`. Sub-arguments follow the same rules as the regular arguments 87b1994897Sopenharmony_ciand can be any type of argument. Compound arguments implicitly have default value `false` and user can't change it. 88b1994897Sopenharmony_ci 89b1994897Sopenharmony_ciTo access compound arguments from `C++`, regular convention should be used, for accessing sub-arguments, method name 90b1994897Sopenharmony_cicontains name of the compound argument plus name of the sub-argument. 91b1994897Sopenharmony_ciE.g. `--compiler-dump:compact`, here, to access `compact` sub-arguments `[Is|Get]CompilerDumpCompact()` should be used. 92b1994897Sopenharmony_ci 93b1994897Sopenharmony_ci#### Parser 94b1994897Sopenharmony_ci 95b1994897Sopenharmony_ci`PandArgParser` provides following public API: 96b1994897Sopenharmony_ci- `bool Add(PandArgBase* arg)` - add an argument to parser. Returns `true` if argument was succsessfully added. `PandArgBase` is a base type for all template argument types 97b1994897Sopenharmony_ci- `bool Parse(int argc, const char* argv[])` - parse arguments. Returns `true` on success. Note: `argv` & `argc` should be passed as is, without any amendments 98b1994897Sopenharmony_ci- `std::string GetErrorString()` - returns error string if error occurred on argument addition or parsing 99b1994897Sopenharmony_ci- `void EnableTail()` - enables positional arguments 100b1994897Sopenharmony_ci- `void DisableTail()` - disables positional arguments 101b1994897Sopenharmony_ci- `bool IsTailEnabled()` - returns `true` if positional arguments enabled 102b1994897Sopenharmony_ci- `bool IsArgSet(PandArgBase* arg)` - returns `true` if an argument was added to a parser 103b1994897Sopenharmony_ci- `bool IsArgSet(const std::string& arg_name)` - returns `true` if an argument with given name was added to a parser 104b1994897Sopenharmony_ci- `bool PushBackTail(PandArgBase* arg)` - add tail argument to the end of tail arguments list. `false` if argument already in a tail list 105b1994897Sopenharmony_ci- `bool PopBackTail()` - remove last argument from tail list 106b1994897Sopenharmony_ci- `void EraseTail()` - remove all arguments from tail list 107b1994897Sopenharmony_ci- `void EnableRemainder()` - enable remainder argument 108b1994897Sopenharmony_ci- `void DisableRemainder()` - disable remainder argument 109b1994897Sopenharmony_ci- `bool IsRemainderEnabled()` - `true` if remainder argument enabled 110b1994897Sopenharmony_ci- `arg_list_t GetRemainder()` - returns remainder argument 111b1994897Sopenharmony_ci- `std::string GetHelpString()` - returns string with all arguments and their description 112b1994897Sopenharmony_ci- `std::string GetRegularArgs()` - returns string with all regular arguments and their values 113b1994897Sopenharmony_ci 114b1994897Sopenharmony_ciTail argument is a sequence of positinal arguments values. Function ```PushBackTail()``` adds an argument to the end of sequence, ```PopBackTail()``` removes the last added argument. Tail arguments may be added to a parser when tail is disabled, but they will be ignored if tail is disabled while parsing 115b1994897Sopenharmony_ci 116b1994897Sopenharmony_ciSample parser usage: 117b1994897Sopenharmony_ci```c++ 118b1994897Sopenharmony_ci panda::PandArgParser pa_parser; 119b1994897Sopenharmony_ci pa_parser.EnableTail(); 120b1994897Sopenharmony_ci pa_parser.Add(&pab); 121b1994897Sopenharmony_ci if (!pa_parser.Add(&pab)) { 122b1994897Sopenharmony_ci std::cout << pa_parser.GetErrorString(); 123b1994897Sopenharmony_ci } 124b1994897Sopenharmony_ci if (!pa_parser.Parse(argc, argv)) { 125b1994897Sopenharmony_ci std::cout << pa_parser.GetErrorString(); 126b1994897Sopenharmony_ci } 127b1994897Sopenharmony_ci``` 128b1994897Sopenharmony_ci 129b1994897Sopenharmony_ci#### Command line arguments convention 130b1994897Sopenharmony_ci 131b1994897Sopenharmony_ci- Any non-positional argument should start with `--` (double dash) prefix 132b1994897Sopenharmony_ci- Argument and it's value may be separated either by whitespace (` `) or by equals (`=`) sign 133b1994897Sopenharmony_ci- If tail (positional arguments) enabled, first argument without double dash prefix concidered as a begin of positional arguments sequence 134b1994897Sopenharmony_ci- Positional arguments should be without names or `=` signs, separated by whitespaces 135b1994897Sopenharmony_ci- Boolean argument may be used without a value. This arguments always considered as `true` 136b1994897Sopenharmony_ci- Remainder arguments are all literals that come after trailing `--` 137b1994897Sopenharmony_ci- True values for boolean arguments: **true**, **on**, **1** 138b1994897Sopenharmony_ci- False values for boolean arguments: **false**, **off**, **0** 139b1994897Sopenharmony_ci- for integer arguments it's possible to define value range 140b1994897Sopenharmony_ci- list values must be repeated with arg name or separated by delimiter 141b1994897Sopenharmony_ci- string and list arguments may accept no parameters 142b1994897Sopenharmony_ci 143b1994897Sopenharmony_ciSample command line usage: 144b1994897Sopenharmony_ci```bash 145b1994897Sopenharmony_ci$ ./app --bool # bool is true 146b1994897Sopenharmony_ci$ ./app --bool= # bool is true 147b1994897Sopenharmony_ci$ ./app --bool on --bool1=off # bool is true, bool1 is false 148b1994897Sopenharmony_ci$ ./app --uint32=32 # uint32 is 32 149b1994897Sopenharmony_ci$ ./app --uint64=64 # uint64 is 64 150b1994897Sopenharmony_ci$ ./app --string="a string" # string is "a string" 151b1994897Sopenharmony_ci$ ./app --string "a string" # string is "a string" 152b1994897Sopenharmony_ci$ ./app --string string # string is "string" 153b1994897Sopenharmony_ci$ ./app --string= --int=1 # string is an empty string, int is 1 154b1994897Sopenharmony_ci$ ./app --list=val1 --list=val2 --list=val3 # list argument example 155b1994897Sopenharmony_ci$ ./app --slist=val1:val2:val3 # list argument example 156b1994897Sopenharmony_ci$ ./app --int=0x40 --uint64=0x400 # int is 64, uint64 is 1024 157b1994897Sopenharmony_ci$ ./app --int=1 false -1 "list1 list2 list3" # tail arguments example 158b1994897Sopenharmony_ci$ ./app --double 3.14 --bool off -- arg1 --arg2=1 --arg3=false # remainder arguments example 159b1994897Sopenharmony_ci$ ./app --compound:bool,int=2,double=54.321,string=Hello # Compound argument example 160b1994897Sopenharmony_ci``` 161b1994897Sopenharmony_ciIn the tail arguments example, `false` is a boolean value, `-1` is integer value and `str1` and `str2` is a string value. List may not be a tail argument. Positional values verified the same way as regular values, difference only that it's names are ignored in an input string, but position matters 162b1994897Sopenharmony_ciIn the remainder arguments example, all literals coming after `--` will go to remainder and can be obtained by `GetRemainder()` function 163b1994897Sopenharmony_ci 164b1994897Sopenharmony_ciHow to add tail arguments: 165b1994897Sopenharmony_ci```c++ 166b1994897Sopenharmony_ci panda::PandArgParser pa_parser; 167b1994897Sopenharmony_ci pa_parser.EnableTail(); 168b1994897Sopenharmony_ci // now pab will be processed as a positional argument 169b1994897Sopenharmony_ci if (!pa_parser.PushBackTail(&pab)) { 170b1994897Sopenharmony_ci std::cout << pa_parser.GetErrorString(); 171b1994897Sopenharmony_ci } 172b1994897Sopenharmony_ci if (!pa_parser.Parse(argc, argv)) { 173b1994897Sopenharmony_ci std::cout << pa_parser.GetErrorString(); 174b1994897Sopenharmony_ci } 175b1994897Sopenharmony_ci``` 176