同步操作将从 OpenHarmony/arkcompiler_runtime_core 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
pandargs is header-only utility tool that helps to parse command line arguments. It supports several argument types:
The more detail description of each type is in "Usage" section below.
Source code of pandargs contains in utils/pandargs.h
file.
pandargs API consists of two major entities: template class PandArg
, which represents an argument and PandArgParser
class, which represents a parser.
Instead of PandArg
, PandArgCompound
can be used, which serves for creation of the compound arguments.
It inherits PandArg<bool>
.
To create an argument, it's template constructor should be called. Here is an instance:
// argument name | default value | argument description
panda::PandArg<bool> pab("bool", false, "Sample boolean argument");
// argument name | argument description | sub-arguments
PandArgCompound arg("compound", "Sample boolean argument", {&sub_bool_arg, &sub_int_arg, &sub_double_arg, &sub_string_arg});
Constructor can accept:
There is description for them:
std::initializer_list<PandArgBase *>
, and it accepts any non-compound arguments.
Sub-arguments should not be added to the parser via PandArgParser::Add
.Template parameter is an argument type. Following values could be passed:
int
for integer argumentdouble
for double argumentbool
for boolean argumentstd::string
for string argumentuint64_t
for uint64_t argumentuint32_t
for uint32_t argumentarg_list_t
for list argumentarg_list_t
is a type, declared in pandargs.h
under panda
namespace, which is an alias for std::vector<std::string>
type.
PandArg
provides following public API:
PandArgType GetType()
- returns type of an argumentstd::string GetName()
- returns name of an argumentstd::string GetDesc()
- returns description of an argumentT GetValue()
- returns value of an argument depending on it's typeT GetDefaultValue()
- returns default value of an argumentvoid SetValue(T val)
- set value of an argumentResetDefaultValue()
- set value back to default oneThere are three global argument types in pandargs:
Regular arguments are typical non-positional arguments like --arg=1
Tail arguments are positional arguments, which should be introduced with help of parser API
Remainder arguments are arguments that come after trailing --
. All of them are plain std::vector of std::string
Compound argument is a boolean argument, which can contains sub-arguments, followed by symbol :
,
e.g: --compiler-dump:folder=ir_dump,compact
. Sub-arguments follow the same rules as the regular arguments
and can be any type of argument. Compound arguments implicitly have default value false
and user can't change it.
To access compound arguments from C++
, regular convention should be used, for accessing sub-arguments, method name
contains name of the compound argument plus name of the sub-argument.
E.g. --compiler-dump:compact
, here, to access compact
sub-arguments [Is|Get]CompilerDumpCompact()
should be used.
PandArgParser
provides following public API:
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 typesbool Parse(int argc, const char* argv[])
- parse arguments. Returns true
on success. Note: argv
& argc
should be passed as is, without any amendmentsstd::string GetErrorString()
- returns error string if error occurred on argument addition or parsingvoid EnableTail()
- enables positional argumentsvoid DisableTail()
- disables positional argumentsbool IsTailEnabled()
- returns true
if positional arguments enabledbool IsArgSet(PandArgBase* arg)
- returns true
if an argument was added to a parserbool IsArgSet(const std::string& arg_name)
- returns true
if an argument with given name was added to a parserbool PushBackTail(PandArgBase* arg)
- add tail argument to the end of tail arguments list. false
if argument already in a tail listbool PopBackTail()
- remove last argument from tail listvoid EraseTail()
- remove all arguments from tail listvoid EnableRemainder()
- enable remainder argumentvoid DisableRemainder()
- disable remainder argumentbool IsRemainderEnabled()
- true
if remainder argument enabledarg_list_t GetRemainder()
- returns remainder argumentstd::string GetHelpString()
- returns string with all arguments and their descriptionstd::string GetRegularArgs()
- returns string with all regular arguments and their valuesTail 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
Sample parser usage:
panda::PandArgParser pa_parser;
pa_parser.EnableTail();
pa_parser.Add(&pab);
if (!pa_parser.Add(&pab)) {
std::cout << pa_parser.GetErrorString();
}
if (!pa_parser.Parse(argc, argv)) {
std::cout << pa_parser.GetErrorString();
}
--
(double dash) prefix
) or by equals (=
) sign=
signs, separated by whitespacestrue
--
Sample command line usage:
$ ./app --bool # bool is true
$ ./app --bool= # bool is true
$ ./app --bool on --bool1=off # bool is true, bool1 is false
$ ./app --uint32=32 # uint32 is 32
$ ./app --uint64=64 # uint64 is 64
$ ./app --string="a string" # string is "a string"
$ ./app --string "a string" # string is "a string"
$ ./app --string string # string is "string"
$ ./app --string= --int=1 # string is an empty string, int is 1
$ ./app --list=val1 --list=val2 --list=val3 # list argument example
$ ./app --slist=val1:val2:val3 # list argument example
$ ./app --int=0x40 --uint64=0x400 # int is 64, uint64 is 1024
$ ./app --int=1 false -1 "list1 list2 list3" # tail arguments example
$ ./app --double 3.14 --bool off -- arg1 --arg2=1 --arg3=false # remainder arguments example
$ ./app --compound:bool,int=2,double=54.321,string=Hello # Compound argument example
In 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
In the remainder arguments example, all literals coming after --
will go to remainder and can be obtained by GetRemainder()
function
How to add tail arguments:
panda::PandArgParser pa_parser;
pa_parser.EnableTail();
// now pab will be processed as a positional argument
if (!pa_parser.PushBackTail(&pab)) {
std::cout << pa_parser.GetErrorString();
}
if (!pa_parser.Parse(argc, argv)) {
std::cout << pa_parser.GetErrorString();
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。