Command_Line module

parse_arguments

procedureSource

Reads the program's arguments into a struct. Each field of T is an option: a bool is a switch that is set by its name alone, anything else takes the next argument as its value (-count 3), and a fixed-size array takes one argument per element. Returns whether it worked, the filled struct, an Is_Set of which options appeared, and the arguments that were not options. An unknown option, a missing or bad value or a repeated option makes it fail. Asking for -help prints the options and fails.

parse_arguments :: (
    $T: Type,
    flags := Default_Argument_Flags,
    help_triggers: [] string = .["help", "HELP", "?"],
    args: [] string = .{ count = -1 }
) -> (success: bool, result: T, is_set: Is_Set(T), free_arguments: [..] string)

Parameters

$T: Type
flags := Default_Argument_Flags
see Argument_Flags.
help_triggers: [] string = .["help", "HELP", "?"]
the option names that ask for help.
args: [] string = .{ count = -1 }
the arguments to read, without the program name. The program's own command line when not given.

Returns

success: bool
result: T
is_set: Is_Set(T)
free_arguments: [..] string
Example↗
Options :: struct {
    verbose: bool;
    count := 1;
    name: string;
}

ok, options, is_set, files := parse_arguments(Options, args = string.["-verbose", "-count", "3", "a.txt"]);
assert(ok && options.verbose && options.count == 3);
assert(!is_set.name && files.count == 1 && files[0] == "a.txt");

See also