3.6 KiB
std::args
std::args reads a program's arguments into a struct, by the attributes the
program marks it with, and writes the help that says what it reads from the
same marks. A struct marked @args::command is read: each field marked
@args::option from --name value, --name=value, or its short form, and
those marked @args::argument from what's left. A union of commands is a
command's subcommands, each a field. args::parse gives what was read or why it
couldn't be, and args::parse_or_exit prints the help or the mistake and exits
where it is asked to.
Using it
Require the standard library in your blossomfile, and name the module among a
target's deps, with the other modules of std it reads:
requires:
- https://git.malunal.studio/sakura/std@v1.0.0
targets:
- name: app
type: program
deps: [std.args, std.io]
import std::io;
import std::args;
@args::command{ name = "greet", help = "Says hello." }
struct greeting {
@args::option{ short = 'n', value = "N", help = "How many times." }
var times: u32 = 1;
@args::option{ short = 'l', help = "Says it loudly." }
var loud: bool;
@args::argument{ value = "NAME", help = "Who to greet." }
var names: vector<strview>;
}
def main(args: span<strview>): void {
val asked := args::parse_or_exit::<greeting>(args);
for &name in asked.names {
for _ in 0..asked.times {
io::println(asked.loud ? $"HELLO, {name}!" : $"Hello, {name}.");
}
}
}
What it reads
The marks are args::command, args::option, args::argument, args::group, and
args::choice, written with args::, as above, or without it, as below, where the file
imports std::args.
- Options are read by their long names,
--times 2or--times=2, which are their fields' with each_written-, and by short names,-n 2or-n2. Aboolis a switch, and switches are written together, as-ln2. A value may begin with-, and--ends the options, as a lone-is positional. - Values: integers within their range, reals,
strview,string,path, an enum by the names of its members marked@choice, and a program's own type implementingargs::value.maybemay be left out, and avectortakes each one written.count = truecounts how often an integer's option is written, as-vvv, and any other option keeps the last one written. - Positional arguments go to the
@argumentfields in turn: a plain one takes one, amaybeone where one is left over, avectorwhat is left over, and the one markedrest = truewhat follows--. - Subcommands: a union marked
@command, whose fields are marked@command, is either whatparseis given or a struct's last argument. Its word picks the field, whose own struct reads the arguments after it. - Groups: options several commands share are written once, in a struct marked
@group. A field of that type, also marked@group, reads them as if the command declared them in its place, and the help text shows them there. A group may hold groups, but no positional arguments. - Help and mistakes:
-h,--help, and, where a command gives aversion,--versionprint the help text or the version and exit with 0, and a mistake in the arguments prints why and exits with 2.args::parsegives each as anargs::errorinstead, andargs::help::<T>(args)andargs::usage::<T>(args)give the text of the subcommand the arguments reach. A mistake in the marks, as two options of one name, stops a checked program the first time it runs.
Testing
From the standard library's root, blossom test --suite std.args runs them, or:
sakurac run --lib std=. tests/main.sakura tests/args/*.sakura