#! @file args.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains what reading a program's arguments knows apart from the walk: the #! attributes a program marks the struct it reads them into with, the traits of #! the types read, the errors, and `args::parse` with the functions around it. #! The help text is written from the same marks, so it says what is read. import std::io; import system; module std::args; #! Marks a struct or union `args::parse` reads, and each field of a union of #! subcommands, which is a subcommand of the field's name. #! #! The marks are also what lets `std::args` see a type private to the program, #! since reflection gives a module what carries an attribute it declares. So a #! command is marked, and each field read, even where it is `pub`. @core::usage{ on = target::structs | target::unions | target::fields } pub struct command : attribute { pub val name: strview = ""; #! Its word, or the program's name in the help text, in place of the declaration's. pub val help: strview = ""; #! What it does, in a sentence or two. pub val version: strview = ""; #! What `--version` prints, where it is not empty. } #! Marks a field read from an option: `--name value` or `--name=value`, and, #! where it has a short name, `-n value` or `-nvalue`. @core::usage{ on = target::fields } pub struct option : attribute { pub val name: strview = ""; #! Its long name, without `--`, in place of the field's, whose `_` are written `-`. pub val short: u8 = 0; #! Its short name, a letter or digit, or 0 for none. pub val value: strview = ""; #! What the help text calls its value, as `DIR`, in place of its name in capitals. pub val help: strview = ""; #! What it does. pub val required: bool = false; #! Whether leaving it out is an error. pub val count: bool = false; #! Whether an integer counts how often it is written, as `-vvv`. } #! Marks a field read from the positional arguments, which go to such fields in #! the order they are declared. The last may be a union marked `@command`, #! whose subcommand the next positional argument names. @core::usage{ on = target::fields } pub struct argument : attribute { pub val value: strview = ""; #! What the help text calls it, as `FILE`, in place of its name in capitals. pub val help: strview = ""; #! What it is. pub val rest: bool = false; #! Whether it takes only what follows `--`. } #! Marks a struct of options several commands share, and each field of such a #! struct that a command reads, whose options are read, and shown in the help #! text, as if the command declared them in place of the field. A group holds #! options, and groups of them, but no positional arguments. @core::usage{ on = target::structs | target::fields } pub struct group : attribute {} #! Marks an enum member read by its name, which is the member's, whose `_` are #! written `-`, or the `name` given. Members left unmarked are not read. @core::usage{ on = target::members } pub struct choice : attribute { pub val name: strview = ""; #! The word for it, in place of the member's. pub val help: strview = ""; #! What it means. } #! A type an option or an argument is read as, from the text written. #! #! std's numbers, `strview`, `string`, `path`, and `maybe` of any of them #! implement it, and a program's own type may, by declaring it, as #! `struct celsius : args::value`. An enum whose members are marked `@choice` #! is read by their names without it. pub trait value { #! Reads a value from an argument. #! @param text The argument, as written. #! @returns The value, or why the text is not one, as `is not a whole number`. def from_argument(text: strview): result; } #! A type an option is a switch of: on where the option is written, which #! takes no value. Only `bool` is one. pub trait switch { #! The value a switch has where it is written. #! @returns The value. def switched_on(): self; } #! Why the arguments could not be read. pub enum error_kind: u8 { unknown_option, #! An option no field takes, as `--colour`. missing_value, #! An option taking a value ends the arguments. invalid_value, #! A value its type refuses, as `300` for a `u8`. switch_given_value, #! A switch written with a value, as `--verbose=yes`. missing_option, #! A `required` option is left out. missing_argument, #! A positional argument that is not a `maybe` or `vector` is left out. unexpected_argument, #! A positional argument no field takes. unknown_command, #! A word that names no subcommand. missing_command, #! The arguments end where a subcommand is needed. help_asked, #! `-h` or `--help` was written. version_asked, #! `--version` was written. } #! Why the arguments could not be read, and where. #! #! An error is plain data, viewing the arguments and the marks, so `else (e)` #! names it, and it lasts as long as the arguments do. pub struct error { pub var kind: error_kind; #! What went wrong. pub var at: u64; #! The argument's index, or how many there are, at their end. pub var text: strview; #! That argument, as written, or the value alone where one is wrong, as `300` of `-O300`. pub var name: strview; #! The option, as written, or the argument, as the help text names it. pub var reason: strview; #! Why the value was refused, from `value`, or empty. } #! Says what went wrong, in a sentence. #! @param this The error to describe. #! @returns The sentence, as "`-O 300`: 300 is not between 0 and 255." pub def error::describe(&this): string { return match this.kind { unknown_option => $"`{this.name}` is not an option.", missing_value => $"`{this.name}` needs a value.", invalid_value => this.name.length != 0 && this.name[0] == '-' ? $"`{this.name} {this.text}`: {this.text} {this.reason}." : $"{this.name} `{this.text}` {this.reason}.", switch_given_value => $"`{this.name}` takes no value, but is given `{this.text}`.", missing_option => $"`{this.name}` is required.", missing_argument => $"{this.name} is missing.", unexpected_argument => $"`{this.text}` is more than the arguments taken.", unknown_command => $"`{this.text}` is not a command.", missing_command => string::from("A command is needed.", nothing {}), help_asked => string::from("The help text is asked for.", nothing {}), version_asked => string::from("The version is asked for.", nothing {}), }; } #! Reads the arguments into a `T`, a struct or union marked `@command`. #! #! Each field marked `@option` is read from the option of its name, each marked #! `@argument` from the positional arguments, in turn, and each marked `@group` #! from the options of its struct, as the README's "Command-line arguments" #! says. A field left unmarked, or left out of the arguments, keeps its default, #! or else its zero value. Before the arguments are read, the marks of `T` and of #! every subcommand and group within it are checked, and a mistake in them, as #! two options of one name, stops the program, so it is found the first time the #! program runs, whatever it is given. #! @tparam T The command, which is `zeroable`, so a field left unread has a #! value. #! @param args The arguments, as `main` is given them. #! @returns The command read, or why the arguments are not one of it, which is #! `help_asked` or `version_asked` where they ask for those. pub def parse(args: span): result where T: zeroable { var reached := reach { words = vector::::new() }; return read_command::(args, &mut reached); } #! Reads the arguments into a `T`, as `parse` does, or else writes why not and #! stops the program: the help text to the standard output, or the version, #! with status 0, or the error to the standard error, with a line saying where #! the help text is, with status 2. #! @tparam T The command, which is `zeroable`. #! @param args The arguments, as `main` is given them. #! @returns The command read. pub def parse_or_exit(args: span): T where T: zeroable { var reached := reach { words = vector::::new() }; var made := read_command::(args, &mut reached) else (wrong) { stop::(&wrong, &reached); var none: T; return move(none); }; return move(made); } #! The help text of the command the arguments reach, so `tool build --help` #! gives `build`'s: its usage, what it does, its arguments, its subcommands, #! and its options, each with what it does. #! @tparam T The command, which is `zeroable`, so the defaults of its #! options are shown. #! @param args The arguments, of which only the subcommands are followed. #! @returns The text, ending in a line break. pub def help(args: span = span::::blank()): string where T: zeroable { var reached := reach { words = vector::::new() }; _ = read_command::(args, &mut reached); val program := program_name::(); return help_at::(reached.words.span, 0, program, "", true); } #! The usage of the command the arguments reach, in one line. #! @tparam T The command, which is `zeroable`. #! @param args The arguments, of which only the subcommands are followed. #! @returns The line, as "usage: sakura build [options] FILE...", without a #! line break. pub def usage(args: span = span::::blank()): string where T: zeroable { var reached := reach { words = vector::::new() }; _ = read_command::(args, &mut reached); val program := program_name::(); return help_at::(reached.words.span, 0, program, "", false); } # What reading the arguments reached, which the help text and the report of an # error follow: the word of each subcommand the arguments named, in turn, and # the version of the command `--version` was written to. struct reach { var words: vector; var version: strview; } # Writes why the arguments could not be read, or the help text or version they # ask for, and stops the program. def stop(wrong: &error, reached: &reach): void where T: zeroable { if wrong.kind == error_kind::help_asked { val text := help_at::(reached.words.span, 0, program_name::(), "", true); print(text.span); system::exit(0); } if wrong.kind == error_kind::version_asked { print($"{reached.version}\n"); system::exit(0); } var named := string::from(program_name::(), nothing {}); for &each in reached.words { named.append(" "); named.append(each); } val described := wrong.describe(); val said := $"{program_name::()}: {described}\nTry `{named} --help`.\n"; _ = io::error_output().write(said.span); system::exit(2); }