Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-10-28 00:51:45 -05:00
commit b176c3d6d1
1625 files changed
+43245

No files matched your search

+996
View File
@@ -0,0 +1,996 @@
#! @file walk.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the walk `args::parse` reads the arguments by, through what the
#! program marks of the command it is given, the checks of those marks, and the
#! forms of the types std gives: numbers, text, paths, `maybe`, and vectors.
#!
#! A field's type decides how it is read, which `if const` tells by the traits
#! the type implements, and by the kind of its declaration, which only a type
#! the program or std declares has: a switch, a `value`, a vector, an enum of
#! choices, or a union of subcommands. Each step on a field is a function
#! deciding so itself, since `if const` decides by types and kinds alone, not by
#! what a function gives.
#!
#! The options of a command are counted in the order they are declared, those
#! of a group where the group's field is, so each step on an option given by its
#! index goes into the group holding it with the index within the group.
import std::semver;
import std::io;
module std::args;
# A type each argument for it is added to, as a vector is, rather than the last
# being kept.
trait repeated {
def add_argument(&mut this, text: strview): result<void, strview>;
}
# A type that may be left out, as `maybe` is, so a positional argument of it
# takes one only where one is left over.
trait vacant {
def vacant_argument(): self;
}
# How many positional arguments a field takes: one, one where one is left over,
# or as many as are left over.
enum shape: u8 { one, optional, many }
# The index of no option.
const no_option: u64 = 0xFFFFFFFFFFFFFFFF;
# Reads the arguments into a command, after checking its marks.
def read_command<T>(args: span<strview>, reached: &mut reach): result<T, error> where T: zeroable {
check_command::<T>();
return read_part::<T>(args, 0, reached);
}
# Reads the arguments from an index into a command: a struct of options and
# arguments, or a union of subcommands.
def read_part<T>(args: span<strview>, from: u64, reached: &mut reach): result<T, error> where T: zeroable {
if const T is switch || T is value || T is repeated {
compile_error("The type is not `@command`, so its arguments are not read. Mark a struct or union `@command`, and each field to read `@option` or `@argument`.");
return error { kind = error_kind::missing_command, at = from };
} else if const attached::<command>(decl_of::<T>()).length == 0 {
compile_error("The type is not `@command`, so its arguments are not read. Mark it `@command`, and each field to read `@option` or `@argument`.");
return error { kind = error_kind::missing_command, at = from };
} else if const decl_of::<T>().kind == target::unions {
return read_union::<T>(args, from, reached);
} else {
return read_struct::<T>(args, from, reached);
}
}
# Reads a union of subcommands: the options it has, which are only those asking
# for the help text and the version, then the word naming the subcommand, then
# the subcommand's own arguments.
def read_union<U>(args: span<strview>, from: u64, reached: &mut reach): result<U, error> where U: zeroable {
var at := from;
while at < args.length {
val arg := args[at];
if arg == "--" {
at++;
break;
}
break if arg.length < 2 || arg[0] != '-';
if arg == "-h" || arg == "--help" {
return asked(error_kind::help_asked, at, arg);
}
val version := attached::<command>(decl_of::<U>())[0].version;
if arg == "--version" && version.length != 0 {
reached.version = version;
return asked(error_kind::version_asked, at, arg);
}
return error { kind = error_kind::unknown_option, at = at, text = arg, name = arg };
}
if at >= args.length {
return error { kind = error_kind::missing_command, at = args.length };
}
val word := args[at];
for const field in members_of(decl_of::<U>()) {
if const field.kind == target::fields && attached::<command>(field).length > 0 {
if named_as(word, field.name, attached::<command>(field)[0].name) {
reached.words.append(word);
var sub := read_part::<type_of(field)>(args, at + 1, reached) else (wrong) { return wrong; };
var made: U;
made.(field) = move(sub);
return move(made);
}
}
}
return error { kind = error_kind::unknown_command, at = at, text = word, name = word };
}
# Reads a struct of options and positional arguments, the last of which may be
# a union of subcommands, which every argument after its word belongs to.
def read_struct<T>(args: span<strview>, from: u64, reached: &mut reach): result<T, error> where T: zeroable {
var made: T;
var seen := vector::<u64>::new();
var positional := vector::<u64>::new();
var after := vector::<u64>::new();
val before := arguments_before_command::<T>();
val commanded := before != no_option;
var named := false;
var ended := false;
var at := from;
while at < args.length {
val arg := args[at];
var plain := ended;
if !ended {
if arg == "--" {
ended = true;
} else if arg.length > 2 && arg[0] == '-' && arg[1] == '-' {
_ = read_long::<T>(&mut made, args, &mut at, &mut seen, reached) else (wrong) { return wrong; };
} else if arg.length > 1 && arg[0] == '-' && !(arg[1].is_digit && !digit_named::<T>()) {
_ = read_short::<T>(&mut made, args, &mut at, &mut seen, reached) else (wrong) { return wrong; };
} else {
plain = true;
}
}
if plain {
# The positional argument after those the fields before it take names a
# subcommand, which reads the rest.
if commanded && positional.length + after.length == before {
_ = read_subcommand::<T>(&mut made, args, at, reached) else (wrong) { return wrong; };
named = true;
break;
}
if ended {
after.append(at);
} else {
positional.append(at);
}
}
at++;
}
if commanded && !named {
return error { kind = error_kind::missing_command, at = args.length };
}
val left := required_left::<T>(&seen, 0);
if left.length != 0 {
return error { kind = error_kind::missing_option, at = args.length, name = keep($"--{left}") };
}
_ = read_positional::<T>(&mut made, args, &mut positional, &after) else (wrong) { return wrong; };
return move(made);
}
# Reads an option written by its long name, as `--name value` or `--name=value`,
# taking its value from the next argument where none is attached, or the help
# text or the version where it asks for them.
def read_long<T>(made: &mut T, args: span<strview>, at: &mut u64, seen: &mut vector<u64>, reached: &mut reach): result<void, error> where T: zeroable {
val arg := args[at];
var name := arg.substr(2, arg.length - 2);
var value: strview = "";
var joined := false;
for i in 0..name.length {
if name[i] == '=' {
value = name.substr(i + 1, name.length - i - 1);
name = name.substr(0, i);
joined = true;
break;
}
}
val written := arg.substr(0, name.length + 2);
val which := option_named::<T>(name);
if which == no_option {
if name == "help" && !joined && helps::<T>(false) {
return asked(error_kind::help_asked, at, arg);
}
val version := attached::<command>(decl_of::<T>())[0].version;
if name == "version" && !joined && version.length != 0 {
reached.version = version;
return asked(error_kind::version_asked, at, arg);
}
return error { kind = error_kind::unknown_option, at = at, text = arg, name = written };
}
seen.append(which);
if !takes_value::<T>(which) {
if joined {
return error { kind = error_kind::switch_given_value, at = at, text = value, name = written };
}
return take_option::<T>(made, which, "", at, written);
}
if !joined {
if at + 1 >= args.length {
return error { kind = error_kind::missing_value, at = args.length, text = arg, name = written };
}
at++;
value = args[at];
}
return take_option::<T>(made, which, value, at, written);
}
# Reads options written by their short names, as `-v`, `-vq`, `-O 2`, or `-vO2`:
# switches may be written together, and the last written together may take a
# value, which is what follows it, or the next argument.
def read_short<T>(made: &mut T, args: span<strview>, at: &mut u64, seen: &mut vector<u64>, reached: &mut reach): result<void, error> where T: zeroable {
val arg := args[at];
var i := 1u;
while i < arg.length {
val letter := arg[i];
val written := short_written(letter);
val which := option_lettered::<T>(letter);
if which == no_option {
if letter == 'h' && helps::<T>(true) {
return asked(error_kind::help_asked, at, arg);
}
return error { kind = error_kind::unknown_option, at = at, text = arg, name = written.length == 2 ? written : arg };
}
seen.append(which);
if !takes_value::<T>(which) {
_ = take_option::<T>(made, which, "", at, written) else (wrong) { return wrong; };
i++;
continue;
}
if i + 1 < arg.length {
return take_option::<T>(made, which, arg.substr(i + 1, arg.length - i - 1), at, written);
}
if at + 1 >= args.length {
return error { kind = error_kind::missing_value, at = args.length, text = arg, name = written };
}
at++;
return take_option::<T>(made, which, args[at], at, written);
}
return;
}
# Reads the subcommand whose word is at an index into the field holding the
# union of them.
def read_subcommand<T>(made: &mut T, args: span<strview>, at: u64, reached: &mut reach): result<void, error> where T: zeroable {
for const field in members_of(decl_of::<T>()) {
if const field.kind == target::fields && attached::<argument>(field).length > 0 {
_ = read_into::<type_of(field)>(&mut made.(field), args, at, reached) else (wrong) { return wrong; };
}
}
return;
}
# Reads a subcommand into a field, where the field holds a union of them, and
# does nothing to any other.
def read_into<V>(place: &mut V, args: span<strview>, at: u64, reached: &mut reach): result<void, error> {
if const V is switch || V is value || V is repeated {
return;
} else if const decl_of::<V>().kind == target::unions && attached::<command>(decl_of::<V>()).length > 0 {
if const V is zeroable {
var sub := read_part::<V>(args, at, reached) else (wrong) { return wrong; };
place = move(sub);
}
return;
} else {
return;
}
}
# Reads the positional arguments into the fields marked `@argument`, in the
# order they are declared: one takes one, a `maybe` one where one is left over
# for those after it, and a vector as many as are left over. The arguments after
# `--` go to the one taking the rest, where there is one, and are otherwise read
# as the others are.
def read_positional<T>(made: &mut T, args: span<strview>, positional: &mut vector<u64>, after: &vector<u64>): result<void, error> where T: zeroable {
var shapes := vector::<shape>::new();
var rests := vector::<bool>::new();
for const field in members_of(decl_of::<T>()) {
if const field.kind == target::fields && attached::<argument>(field).length > 0 {
if !is_command::<type_of(field)>() {
shapes.append(shape_of::<type_of(field)>());
rests.append(attached::<argument>(field)[0].rest);
}
}
}
var resting := false;
for &each in rests {
resting = resting || each;
}
if !resting {
for &each in after {
positional.append(each);
}
}
# How many each takes, leaving those after it the one each needs.
var counts := vector::<u64>::new();
var left := positional.length;
for k in 0..shapes.length {
var needed := 0u;
for j in k + 1..shapes.length {
if !rests[j] && shapes[j] == shape::one {
needed++;
}
}
var taken := 0u;
if !rests[k] {
if shapes[k] == shape::one {
taken = left > 0 ? 1 : 0;
} else if shapes[k] == shape::optional {
taken = left > needed ? 1 : 0;
} else {
taken = left > needed ? left - needed : 0;
}
}
counts.append(taken);
left -= taken;
}
if left > 0 {
val extra := positional[positional.length - left];
return error { kind = error_kind::unexpected_argument, at = extra, text = args[extra] };
}
var k := 0u;
var next := 0u;
for const field in members_of(decl_of::<T>()) {
if const field.kind == target::fields && attached::<argument>(field).length > 0 {
if !is_command::<type_of(field)>() {
val name := attached::<argument>(field)[0].value;
if rests[k] {
for &each in after {
_ = store::<type_of(field)>(&mut made.(field), args[each]) else (why) {
return refused(each, args[each], argument_name(field.name, name), why);
};
}
} else {
if shapes[k] == shape::one && counts[k] == 0 {
return error { kind = error_kind::missing_argument, at = args.length, name = argument_name(field.name, name) };
}
for _ in 0..counts[k] {
val each := positional[next];
next++;
_ = store::<type_of(field)>(&mut made.(field), args[each]) else (why) {
return refused(each, args[each], argument_name(field.name, name), why);
};
}
}
k++;
}
}
}
return;
}
# Reads an option into its field, by its index among the options: a count adds
# one, a switch is turned on, and anything else reads the value written.
def take_option<T>(made: &mut T, which: u64, value: strview, at: u64, written: strview): result<void, error> where T: zeroable {
var index := 0u;
for const field in members_of(decl_of::<T>()) {
if const field.kind == target::fields && attached::<option>(field).length > 0 {
if index == which {
if attached::<option>(field)[0].count {
count_one::<type_of(field)>(&mut made.(field));
return;
}
if const type_of(field) is switch {
made.(field) = switched::<type_of(field)>();
return;
} else {
_ = store::<type_of(field)>(&mut made.(field), value) else (why) {
return refused(at, value, written, why);
};
return;
}
}
index++;
} else if const field.kind == target::fields && attached::<group>(field).length > 0 {
val held := options_in::<type_of(field)>();
if which < index + held {
return take_option::<type_of(field)>(&mut made.(field), which - index, value, at, written);
}
index += held;
}
}
return;
}
# How many options a struct holds, those of its groups among them.
def options_in<T>(): u64 {
var count := 0u;
for const field in members_of(decl_of::<T>()) {
if const field.kind == target::fields && attached::<option>(field).length > 0 {
count++;
} else if const field.kind == target::fields && attached::<group>(field).length > 0 {
count += options_in::<type_of(field)>();
}
}
return count;
}
# The index among the options of the one of a long name, or `no_option`.
def option_named<T>(name: strview): u64 {
var index := 0u;
for const field in members_of(decl_of::<T>()) {
if const field.kind == target::fields && attached::<option>(field).length > 0 {
if named_as(name, field.name, attached::<option>(field)[0].name) {
return index;
}
index++;
} else if const field.kind == target::fields && attached::<group>(field).length > 0 {
val found := option_named::<type_of(field)>(name);
if found != no_option {
return index + found;
}
index += options_in::<type_of(field)>();
}
}
return no_option;
}
# The index among the options of the one of a short name, or `no_option`.
def option_lettered<T>(letter: u8): u64 {
var index := 0u;
for const field in members_of(decl_of::<T>()) {
if const field.kind == target::fields && attached::<option>(field).length > 0 {
if attached::<option>(field)[0].short == letter {
return index;
}
index++;
} else if const field.kind == target::fields && attached::<group>(field).length > 0 {
val found := option_lettered::<type_of(field)>(letter);
if found != no_option {
return index + found;
}
index += options_in::<type_of(field)>();
}
}
return no_option;
}
# Whether the option of an index takes a value, which a switch and a count do
# not.
def takes_value<T>(which: u64): bool {
var index := 0u;
for const field in members_of(decl_of::<T>()) {
if const field.kind == target::fields && attached::<option>(field).length > 0 {
if index == which {
if attached::<option>(field)[0].count {
return false;
}
if const type_of(field) is switch {
return false;
} else {
return true;
}
}
index++;
} else if const field.kind == target::fields && attached::<group>(field).length > 0 {
val held := options_in::<type_of(field)>();
if which < index + held {
return takes_value::<type_of(field)>(which - index);
}
index += held;
}
}
return false;
}
# The long name of the first `required` option left out, counting the options
# from an index, or empty text where none is.
def required_left<T>(seen: &vector<u64>, from: u64): string {
var index := from;
for const field in members_of(decl_of::<T>()) {
if const field.kind == target::fields && attached::<option>(field).length > 0 {
if attached::<option>(field)[0].required && !holds(seen, index) {
return long_name(field.name, attached::<option>(field)[0].name);
}
index++;
} else if const field.kind == target::fields && attached::<group>(field).length > 0 {
val left := required_left::<type_of(field)>(seen, index);
if left.length != 0 {
return left;
}
index += options_in::<type_of(field)>();
}
}
return string::new(nothing {});
}
# Whether any option's short name is a digit, which makes `-5` an option rather
# than a positional argument.
def digit_named<T>(): bool {
for const field in members_of(decl_of::<T>()) {
if const field.kind == target::fields && attached::<option>(field).length > 0 {
if attached::<option>(field)[0].short.is_digit {
return true;
}
} else if const field.kind == target::fields && attached::<group>(field).length > 0 {
if digit_named::<type_of(field)>() {
return true;
}
}
}
return false;
}
# Whether the command has `-h`, where `short`, or else `--help`, which it has
# unless an option of its own takes the name.
def helps<T>(short: bool): bool {
for const field in members_of(decl_of::<T>()) {
if const field.kind == target::fields && attached::<option>(field).length > 0 {
if short && attached::<option>(field)[0].short == 'h' {
return false;
}
if !short && named_as("help", field.name, attached::<option>(field)[0].name) {
return false;
}
} else if const field.kind == target::fields && attached::<group>(field).length > 0 {
if !helps::<type_of(field)>(short) {
return false;
}
}
}
return true;
}
# How many positional arguments come before a struct's union of subcommands, or
# `no_option` where it has none.
def arguments_before_command<T>(): u64 {
var count := 0u;
for const field in members_of(decl_of::<T>()) {
if const field.kind == target::fields && attached::<argument>(field).length > 0 {
if is_command::<type_of(field)>() {
return count;
}
count++;
}
}
return no_option;
}
# Whether a type is a union of subcommands, which a field marked `@argument`
# holds. Only a type with a declaration of its own may be one, which the
# language's own types are not.
def is_command<V>(): bool {
if const V is switch || V is value || V is repeated {
return false;
} else if const decl_of::<V>().kind == target::unions && attached::<command>(decl_of::<V>()).length > 0 {
return true;
} else {
return false;
}
}
# How many positional arguments a field of a type takes.
def shape_of<V>(): shape {
if const V is repeated {
return shape::many;
} else if const V is vacant {
return shape::optional;
} else {
return shape::one;
}
}
# Stores what an argument gives into a field: one more element of what is
# repeated, or else the value read, in place of any before. A union of
# subcommands is read otherwise, and takes nothing here.
def store<V>(place: &mut V, text: strview): result<void, strview> {
if const V is repeated {
return place.add_argument(text);
} else if const V is value {
var read := V::from_argument(text) else (why) { return why; };
place = move(read);
return;
} else if const V is switch {
return "is a switch";
} else if const decl_of::<V>().kind == target::enums {
var read := read_value::<V>(text) else (why) { return why; };
place = move(read);
return;
} else {
return "is not read";
}
}
# Reads a value of a type from the text written: a `value` reads itself, and an
# enum is read by the name of a member marked `@choice`.
def read_value<V>(text: strview): result<V, strview> {
if const V is value {
return V::from_argument(text);
} else if const V is switch {
compile_error("A switch is turned on by its option alone, so it is not read from text: a `maybe` or `vector` of `bool` is not read.");
return "is not read";
} else if const decl_of::<V>().kind == target::enums {
for const member in members_of(decl_of::<V>()) {
if const member.kind == target::members && attached::<choice>(member).length > 0 {
if named_as(text, member.name, attached::<choice>(member)[0].name) {
return value_of(member);
}
}
}
return "is not one of the choices";
} else {
compile_error("The type is not read from an argument. A field read is a `bool` switch, an enum of members marked `@choice`, or what implements `args::value`, or a `maybe` or `vector` of one.");
return "is not read";
}
}
# Adds one to a count, which is an integer.
def count_one<V>(place: &mut V): void {
if const V is integer {
place = place + 1;
}
}
# The value a switch has where its option is written.
def switched<V>(): V where V: switch {
return V::switched_on();
}
# Whether the indices hold one.
def holds(indices: &vector<u64>, index: u64): bool {
for &each in indices {
return true if each == index;
}
return false;
}
# The error of a value its type refused, at the argument holding it.
def refused(at: u64, text: strview, name: strview, why: strview): error {
return error { kind = error_kind::invalid_value, at = at, text = text, name = name, reason = why };
}
# The error of the help text or the version asked for, at the argument asking.
def asked(kind: error_kind, at: u64, text: strview): error {
return error { kind = kind, at = at, text = text, name = text };
}
# Whether a word names what is declared: the name given in its mark, where it
# is not empty, as it is written, or else the declaration's, whose `_` are
# written `-`.
def named_as(word: strview, declared: strview, given: strview): bool {
if given.length != 0 {
return word == given;
}
if word.length != declared.length {
return false;
}
for i in 0..word.length {
if word[i] != (declared[i] == '_' ? '-' : declared[i]) {
return false;
}
}
return true;
}
# A name as it is written: the name given in a mark, where it is not empty, or
# else the declaration's, whose `_` are written `-`.
def long_name(declared: strview, given: strview): string {
if given.length != 0 {
return string::from(given, nothing {});
}
var made := string::new(nothing {});
for each in declared {
made.push(each == '_' ? '-' : each);
}
return made;
}
# What the help text calls a positional argument, or an option's value: the
# name given in its mark, or else the declaration's in capitals.
def value_name(declared: strview, given: strview): string {
if given.length != 0 {
return string::from(given, nothing {});
}
var made := string::new(nothing {});
for each in declared {
made.push(each.to_upper);
}
return made;
}
# What an error calls a positional argument, as the help text does.
def argument_name(declared: strview, given: strview): strview {
return given.length != 0 ? given : keep(value_name(declared, given));
}
# Each short name as written, `-` and the letter or digit, which an error views.
const shorts: strview = "-0-1-2-3-4-5-6-7-8-9-A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z-a-b-c-d-e-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z";
# A short name as written, as an error names it, or `-` alone for a byte that
# is no letter or digit.
def short_written(letter: u8): strview {
for i in 0..shorts.length / 2 {
if shorts[i * 2 + 1] == letter {
return shorts.substr(i * 2, 2);
}
}
return shorts.substr(0, 1);
}
# Text made for an error, which views it, kept for as long as the program runs,
# as an error's views of the arguments are.
var kept: vector<string>;
# Keeps text for as long as the program runs.
def keep(text: string): strview {
kept.append(move(text));
return kept[kept.length - 1].view();
}
# Checks the marks of a command, and of each command within it: while compiling,
# that each field read is of a type read from an argument, and before reading,
# what `if const` cannot decide, stopping the program with an `asserts` at the
# first mistake.
def check_command<T>(): void {
if const decl_of::<T>().kind == target::unions {
var names := vector::<string>::new();
for const field in members_of(decl_of::<T>()) {
if const field.kind == target::fields && attached::<command>(field).length > 0 {
if const type_of(field) is switch || type_of(field) is value || type_of(field) is repeated {
compile_error("A subcommand is a struct or union marked `@command`.", field.at);
} else if const attached::<command>(decl_of::<type_of(field)>()).length == 0 {
compile_error("A subcommand is a struct or union marked `@command`.", field.at);
} else {
check_command::<type_of(field)>();
}
var name := long_name(field.name, attached::<command>(field)[0].name);
for &other in names {
asserts(other.view() != name.view(), $"Two subcommands are named `{name}`.");
}
names.append(move(name));
}
}
} else {
var longs := vector::<string>::new();
var letters := vector::<u8>::new();
check_options::<T>(&mut longs, &mut letters);
var rests := 0u;
var commanded := false;
var single := true;
for const field in members_of(decl_of::<T>()) {
if const field.kind == target::fields && attached::<argument>(field).length > 0 {
asserts(!commanded, "A union of subcommands is the last argument.");
if const type_of(field) is switch {
compile_error("A switch is an option, which a positional argument is not.", field.at);
} else if const type_of(field) is value || type_of(field) is repeated {
} else if const decl_of::<type_of(field)>().kind == target::enums {
} else if const decl_of::<type_of(field)>().kind == target::unions && attached::<command>(decl_of::<type_of(field)>()).length > 0 {
asserts(single, "The arguments before a union of subcommands each take one.");
commanded = true;
check_command::<type_of(field)>();
} else {
compile_error("The type is not read from an argument. An argument is an enum of members marked `@choice`, or what implements `args::value`, or a `maybe` or `vector` of one, or a union of subcommands.", field.at);
}
if !commanded {
val rest := attached::<argument>(field)[0].rest;
single = single && !rest && shape_of::<type_of(field)>() == shape::one;
if rest {
rests++;
asserts(shape_of::<type_of(field)>() == shape::many, "The argument taking what follows `--` is a vector.");
asserts(rests <= 1, "Only one argument takes what follows `--`.");
}
}
}
}
}
}
# Checks the marks of the options of a command, or of a group within it, and of
# the groups within that, which share the names the options are given: while
# compiling, that each is of a type read from an argument, and that a group is
# a struct of options marked `@group`, and before reading, that no two options
# share a name, which an `asserts` stops the program at.
def check_options<T>(longs: &mut vector<string>, letters: &mut vector<u8>): void {
for const field in members_of(decl_of::<T>()) {
if const field.kind == target::fields && attached::<option>(field).length > 0 && attached::<argument>(field).length > 0 {
compile_error("A field is read from an option or from the positional arguments, not both.", field.at);
}
if const field.kind == target::fields && attached::<group>(field).length > 0 && (attached::<option>(field).length > 0 || attached::<argument>(field).length > 0) {
compile_error("A group's field is read from the options of its struct, so it is marked only `@group`.", field.at);
}
if const field.kind == target::fields && attached::<option>(field).length > 0 {
if const type_of(field) is switch || type_of(field) is value || type_of(field) is repeated {
} else if const decl_of::<type_of(field)>().kind == target::enums {
} else if const decl_of::<type_of(field)>().kind == target::unions && attached::<command>(decl_of::<type_of(field)>()).length > 0 {
compile_error("Subcommands are read from the positional arguments, so their union is marked `@argument`.", field.at);
} else {
compile_error("The type is not read from an argument. An option is a `bool` switch, an enum of members marked `@choice`, or what implements `args::value`, or a `maybe` or `vector` of one.", field.at);
}
val marked := attached::<option>(field)[0];
var name := long_name(field.name, marked.name);
for &other in longs {
asserts(other.view() != name.view(), $"Two options are named `--{name}`.");
}
longs.append(move(name));
if marked.short != 0 {
asserts(marked.short.is_alnum, "A short name is a letter or a digit.");
for &other in letters {
asserts(other != marked.short, $"Two options are named `{short_written(marked.short)}`.");
}
letters.append(marked.short);
}
asserts(!marked.count || counts::<type_of(field)>(), "A `count` option is an integer.");
}
if const field.kind == target::fields && attached::<group>(field).length > 0 {
if const type_of(field) is switch || type_of(field) is value || type_of(field) is repeated {
compile_error("A group is a struct of options marked `@group`.", field.at);
} else if const decl_of::<type_of(field)>().kind != target::structs || attached::<group>(decl_of::<type_of(field)>()).length == 0 {
compile_error("A group is a struct of options marked `@group`.", field.at);
} else {
check_group::<type_of(field)>(longs, letters);
}
}
}
}
# Checks the marks of a group, which holds options and groups of them, as
# `check_options` does, and no positional argument.
def check_group<G>(longs: &mut vector<string>, letters: &mut vector<u8>): void {
for const field in members_of(decl_of::<G>()) {
if const field.kind == target::fields && attached::<argument>(field).length > 0 {
compile_error("A group holds options, and groups of them, but no positional argument.", field.at);
}
}
check_options::<G>(longs, letters);
}
# Whether a type counts how often its option is written, as an integer does.
def counts<V>(): bool {
if const V is integer {
return true;
} else {
return false;
}
}
def bool::switched_on(): bool { return true; }
def i8::from_argument(text: strview): result<i8, strview> {
val read := signed_within(text, 0 - 128, 127) else (why) { return why; };
return read as i8;
}
def i16::from_argument(text: strview): result<i16, strview> {
val read := signed_within(text, 0 - 32768, 32767) else (why) { return why; };
return read as i16;
}
def i32::from_argument(text: strview): result<i32, strview> {
val read := signed_within(text, 0 - 2147483648, 2147483647) else (why) { return why; };
return read as i32;
}
def i64::from_argument(text: strview): result<i64, strview> {
return signed_within(text, 0 - 9223372036854775807 - 1, 9223372036854775807);
}
def u8::from_argument(text: strview): result<u8, strview> {
val read := unsigned_within(text, 255) else (why) { return why; };
return read as u8;
}
def u16::from_argument(text: strview): result<u16, strview> {
val read := unsigned_within(text, 65535) else (why) { return why; };
return read as u16;
}
def u32::from_argument(text: strview): result<u32, strview> {
val read := unsigned_within(text, 4294967295) else (why) { return why; };
return read as u32;
}
def u64::from_argument(text: strview): result<u64, strview> {
return unsigned_within(text, 18446744073709551615);
}
def f32::from_argument(text: strview): result<f32, strview> {
val read := real_of(text) else (why) { return why; };
if read > 3.4028234663852886e38 || read < -3.4028234663852886e38 {
return "is too large";
}
return read as f32;
}
def f64::from_argument(text: strview): result<f64, strview> {
return real_of(text);
}
# A view of the argument, which lasts as long as the arguments do.
def strview::from_argument(text: strview): result<strview, strview> {
return result::<strview, strview> { value = text };
}
def string::from_argument(text: strview): result<string, strview> {
return string::from(text, nothing {});
}
def path::from_argument(text: strview): result<path, strview> {
var read := path::parse(text) else (_) { return "is not a path"; };
return move(read);
}
def maybe<T>::from_argument(text: strview): result<maybe<T>, strview> {
var read := read_value::<T>(text) else (why) { return why; };
var made: maybe<T> = move(read);
return move(made);
}
def maybe<T>::vacant_argument(): maybe<T> {
return nothing {};
}
def vector<T>::add_argument(&mut this, text: strview): result<void, strview> {
var read := read_value::<T>(text) else (why) { return why; };
this.append(move(read));
return;
}
# A whole number written in decimal, or after `0x`, `0o`, or `0b`, where `_` may
# separate digits, and a sign may come first: how large it is, and its sign.
struct whole {
var magnitude: u64;
var negative: bool;
}
# Reads a whole number, giving nothing where the text is no such number, or one
# past 64 bits.
def whole_of(text: strview): maybe<whole> {
var at := 0u;
var negative := false;
if text.length > 0 && (text[0] == '-' || text[0] == '+') {
negative = text[0] == '-';
at = 1;
}
var base := 10u;
if text.length > at + 1 && text[at] == '0' {
val marker := text[at + 1];
if marker == 'x' || marker == 'X' { base = 16; }
if marker == 'o' || marker == 'O' { base = 8; }
if marker == 'b' || marker == 'B' { base = 2; }
if base != 10 { at += 2; }
}
var digits := 0u;
var magnitude := 0u;
while at < text.length {
val byte := text[at];
at++;
continue if byte == '_' && digits != 0;
return nothing {} if !byte.is_alnum;
val digit := byte.digit_value;
return nothing {} if digit >= base;
return nothing {} if magnitude > (18446744073709551615 - digit) / base;
magnitude = magnitude * base + digit;
digits++;
}
return nothing {} if digits == 0;
return whole { magnitude = magnitude, negative = negative };
}
# A signed integer read from text, within bounds.
def signed_within(text: strview, low: i64, high: i64): result<i64, strview> {
val read := whole_of(text) else { return "is not a whole number"; };
val limit: u64 = read.negative ? (0 - (low + 1)) as u64 + 1 : high as u64;
if read.magnitude > limit {
return keep($"is not between {low} and {high}");
}
return read.negative ? (0 - read.magnitude) as i64 : read.magnitude as i64;
}
# An unsigned integer read from text, within a bound.
def unsigned_within(text: strview, high: u64): result<u64, strview> {
val read := whole_of(text) else { return "is not a whole number"; };
if read.magnitude > high || (read.negative && read.magnitude != 0) {
return keep($"is not between 0 and {high}");
}
return read.magnitude;
}
# A real number read from text.
def real_of(text: strview): result<f64, strview> {
return match real::parse::<f64>(text) {
value(read) => read,
error(_) => "is not a number",
};
}