Files
std/args
2026-11-10 11:26:34 -06:00
..
2026-11-10 11:26:34 -06:00
2026-11-10 11:26:34 -06:00
2026-11-10 11:26:34 -06:00
2026-11-10 11:26:34 -06:00

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 2 or --times=2, which are their fields' with each _ written -, and by short names, -n 2 or -n2. A bool is 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 implementing args::value. maybe may be left out, and a vector takes each one written. count = true counts how often an integer's option is written, as -vvv, and any other option keeps the last one written.
  • Positional arguments go to the @argument fields in turn: a plain one takes one, a maybe one where one is left over, a vector what is left over, and the one marked rest = true what follows --.
  • Subcommands: a union marked @command, whose fields are marked @command, is either what parse is 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 a version, --version print the help text or the version and exit with 0, and a mistake in the arguments prints why and exits with 2. args::parse gives each as an args::error instead, and args::help::<T>(args) and args::usage::<T>(args) give the text of the subcommand the arguments reach. A mistake in the marks, as two options of one name, stops the program the first time it runs.

Testing

From the standard library's root, blossom test --only std.args runs them, or:

sakurac run --lib std=. tests/main.sakura tests/args/*.sakura