Args API
CLI argument schema and parsing helpers are available under ptool.args and p.args.
ptool.args.arg
v0.1.0- Introduced.
ptool.args.arg(id, kind, options) creates an argument builder for use in
ptool.args.parse(...).schema.args.
id(string, required): The argument identifier. It is also the key in the returned table.kind(string, required): The argument type. Supported values:"flag": A boolean flag."string": A string option."int": An integer option (i64)."positional": A positional argument.
options(table, optional): The same optional fields supported by argument tables inptool.args.parse, such aslong,short,help,required,multiple, anddefault.
The builder supports chainable methods, all of which return itself:
arg:long(value)sets the long option name. Supported only for non-positionalarguments.arg:short(value)sets the short option name. Supported only for non-positionalarguments.arg:help(value)sets the help text.arg:required(value)sets whether the argument is required. Ifvalueis omitted, it defaults totrue.arg:multiple(value)sets whether the argument can be repeated. Ifvalueis omitted, it defaults totrue.arg:default(value)sets the default value. Ifvalue = nil, the default is cleared.
Example:
local res = ptool.args.parse({
args = {
ptool.args.arg("name", "string"):required(),
ptool.args.arg("verbose", "flag", { short = "v" }),
ptool.args.arg("paths", "positional"):multiple(),
}
})
ptool.args.parse
v0.1.0- Introduced.
v0.3.0- Addedsubcommandssupport.
ptool.args.parse(schema) parses script arguments with clap and returns a
table indexed by id.
Script arguments come from the part after -- in ptool run <lua_file> -- ....
For example:
ptool.use("v0.1.0")
local res = ptool.args.parse({
name = "test",
about = "The test command",
args = {
{ id = "name", kind = "string" }
}
})
print("Hello, " .. res.name .. "!")
Schema Structure
name(string, optional): The command name, used in help output. Defaults to the script file name.about(string, optional): Help description.args(table, optional): An array of argument definitions. Each item supports two forms:- An argument table.
- A builder object returned by
ptool.args.arg(...).
subcommands(table, optional): A map of subcommand name to subcommand schema. Each subcommand schema supportsabout,args, andsubcommandsrecursively.
At least one of args or subcommands must be provided.
Argument table fields:
id(string, required): The argument identifier. It is also the key in the returned table.kind(string, required): The argument type. Supported values:"flag": A boolean flag."string": A string option."int": An integer option (i64)."positional": A positional argument.
long(string, optional): The long option name, such as"name"for--name. For non-positionalarguments, the default can be derived fromid.short(string, optional): The short option name, a single character such as"v"for-v.help(string, optional): Help text for the argument.required(boolean, optional): Whether the argument is required. Defaults tofalse.multiple(boolean, optional): Whether the argument can be repeated. Defaults tofalse.default(string/integer, optional): The default value.
When subcommands is present, the current command's args act as shared
options for that command tree, and are accepted before or after the selected
subcommand.
Example with subcommands:
local res = ptool.args.parse({
name = "demo",
args = {
ptool.args.arg("verbose", "flag", { short = "v" }),
ptool.args.arg("config", "string"),
},
subcommands = {
build = {
args = {
ptool.args.arg("release", "flag"),
},
subcommands = {
web = {
args = {
ptool.args.arg("out", "string"):required(),
},
},
},
},
clean = {
args = {
ptool.args.arg("all", "flag"),
},
},
},
})
Constraints
- The following constraints apply to both argument tables and builder syntax.
- Non-
positionalarguments may omitlongandshort. Iflongis omitted,idis used automatically. positionalarguments cannot setlong,short, ordefault.- When
positional.multiple = true, it must be the last argument inargs. multiple = trueis supported only forstringandpositional.defaultis supported only forstringandint, and cannot be used together withmultiple = true.- When
subcommandsis present,positionalarguments are not allowed in that same schema. - When
subcommandsis present at the top level, argument idscommand_pathandargsare reserved. - Along one selected subcommand path, ancestor and descendant subcommands cannot
reuse the same argument
id, because their values are merged into oneargstable.
Return Value
A Lua table is returned where keys are id and value types are as follows:
flag->booleanstring->string(orstring[]whenmultiple = true)int->integerpositional->string(orstring[]whenmultiple = true)
When subcommands is not present, the return value stays flat as above.
When subcommands is present, the return value has this shape:
- Top-level
argsvalues are returned directly on the top-level table. command_path->string[]: The matched subcommand path, for example{"build", "web"}.args->table: The merged argument values from the matched subcommand path.
For example:
{
verbose = true,
config = "cfg.toml",
command_path = { "build", "web" },
args = {
release = true,
out = "dist",
},
}