.\" Man page generated from reStructuredText. . .TH "ARGPARSE" "1" "Jan 03, 2021" "0.6.0" "argparse" .SH NAME argparse \- argparse tutorial . .nr rst2man-indent-level 0 . .de1 rstReportMargin \\$1 \\n[an-margin] level \\n[rst2man-indent-level] level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] - \\n[rst2man-indent0] \\n[rst2man-indent1] \\n[rst2man-indent2] .. .de1 INDENT .\" .rstReportMargin pre: . RS \\$1 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] . nr rst2man-indent-level +1 .\" .rstReportMargin post: .. .de UNINDENT . RE .\" indent \\n[an-margin] .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] .nr rst2man-indent-level -1 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. .sp Contents: .SH CREATING AND USING PARSERS .sp The \fBargparse\fP module is a function which, when called, creates an instance of the Parser class. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C \-\- script.lua local argparse = require "argparse" local parser = argparse() .ft P .fi .UNINDENT .UNINDENT .sp \fBparser\fP is now an empty parser which does not recognize any command line arguments or options. .SS Parsing command line arguments .sp \fB:parse([argv])\fP method of the Parser class returns a table with processed data from the command line or \fBargv\fP array. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C local args = parser:parse() .ft P .fi .UNINDENT .UNINDENT .sp After this is executed with \fBlua script.lua\fP, \fBargs\fP is an empty table because the parser is empty and no command line arguments were supplied. .SS Error handling .sp If the provided command line arguments are not recognized by the parser, it will print an error message and call \fBos.exit(1)\fP\&. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua foo .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Usage: script.lua [\-h] Error: too many arguments .ft P .fi .UNINDENT .UNINDENT .sp If halting the program is undesirable, \fB:pparse([args])\fP method should be used. It returns boolean flag indicating success of parsing and result or error message. .sp An error can raised manually using \fB:error()\fP method. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C parser:error("manual argument validation failed") .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Usage: script.lua [\-h] Error: manual argument validation failed .ft P .fi .UNINDENT .UNINDENT .SS Help option .sp As the automatically generated usage message states, there is a help option \fB\-h\fP added to any parser by default. .sp When a help option is used, parser will print a help message and call \fBos.exit(0)\fP\&. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-h .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Usage: script.lua [\-h] Options: \-h, \-\-help Show this help message and exit. .ft P .fi .UNINDENT .UNINDENT .SS Typo autocorrection .sp When an option is not recognized by the parser, but there is an option with a similar name, a suggestion is automatically added to the error message. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-\-hepl .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Usage: script.lua [\-h] Error: unknown option \(aq\-\-hepl\(aq Did you mean \(aq\-\-help\(aq? .ft P .fi .UNINDENT .UNINDENT .SS Configuring parsers .sp Parsers have several properties affecting their behavior. For example, \fBdescription\fP and \fBepilog\fP properties set the text to be displayed in the help message after the usage message and after the listings of options and arguments, respectively. Another is \fBname\fP, which overwrites the name of the program which is used in the usage message (default value is inferred from command line arguments). .sp There are several ways to set properties. The first is to chain setter methods of Parser object. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C local parser = argparse() :name "script" :description "A testing script." :epilog "For more info, see http://example.com" .ft P .fi .UNINDENT .UNINDENT .sp The second is to call a parser with a table containing some properties. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C local parser = argparse() { name = "script", description = "A testing script.", epilog "For more info, see http://example.com." } .ft P .fi .UNINDENT .UNINDENT .sp Finally, \fBname\fP\&. \fBdescription\fP and \fBepilog\fP properties can be passed as arguments when calling a parser. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C local parser = argparse("script", "A testing script.", "For more info, see http://example.com.") .ft P .fi .UNINDENT .UNINDENT .SH ADDING AND CONFIGURING ARGUMENTS .sp Positional arguments can be added using \fB:argument(name, description, default, convert, args)\fP method. It returns an Argument instance, which can be configured in the same way as Parsers. The \fBname\fP property is required. .sp This and the following examples show contents of the result table returned by \fIparser:parse()\fP when the script is executed with given command\-line arguments. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C parser:argument "input" .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua foo .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C { input = "foo" } .ft P .fi .UNINDENT .UNINDENT .sp The data passed to the argument is stored in the result table at index \fBinput\fP because it is the argument\(aqs name. The index can be changed using \fBtarget\fP property. .SS Setting number of consumed arguments .sp \fBargs\fP property sets how many command line arguments the argument consumes. Its value is interpreted as follows: .TS center; |l|l|. _ T{ Value T} T{ Interpretation T} _ T{ Number \fBN\fP T} T{ Exactly \fBN\fP arguments T} _ T{ String \fBA\-B\fP, where \fBA\fP and \fBB\fP are numbers T} T{ From \fBA\fP to \fBB\fP arguments T} _ T{ String \fBN+\fP, where \fBN\fP is a number T} T{ \fBN\fP or more arguments T} _ T{ String \fB?\fP T} T{ An optional argument T} _ T{ String \fB*\fP T} T{ Any number of arguments T} _ T{ String \fB+\fP T} T{ At least one argument T} _ .TE .sp If more than one argument can be consumed, a table is used to store the data. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C parser:argument("pair", "A pair of arguments.") :args(2) parser:argument("optional", "An optional argument.") :args "?" .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua foo bar .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C { pair = {"foo", "bar"} } .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua foo bar baz .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C { pair = {"foo", "bar"}, optional = "baz" } .ft P .fi .UNINDENT .UNINDENT .SH ADDING AND CONFIGURING OPTIONS .sp Options can be added using \fB:option(name, description, default, convert, args, count)\fP method. It returns an Option instance, which can be configured in the same way as Parsers. The \fBname\fP property is required. An option can have several aliases, which can be set as space separated substrings in its name or by continuously setting \fBname\fP property. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C \-\- These lines are equivalent: parser:option "\-f" "\-\-from" parser:option "\-f \-\-from" .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-\-from there $ lua script.lua \-\-from=there $ lua script.lua \-f there $ lua script.lua \-fthere .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C { from = "there" } .ft P .fi .UNINDENT .UNINDENT .sp For an option, default index used to store arguments passed to it is the first "long" alias (an alias starting with two control characters, typically hyphens) or just the first alias, without control characters. Hyphens in the default index are replaced with underscores. In the following table it is assumed that \fBlocal args = parser:parse()\fP has been executed. .TS center; |l|l|. _ T{ Option\(aqs aliases T} T{ Location of option\(aqs arguments T} _ T{ \fB\-o\fP T} T{ \fBargs.o\fP T} _ T{ \fB\-o\fP \fB\-\-output\fP T} T{ \fBargs.output\fP T} _ T{ \fB\-s\fP \fB\-\-from\-server\fP T} T{ \fBargs.from_server\fP T} _ .TE .sp As with arguments, the index can be explicitly set using \fBtarget\fP property. .SS Flags .sp Flags are almost identical to options, except that they don\(aqt take an argument by default. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C parser:flag("\-q \-\-quiet") .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-q .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C { quiet = true } .ft P .fi .UNINDENT .UNINDENT .SS Control characters .sp The first characters of all aliases of all options of a parser form the set of control characters, used to distinguish options from arguments. Typically the set only consists of a hyphen. .SS Setting number of consumed arguments .sp Just as arguments, options can be configured to take several command line arguments. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C parser:option "\-\-pair" :args(2) parser:option "\-\-optional" :args "?" .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-\-pair foo bar .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C { pair = {"foo", "bar"} } .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-\-pair foo bar \-\-optional .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C { pair = {"foo", "bar"}, optional = {} } .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-\-optional=baz .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C { optional = {"baz"} } .ft P .fi .UNINDENT .UNINDENT .sp Note that the data passed to \fBoptional\fP option is stored in an array. That is necessary to distinguish whether the option was invoked without an argument or it was not invoked at all. .SS Setting number of invocations .sp For options, it is possible to control how many times they can be used. argparse uses \fBcount\fP property to set how many times an option can be invoked. The value of the property is interpreted in the same way \fBargs\fP is. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C parser:option("\-e \-\-exclude") :count "*" .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-eFOO \-eBAR .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C { exclude = {"FOO", "BAR"} } .ft P .fi .UNINDENT .UNINDENT .sp If an option can be used more than once and it can consume more than one argument, the data is stored as an array of invocations, each being an array of arguments. .sp As a special case, if an option can be used more than once and it consumes no arguments (e.g. it\(aqs a flag), than the number of invocations is stored in the associated field of the result table. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C parser:flag("\-v \-\-verbose", "Sets verbosity level.") :count "0\-2" :target "verbosity" .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-vv .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C { verbosity = 2 } .ft P .fi .UNINDENT .UNINDENT .SH MUTUALLY EXCLUSIVE GROUPS .sp A group of arguments and options can be marked as mutually exclusive using \fB:mutex(argument_or_option, ...)\fP method of the Parser class. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C parser:mutex( parser:argument "input" :args "?", parser:flag "\-\-process\-stdin" ) parser:mutex( parser:flag "\-q \-\-quiet", parser:flag "\-v \-\-verbose" ) .ft P .fi .UNINDENT .UNINDENT .sp If more than one element of a mutually exclusive group is used, an error is raised. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-qv .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Usage: script.lua ([\-q] | [\-v]) [\-h] ([] | [\-\-process\-stdin]) Error: option \(aq\-v\(aq can not be used together with option \(aq\-q\(aq .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua file \-\-process\-stdin .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Usage: script.lua ([\-q] | [\-v]) [\-h] ([] | [\-\-process\-stdin]) Error: option \(aq\-\-process\-stdin\(aq can not be used together with argument \(aqinput\(aq .ft P .fi .UNINDENT .UNINDENT .SH ADDING AND CONFIGURING COMMANDS .sp A command is a subparser invoked when its name is passed as an argument. For example, in \fI\%git\fP CLI \fBadd\fP, \fBcommit\fP, \fBpush\fP, etc. are commands. Each command has its own set of arguments and options, but inherits options of its parent. .sp Commands can be added using \fB:command(name, description, epilog)\fP method. Just as options, commands can have several aliases. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C parser:command "install i" .ft P .fi .UNINDENT .UNINDENT .sp If a command it used, \fBtrue\fP is stored in the corresponding field of the result table. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua install .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C { install = true } .ft P .fi .UNINDENT .UNINDENT .sp A typo will result in an appropriate error message. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua instal .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Usage: script.lua [\-h] ... Error: unknown command \(aqinstal\(aq Did you mean \(aqinstall\(aq? .ft P .fi .UNINDENT .UNINDENT .SS Getting name of selected command .sp Use \fBcommand_target\fP property of the parser to store the name of used command in a field of the result table. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C parser:command_target("command") parser:command("install") parser:command("remove") .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua install .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C { install = true, command = "install" } .ft P .fi .UNINDENT .UNINDENT .SS Adding elements to commands .sp The Command class is a subclass of the Parser class, so all the Parser\(aqs methods for adding elements work on commands, too. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C local install = parser:command "install" install:argument "rock" install:option "\-f \-\-from" .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua install foo \-\-from=bar .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C { install = true, rock = "foo", from = "bar" } .ft P .fi .UNINDENT .UNINDENT .sp Commands have their own usage and help messages. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua install .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Usage: script.lua install [\-f ] [\-h] Error: too few arguments .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua install \-\-help .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Usage: script.lua install [\-f ] [\-h] Arguments: rock Options: \-f , \-\-from \-h, \-\-help Show this help message and exit. .ft P .fi .UNINDENT .UNINDENT .SS Making a command optional .sp By default, if a parser has commands, using one of them is obligatory. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C local parser = argparse() parser:command "install" .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Usage: script.lua [\-h] ... Error: a command is required .ft P .fi .UNINDENT .UNINDENT .sp This can be changed using \fBrequire_command\fP property. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C local parser = argparse() :require_command(false) parser:command "install" .ft P .fi .UNINDENT .UNINDENT .SH DEFAULT VALUES .sp For elements such as arguments and options, if \fBdefault\fP property is set to a string, its value is stored in case the element was not used (if it\(aqs not a string, it\(aqll be used as \fBinit\fP property instead, see actions). .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C parser:option("\-o \-\-output", "Output file.", "a.out") \-\- Equivalent: parser:option "\-o" "\-\-output" :description "Output file." :default "a.out" .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C { output = "a.out" } .ft P .fi .UNINDENT .UNINDENT .sp The existence of a default value is reflected in help message, unless \fBshow_default\fP property is set to \fBfalse\fP\&. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-\-help .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Usage: script.lua [\-o ] [\-h] Options: \-o , \-\-output Output file. (default: a.out) \-h, \-\-help Show this help message and exit. .ft P .fi .UNINDENT .UNINDENT .sp Note that invocation without required arguments is still an error. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-o .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Usage: script.lua [\-o ] [\-h] Error: too few arguments .ft P .fi .UNINDENT .UNINDENT .SS Default mode .sp \fBdefmode\fP property regulates how argparse should use the default value of an element. .sp By default, or if \fBdefmode\fP contains \fBu\fP (for unused), the default value will be automatically passed to the element if it was not invoked at all. It will be passed minimal required of times, so that if the element is allowed to consume no arguments (e.g. using \fB:args "?"\fP), the default value is ignored. .sp If \fBdefmode\fP contains \fBa\fP (for argument), the default value will be automatically passed to the element if not enough arguments were passed, or not enough invocations were made. .sp Consider the difference: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C parser:option "\-o" :default "a.out" parser:option "\-p" :default "password" :defmode "arg" .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-h .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Usage: script.lua [\-o ] [\-p [

]] [\-h] Options: \-o default: a.out \-p [

] default: password \-h, \-\-help Show this help message and exit. .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C { o = "a.out" } .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-p .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C { o = "a.out", p = "password" } .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-o .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Usage: script.lua [\-o ] [\-p [

]] [\-h] Error: too few arguments .ft P .fi .UNINDENT .UNINDENT .SH CALLBACKS .SS Converters .sp argparse can perform automatic validation and conversion on arguments. If \fBconvert\fP property of an element is a function, it will be applied to all the arguments passed to it. The function should return \fBnil\fP and, optionally, an error message if conversion failed. Standard \fBtonumber\fP and \fBio.open\fP functions work exactly like that. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C parser:argument "input" :convert(io.open) parser:option "\-t \-\-times" :convert(tonumber) .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua foo.txt \-t5 .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C { input = file_object, times = 5 } .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua nonexistent.txt .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Usage: script.lua [\-t ] [\-h] Error: nonexistent.txt: No such file or directory .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua foo.txt \-\-times=many .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Usage: script.lua [\-t ] [\-h] Error: malformed argument \(aqmany\(aq .ft P .fi .UNINDENT .UNINDENT .sp If \fBconvert\fP property of an element is an array of functions, they will be used as converters for corresponding arguments in case the element accepts multiple arguments. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C parser:option "\-\-line\-style" :args(2) :convert {string.lower, tonumber} .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-\-line\-style DASHED 1.5 .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C { line_style = {"dashed", 1.5} } .ft P .fi .UNINDENT .UNINDENT .SS Table converters .sp If convert property of an element is a table and doesn\(aqt have functions in array part, arguments passed to it will be used as keys. If a key is missing, an error is raised. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C parser:argument "choice" :convert { foo = "Something foo\-related", bar = "Something bar\-related" } .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua bar .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C { choice = "Something bar\-related" } .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua baz .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Usage: script.lua [\-h] Error: malformed argument \(aqbaz\(aq .ft P .fi .UNINDENT .UNINDENT .SS Actions .SS Argument and option actions .sp argparse uses action callbacks to process invocations of arguments and options. Default actions simply put passed arguments into the result table as a single value or insert into an array depending on number of arguments the option can take and how many times it can be used. .sp A custom action can be set using \fBaction\fP property. An action must be a function. and will be called after each invocation of the option or the argument it is assigned to. Four arguments are passed: result table, target index in that table, an argument or an array of arguments passed by user, and overwrite flag used when an option is invoked too many times. .sp Converters are applied before actions. .sp Initial value to be stored at target index in the result table can be set using \fBinit\fP property, or also using \fBdefault\fP property if the value is not a string. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C parser:option("\-\-exceptions"):args("*"):action(function(args, _, exceptions) for _, exception in ipairs(exceptions) do table.insert(args.exceptions, exception) end end):init({"foo", "bar"}) parser:flag("\-\-no\-exceptions"):action(function(args) args.exceptions = {} end) .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-\-exceptions x y \-\-exceptions z t .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C { exceptions = { "foo", "bar", "x", "y", "z", "t" } } .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-\-exceptions x y \-\-no\-exceptions .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C { exceptions = {} } .ft P .fi .UNINDENT .UNINDENT .sp Actions can also be used when a flag needs to print some message and exit without parsing remaining arguments. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C parser:flag("\-v \-\-version"):action(function() print("script v1.0.0") os.exit(0) end) .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-v .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C script v1.0.0 .ft P .fi .UNINDENT .UNINDENT .SS Built\-in actions .sp These actions can be referred to by their string names when setting \fBaction\fP property: .TS center; |l|l|. _ T{ Name T} T{ Description T} _ T{ store T} T{ Stores argument or arguments at target index. T} _ T{ store_true T} T{ Stores \fBtrue\fP at target index. T} _ T{ store_false T} T{ Stores \fBfalse\fP at target index. T} _ T{ count T} T{ Increments number at target index. T} _ T{ append T} T{ Appends argument or arguments to table at target index. T} _ T{ concat T} T{ Appends arguments one by one to table at target index. T} _ .TE .sp Examples using \fBstore_false\fP and \fBconcat\fP actions: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C parser:flag("\-\-candy") parser:flag("\-\-no\-candy"):target("candy"):action("store_false") parser:flag("\-\-rain", "Enable rain", false) parser:option("\-\-exceptions"):args("*"):action("concat"):init({"foo", "bar"}) .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C { rain = false } .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-\-candy .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C { candy = true, rain = false } .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-\-no\-candy \-\-rain .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C { candy = false, rain = true } .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-\-exceptions x y \-\-exceptions z t .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C { exceptions = { "foo", "bar", "x", "y", "z", "t" }, rain = false } .ft P .fi .UNINDENT .UNINDENT .SS Command actions .sp Actions for parsers and commands are simply callbacks invoked after parsing, with result table and command name as the arguments. Actions for nested commands are called first. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C local install = parser:command("install"):action(function(args, name) print("Running " .. name) \-\- Use args here ) parser:action(function(args) print("Callbacks are fun!") end) .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua install .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Running install Callbacks are fun! .ft P .fi .UNINDENT .UNINDENT .SH CONFIGURING HELP AND USAGE MESSAGES .sp The usage and help messages of parsers and commands can be generated on demand using \fB:get_usage()\fP and \fB:get_help()\fP methods, and overridden using \fBhelp\fP and \fBusage\fP properties. When using the autogenerated usage and help messages, there are several ways to adjust them. .SS Hiding arguments, options, and commands from messages .sp If \fBhidden\fP property for an argument, an option or a command is set to \fBtrue\fP, it is not included into help and usage messages, but otherwise works normally. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C parser:option "\-\-normal\-option" parser:option "\-\-deprecated\-option" :hidden(true) .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-\-help .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Usage: script.lua [\-\-normal\-option ] [\-h] Options: \-\-normal\-option \-h, \-\-help Show this help message and exit. .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-\-deprecated\-option value .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C { deprecated_option = "value" } .ft P .fi .UNINDENT .UNINDENT .SS Setting argument placeholder .sp For options and arguments, \fBargname\fP property controls the placeholder for the argument in the usage message. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C parser:option "\-f" "\-\-from" :argname "" .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-\-help .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Usage: script.lua [\-f ] [\-h] Options: \-f , \-\-from \-h, \-\-help Show this help message and exit. .ft P .fi .UNINDENT .UNINDENT .sp \fBargname\fP can be an array of placeholders. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C parser:option "\-\-pair" :args(2) :argname {"", ""} .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-\-help .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Usage: script.lua [\-\-pair ] [\-h] Options: \-\-pair \-h, \-\-help Show this help message and exit. .ft P .fi .UNINDENT .UNINDENT .SS Grouping elements .sp \fB:group(name, ...)\fP method of parsers and commands puts passed arguments, options, and commands into a named group with its own section in the help message. Elements outside any groups are put into a default section. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C parser:group("Configuring output format", parser:flag "\-v \-\-verbose", parser:flag "\-\-use\-colors", parser:option "\-\-encoding" ) parser:group("Configuring processing", parser:option "\-\-compression\-level", parser:flag "\-\-skip\-broken\-chunks" ) parser:flag "\-\-version" :action(function() print("script.lua 1.0.0") os.exit(0) end) .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-\-help .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Usage: script.lua [\-v] [\-\-use\-colors] [\-\-encoding ] [\-\-compression\-level ] [\-\-skip\-broken\-chunks] [\-\-version] [\-h] Configuring output format: \-v, \-\-verbose \-\-use\-colors \-\-encoding Configuring processing: \-\-compression\-level \-\-skip\-broken\-chunks Other options: \-\-version \-h, \-\-help Show this help message and exit. .ft P .fi .UNINDENT .UNINDENT .SS Help message line wrapping .sp If \fBhelp_max_width\fP property of a parser or a command is set, when generating its help message, argparse will automatically wrap lines, attempting to fit into given number of columns. This includes wrapping lines in parser description and epilog and descriptions of arguments, options, and commands. .sp Line wrapping preserves existing line endings and only splits overly long input lines. When breaking a long line, it replicates indentation of the line in the continuation lines. Additionally, if the first non\-space token in a line is \fB*\fP, \fB+\fP, or \fB\-\fP, the line is considered a list item, and the continuation lines are aligned with the first word after the list item marker. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C parser:help_max_width(80) parser:option "\-f \-\-foo" :description("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor " .. "incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation " .. "ullamco laboris nisi ut aliquip ex ea commodo consequat.\en" .. "The next paragraph is indented:\en" .. " Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. " .. "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.") parser:option "\-b \-\-bar" :description("Here is a list:\en".. "* Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor...\en" .. "* Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip...\en" .. "* Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.") .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-\-help .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Usage: script.lua [\-f ] [\-b ] [\-h] Options: \-f , Lorem ipsum dolor sit amet, consectetur adipiscing \-\-foo elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. The next paragraph is indented: Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. \-b , Here is a list: \-\-bar * Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor... * Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip... * Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. \-h, \-\-help Show this help message and exit. .ft P .fi .UNINDENT .UNINDENT .sp \fBhelp_max_width\fP property is inherited by commands. .SS Configuring help and usage message layout .sp Several other parser and command properties can be used to tweak help and usage message format. Like \fBhelp_max_width\fP, all of them are inherited by commands when set on the parser or a parent command. .sp \fBusage_max_width\fP property sets maximum width of the usage string. Default is \fB70\fP\&. .sp \fBusage_margin\fP property sets margin width used when line wrapping long usage strings. Default is \fB7\fP\&. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C parser:usage_max_width(50) :usage_margin(#"Usage: script.lua ") parser:option "\-\-foo" parser:option "\-\-bar" parser:option "\-\-baz" parser:option "\-\-qux" print(parser:get_usage()) .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Usage: script.lua [\-\-foo ] [\-\-bar ] [\-\-baz ] [\-\-qux ] [\-h] .ft P .fi .UNINDENT .UNINDENT .sp Help message for a group of arguments, options, or commands is organized into two columns, with usage template on the left side and descriptions on the right side. \fBhelp_usage_margin\fP property sets horizontal offset for the first column (\fB3\fP by default). \fBhelp_description_margin\fP property sets horizontal offset for the second column (\fB25\fP by default). .sp \fBhelp_vertical_space\fP property sets number of extra empty lines to put between descriptions for different elements within a group (\fB0\fP by default). .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C parser:help_usage_margin(2) :help_description_margin(17) :help_vertical_space(1) parser:option("\-\-foo", "Set foo.") parser:option("\-\-bar", "Set bar.") parser:option("\-\-baz", "Set baz.") parser:option("\-\-qux", "Set qux.") .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-\-help .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Usage: script.lua [\-\-foo ] [\-\-bar ] [\-\-baz ] [\-\-qux ] [\-h] Options: \-\-foo Set foo. \-\-bar Set bar. \-\-baz Set baz. \-\-qux Set qux. \-h, \-\-help Show this help message and exit. .ft P .fi .UNINDENT .UNINDENT .SH MISCELLANEOUS .SS Argparse version .sp \fBargparse\fP module is a table with \fB__call\fP metamethod. \fBargparse.version\fP is a string in \fBMAJOR.MINOR.PATCH\fP format specifying argparse version. .SS Overwriting default help option .sp If the property \fBadd_help\fP of a parser is set to \fBfalse\fP, no help option will be added to it. Otherwise, the value of the field will be used to configure it. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C local parser = argparse() :add_help "/?" .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua /? .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Usage: script.lua [/?] Options: /? Show this help message and exit. .ft P .fi .UNINDENT .UNINDENT .SS Disabling option handling .sp When \fBhandle_options\fP property of a parser or a command is set to \fBfalse\fP, all options will be passed verbatim to the argument list, as if the input included double\-hyphens. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C parser:handle_options(false) parser:argument "input" :args "*" parser:option "\-f" "\-\-foo" :args "*" .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua bar \-f \-\-foo bar .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C { input = {"bar", "\-f", "\-\-foo", "bar"} } .ft P .fi .UNINDENT .UNINDENT .SS Prohibiting overuse of options .sp By default, if an option is invoked too many times, latest invocations overwrite the data passed earlier. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C parser:option "\-o \-\-output" .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-oFOO \-oBAR .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C { output = "BAR" } .ft P .fi .UNINDENT .UNINDENT .sp Set \fBoverwrite\fP property to \fBfalse\fP to prohibit this behavior. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C parser:option "\-o \-\-output" :overwrite(false) .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ lua script.lua \-oFOO \-oBAR .ft P .fi .UNINDENT .UNINDENT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Usage: script.lua [\-o ] [\-h] Error: option \(aq\-o\(aq must be used at most 1 time .ft P .fi .UNINDENT .UNINDENT .SS Parsing algorithm .sp argparse interprets command line arguments in the following way: .TS center; |l|l|. _ T{ Argument T} T{ Interpretation T} _ T{ \fBfoo\fP T} T{ An argument of an option or a positional argument. T} _ T{ \fB\-\-foo\fP T} T{ An option. T} _ T{ \fB\-\-foo=bar\fP T} T{ An option and its argument. The option must be able to take arguments. T} _ T{ \fB\-f\fP T} T{ An option. T} _ T{ \fB\-abcdef\fP T} T{ Letters are interpreted as options. If one of them can take an argument, the rest of the string is passed to it. T} _ T{ \fB\-\-\fP T} T{ The rest of the command line arguments will be interpreted as positional arguments. T} _ .TE .SS Property lists .SS Parser properties .sp Properties that can be set as arguments when calling or constructing a parser, in this order: .TS center; |l|l|. _ T{ Property T} T{ Type T} _ T{ \fBname\fP T} T{ String T} _ T{ \fBdescription\fP T} T{ String T} _ T{ \fBepilog\fP T} T{ String T} _ .TE .sp Other properties: .TS center; |l|l|. _ T{ Property T} T{ Type T} _ T{ \fBusage\fP T} T{ String T} _ T{ \fBhelp\fP T} T{ String T} _ T{ \fBrequire_command\fP T} T{ Boolean T} _ T{ \fBhandle_options\fP T} T{ Boolean T} _ T{ \fBadd_help\fP T} T{ Boolean or string or table T} _ T{ \fBcommand_target\fP T} T{ String T} _ T{ \fBusage_max_width\fP T} T{ Number T} _ T{ \fBusage_margin\fP T} T{ Number T} _ T{ \fBhelp_max_width\fP T} T{ Number T} _ T{ \fBhelp_usage_margin\fP T} T{ Number T} _ T{ \fBhelp_description_margin\fP T} T{ Number T} _ T{ \fBhelp_vertical_space\fP T} T{ Number T} _ .TE .SS Command properties .sp Properties that can be set as arguments when calling or constructing a command, in this order: .TS center; |l|l|. _ T{ Property T} T{ Type T} _ T{ \fBname\fP T} T{ String T} _ T{ \fBdescription\fP T} T{ String T} _ T{ \fBepilog\fP T} T{ String T} _ .TE .sp Other properties: .TS center; |l|l|. _ T{ Property T} T{ Type T} _ T{ \fBtarget\fP T} T{ String T} _ T{ \fBusage\fP T} T{ String T} _ T{ \fBhelp\fP T} T{ String T} _ T{ \fBrequire_command\fP T} T{ Boolean T} _ T{ \fBhandle_options\fP T} T{ Boolean T} _ T{ \fBaction\fP T} T{ Function T} _ T{ \fBadd_help\fP T} T{ Boolean or string or table T} _ T{ \fBcommand_target\fP T} T{ String T} _ T{ \fBhidden\fP T} T{ Boolean T} _ T{ \fBusage_max_width\fP T} T{ Number T} _ T{ \fBusage_margin\fP T} T{ Number T} _ T{ \fBhelp_max_width\fP T} T{ Number T} _ T{ \fBhelp_usage_margin\fP T} T{ Number T} _ T{ \fBhelp_description_margin\fP T} T{ Number T} _ T{ \fBhelp_vertical_space\fP T} T{ Number T} _ .TE .SS Argument properties .sp Properties that can be set as arguments when calling or constructing an argument, in this order: .TS center; |l|l|. _ T{ Property T} T{ Type T} _ T{ \fBname\fP T} T{ String T} _ T{ \fBdescription\fP T} T{ String T} _ T{ \fBdefault\fP T} T{ Any T} _ T{ \fBconvert\fP T} T{ Function or table T} _ T{ \fBargs\fP T} T{ Number or string T} _ .TE .sp Other properties: .TS center; |l|l|. _ T{ Property T} T{ Type T} _ T{ \fBtarget\fP T} T{ String T} _ T{ \fBdefmode\fP T} T{ String T} _ T{ \fBshow_default\fP T} T{ Boolean T} _ T{ \fBargname\fP T} T{ String or table T} _ T{ \fBaction\fP T} T{ Function or string T} _ T{ \fBinit\fP T} T{ Any T} _ T{ \fBhidden\fP T} T{ Boolean T} _ .TE .SS Option and flag properties .sp Properties that can be set as arguments when calling or constructing an option or a flag, in this order: .TS center; |l|l|. _ T{ Property T} T{ Type T} _ T{ \fBname\fP T} T{ String T} _ T{ \fBdescription\fP T} T{ String T} _ T{ \fBdefault\fP T} T{ Any T} _ T{ \fBconvert\fP T} T{ Function or table T} _ T{ \fBargs\fP T} T{ Number or string T} _ T{ \fBcount\fP T} T{ Number or string T} _ .TE .sp Other properties: .TS center; |l|l|. _ T{ Property T} T{ Type T} _ T{ \fBtarget\fP T} T{ String T} _ T{ \fBdefmode\fP T} T{ String T} _ T{ \fBshow_default\fP T} T{ Boolean T} _ T{ \fBoverwrite\fP T} T{ Booleans T} _ T{ \fBargname\fP T} T{ String or table T} _ T{ \fBaction\fP T} T{ Function or string T} _ T{ \fBinit\fP T} T{ Any T} _ T{ \fBhidden\fP T} T{ Boolean T} _ .TE .sp This is a tutorial for \fI\%argparse\fP, a feature\-rich command line parser for Lua. .SH AUTHOR Peter Melnichenko .SH COPYRIGHT 2021 - 2018, Peter Melnichenko .\" Generated by docutils manpage writer. .