Initial Commit
This commit is contained in:
1625 files changed
+43618
No files matched your search
@@ -0,0 +1,403 @@
|
||||
#! @file help.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the help text `args::help` and `args::usage` write from the marks of
|
||||
#! a command: its usage, what it does, its arguments, its subcommands, and its
|
||||
#! options, each with what it does, its default, and the choices it takes, in
|
||||
#! columns.
|
||||
import std::semver;
|
||||
import std::io;
|
||||
|
||||
module std::args;
|
||||
|
||||
|
||||
# A type whose default an option's help text shows, as std's numbers and text
|
||||
# are.
|
||||
trait shown {
|
||||
def shown_argument(&this): string;
|
||||
}
|
||||
|
||||
# A type holding values of another, whose choices an option's help text lists,
|
||||
# as a `maybe` or a vector of an enum does.
|
||||
trait listed {
|
||||
def list_choices(into: &mut page): void;
|
||||
}
|
||||
|
||||
# Where a row of the help text goes: under the arguments, the subcommands, or
|
||||
# the options, or under the option before it, as one of its choices.
|
||||
enum row_place: u8 { arguments, commands, options, choices }
|
||||
|
||||
# The rows of a help text, each a name and what it is, in turn.
|
||||
struct page {
|
||||
var places: vector<row_place>;
|
||||
var lefts: vector<string>;
|
||||
var rights: vector<string>;
|
||||
}
|
||||
|
||||
# Makes a help text with no rows.
|
||||
def page::new(): page {
|
||||
return page {
|
||||
places = vector::<row_place>::new(),
|
||||
lefts = vector::<string>::new(),
|
||||
rights = vector::<string>::new()
|
||||
};
|
||||
}
|
||||
|
||||
# Adds a row to a help text.
|
||||
def page::add(&mut this, place: row_place, left: string, right: string): void {
|
||||
this.places.append(place);
|
||||
this.lefts.append(move(left));
|
||||
this.rights.append(move(right));
|
||||
}
|
||||
|
||||
# Writes the sections of a help text, each a heading and its rows, whose names
|
||||
# are padded so what each is begins in one column, with an option's choices in
|
||||
# a column of their own under it.
|
||||
def page::write(&this, into: &mut string): void {
|
||||
var width := 0u;
|
||||
for i in 0..this.places.length {
|
||||
if this.places[i] != row_place::choices && this.lefts[i].length > width {
|
||||
width = this.lefts[i].length;
|
||||
}
|
||||
}
|
||||
width += 3;
|
||||
|
||||
val sections: row_place[3] = [row_place::arguments, row_place::commands, row_place::options];
|
||||
val headings: strview[3] = ["arguments", "commands", "options"];
|
||||
for s in 0u..3u {
|
||||
var any := false;
|
||||
for &each in this.places {
|
||||
any = any || each == sections[s];
|
||||
}
|
||||
continue if !any;
|
||||
|
||||
into.append($"\n{headings[s]}:\n");
|
||||
for i in 0..this.places.length {
|
||||
continue if this.places[i] != sections[s];
|
||||
line(into, this.lefts[i].span, width, this.rights[i].span);
|
||||
|
||||
# The choices after an option are padded to the widest of them.
|
||||
var end := i + 1;
|
||||
var widest := 0u;
|
||||
while end < this.places.length && this.places[end] == row_place::choices {
|
||||
widest = this.lefts[end].length > widest ? this.lefts[end].length : widest;
|
||||
end++;
|
||||
}
|
||||
for j in i + 1..end {
|
||||
var left := spaces(width + 2);
|
||||
left.append(this.lefts[j].span);
|
||||
line(into, left.span, width + 4 + widest, this.rights[j].span);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Writes a row: its name, padded to a width where something follows it, and
|
||||
# what follows.
|
||||
def line(into: &mut string, left: strview, width: u64, right: strview): void {
|
||||
into.append(left);
|
||||
if right.length != 0 {
|
||||
for _ in left.length..width {
|
||||
into.push(' ');
|
||||
}
|
||||
into.append(right);
|
||||
}
|
||||
into.append("\n");
|
||||
}
|
||||
|
||||
# Text of a number of spaces.
|
||||
def spaces(count: u64): string {
|
||||
var made := string::new(nothing {});
|
||||
for _ in 0..count {
|
||||
made.push(' ');
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# The name of the program, as the help text and errors call it: the command's
|
||||
# `name`, or else its declaration's.
|
||||
def program_name<T>(): strview {
|
||||
if const T is switch || T is value || T is repeated {
|
||||
return "";
|
||||
} else {
|
||||
val given := attached::<command>(decl_of::<T>())[0].name;
|
||||
return given.length != 0 ? given : decl_of::<T>().name;
|
||||
}
|
||||
}
|
||||
|
||||
# The help text, or only its usage, of the command the words reach within a
|
||||
# command, through each subcommand they name in turn.
|
||||
def help_at<T>(words: span<strview>, depth: u64, path: strview, about: strview, whole: bool): string where T: zeroable {
|
||||
if const T is switch || T is value || T is repeated {
|
||||
return string::new(nothing {});
|
||||
} else if const decl_of::<T>().kind == target::unions {
|
||||
if depth < words.length {
|
||||
for const field in members_of(decl_of::<T>()) {
|
||||
if const field.kind == target::fields && attached::<command>(field).length > 0 {
|
||||
if named_as(words[depth], field.name, attached::<command>(field)[0].name) {
|
||||
val deeper := $"{path} {words[depth]}";
|
||||
return help_at::<type_of(field)>(words, depth + 1, deeper.span, attached::<command>(field)[0].help, whole);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return union_help::<T>(path, about, whole);
|
||||
} else {
|
||||
if depth < words.length {
|
||||
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 help_within::<type_of(field)>(words, depth, path, about, whole);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return struct_help::<T>(path, about, whole);
|
||||
}
|
||||
}
|
||||
|
||||
# The help text the words reach within a field's union of subcommands, or
|
||||
# nothing for any other field.
|
||||
def help_within<V>(words: span<strview>, depth: u64, path: strview, about: strview, whole: bool): string {
|
||||
if const V is switch || V is value || V is repeated {
|
||||
return string::new(nothing {});
|
||||
} else if const decl_of::<V>().kind == target::unions && attached::<command>(decl_of::<V>()).length > 0 {
|
||||
if const V is zeroable {
|
||||
return help_at::<V>(words, depth, path, about, whole);
|
||||
} else {
|
||||
return string::new(nothing {});
|
||||
}
|
||||
} else {
|
||||
return string::new(nothing {});
|
||||
}
|
||||
}
|
||||
|
||||
# The help text, or only its usage, of a union of subcommands.
|
||||
def union_help<U>(path: strview, about: strview, whole: bool): string {
|
||||
var made := $"usage: {path} [options] COMMAND ...";
|
||||
return made if !whole;
|
||||
made.append("\n");
|
||||
val own := attached::<command>(decl_of::<U>())[0].help;
|
||||
val said := own.length != 0 ? own : about;
|
||||
if said.length != 0 {
|
||||
made.append($"\n{said}\n");
|
||||
}
|
||||
|
||||
var rows := page::new();
|
||||
commands_of::<U>(&mut rows);
|
||||
helped(&mut rows, true, true, attached::<command>(decl_of::<U>())[0].version.length != 0);
|
||||
rows.write(&mut made);
|
||||
return made;
|
||||
}
|
||||
|
||||
# The help text, or only its usage, of a struct of options and arguments.
|
||||
def struct_help<T>(path: strview, about: strview, whole: bool): string where T: zeroable {
|
||||
var made := $"usage: {path} [options]";
|
||||
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)>() {
|
||||
made.append(" COMMAND ...");
|
||||
} else {
|
||||
val marked := attached::<argument>(field)[0];
|
||||
val name := value_name(field.name, marked.value);
|
||||
made.append(" ");
|
||||
made.append(argument_shown(name.span, shape_of::<type_of(field)>(), marked.rest).span);
|
||||
}
|
||||
}
|
||||
}
|
||||
return made if !whole;
|
||||
made.append("\n");
|
||||
val own := attached::<command>(decl_of::<T>())[0].help;
|
||||
val said := own.length != 0 ? own : about;
|
||||
if said.length != 0 {
|
||||
made.append($"\n{said}\n");
|
||||
}
|
||||
|
||||
var rows := page::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)>() {
|
||||
commands_within::<type_of(field)>(&mut rows);
|
||||
} else {
|
||||
val marked := attached::<argument>(field)[0];
|
||||
val name := value_name(field.name, marked.value);
|
||||
var left := string::from(" ", nothing {});
|
||||
var shown := argument_shown(name.span, shape_of::<type_of(field)>(), marked.rest);
|
||||
if marked.rest {
|
||||
shown = $"-- {name}...";
|
||||
}
|
||||
left.append(shown.span);
|
||||
rows.add(row_place::arguments, move(left), string::from(marked.help, nothing {}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var defaults: T;
|
||||
option_rows::<T>(&mut rows, &defaults);
|
||||
helped(&mut rows, helps::<T>(true), helps::<T>(false), attached::<command>(decl_of::<T>())[0].version.length != 0);
|
||||
rows.write(&mut made);
|
||||
return made;
|
||||
}
|
||||
|
||||
# Adds a row for each option of a command, or of a group within it, in the
|
||||
# order they are declared, those of a group where its field is: its names, and
|
||||
# what it does, with its default, read from what holds the defaults, or that it
|
||||
# is required, and its choices under it.
|
||||
def option_rows<T>(rows: &mut page, defaults: &T): void {
|
||||
for const field in members_of(decl_of::<T>()) {
|
||||
if const field.kind == target::fields && attached::<option>(field).length > 0 {
|
||||
val marked := attached::<option>(field)[0];
|
||||
var left := string::from(marked.short != 0 ? $" {short_written(marked.short)}, --" : " --", nothing {});
|
||||
left.append(long_name(field.name, marked.name).span);
|
||||
var switched := marked.count;
|
||||
if const type_of(field) is switch {
|
||||
switched = true;
|
||||
}
|
||||
if !switched {
|
||||
left.append(" ");
|
||||
left.append(value_name(marked.name.length != 0 ? marked.name : field.name, marked.value).span);
|
||||
}
|
||||
|
||||
var right := string::from(marked.help, nothing {});
|
||||
if marked.required {
|
||||
right.append(right.length != 0 ? " Required." : "Required.");
|
||||
} else if !marked.count {
|
||||
val shown := default_of::<type_of(field)>(&defaults.(field));
|
||||
if shown.length != 0 {
|
||||
right.append(right.length != 0 ? " Default: " : "Default: ");
|
||||
right.append(shown.span);
|
||||
right.append(".");
|
||||
}
|
||||
}
|
||||
rows.add(row_place::options, move(left), move(right));
|
||||
choices_of::<type_of(field)>(rows);
|
||||
} else if const field.kind == target::fields && attached::<group>(field).length > 0 {
|
||||
option_rows::<type_of(field)>(rows, &defaults.(field));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# How a positional argument is shown: as its name, in brackets where it may be
|
||||
# left out, and followed by `...` where it takes as many as are left over.
|
||||
def argument_shown(name: strview, taken: shape, rest: bool): string {
|
||||
if rest {
|
||||
return $"[-- {name}...]";
|
||||
}
|
||||
return match taken {
|
||||
shape::one => string::from(name, nothing {}),
|
||||
shape::optional => $"[{name}]",
|
||||
shape::many => $"{name}...",
|
||||
};
|
||||
}
|
||||
|
||||
# Adds the rows of the options every command has: `-h` and `--help`, where no
|
||||
# option of its own takes their names, and `--version`, where it gives one.
|
||||
def helped(rows: &mut page, short: bool, long: bool, version: bool): void {
|
||||
if short || long {
|
||||
val left: strview = short && long ? " -h, --help" : short ? " -h" : " --help";
|
||||
rows.add(row_place::options, string::from(left, nothing {}), string::from("Shows this help.", nothing {}));
|
||||
}
|
||||
if version {
|
||||
rows.add(row_place::options, string::from(" --version", nothing {}), string::from("Shows the version.", nothing {}));
|
||||
}
|
||||
}
|
||||
|
||||
# Adds a row for each subcommand of a field's union of them, or none for any
|
||||
# other field.
|
||||
def commands_within<V>(rows: &mut page): void {
|
||||
if const V is switch || V is value || V is repeated {
|
||||
} else if const decl_of::<V>().kind == target::unions && attached::<command>(decl_of::<V>()).length > 0 {
|
||||
commands_of::<V>(rows);
|
||||
}
|
||||
}
|
||||
|
||||
# Adds a row for each subcommand of a union of them: its word, and what it does,
|
||||
# as its mark in the union says, or else the mark of its own type.
|
||||
def commands_of<U>(rows: &mut page): void {
|
||||
for const field in members_of(decl_of::<U>()) {
|
||||
if const field.kind == target::fields && attached::<command>(field).length > 0 {
|
||||
val given := attached::<command>(field)[0].help;
|
||||
val said := given.length != 0 ? given : command_help::<type_of(field)>();
|
||||
var left := string::from(" ", nothing {});
|
||||
left.append(long_name(field.name, attached::<command>(field)[0].name).span);
|
||||
rows.add(row_place::commands, move(left), string::from(said, nothing {}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# What a command says it does, in its own mark.
|
||||
def command_help<V>(): strview {
|
||||
if const V is switch || V is value || V is repeated {
|
||||
return "";
|
||||
} else if const attached::<command>(decl_of::<V>()).length > 0 {
|
||||
return attached::<command>(decl_of::<V>())[0].help;
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
# Adds a row for each choice an option of a type takes, under it: those of an
|
||||
# enum, or of the enum a `maybe` or a vector holds.
|
||||
def choices_of<V>(rows: &mut page): void {
|
||||
if const V is listed {
|
||||
V::list_choices(rows);
|
||||
} else if const V is switch || V is value {
|
||||
} 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 {
|
||||
rows.add(
|
||||
row_place::choices,
|
||||
long_name(member.name, attached::<choice>(member)[0].name),
|
||||
string::from(attached::<choice>(member)[0].help, nothing {})
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# The default an option's help text shows, or nothing for a switch, a `maybe`,
|
||||
# a vector, empty text, and a program's own type.
|
||||
def default_of<V>(value: &V): string {
|
||||
if const V is switch || V is vacant || V is repeated {
|
||||
return string::new(nothing {});
|
||||
} else if const V is shown {
|
||||
return value.shown_argument();
|
||||
} else if const V is value {
|
||||
return string::new(nothing {});
|
||||
} 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 value == value_of(member) {
|
||||
return long_name(member.name, attached::<choice>(member)[0].name);
|
||||
}
|
||||
}
|
||||
}
|
||||
return string::new(nothing {});
|
||||
} else {
|
||||
return string::new(nothing {});
|
||||
}
|
||||
}
|
||||
|
||||
def i8::shown_argument(&this): string { return $"{this}"; }
|
||||
def i16::shown_argument(&this): string { return $"{this}"; }
|
||||
def i32::shown_argument(&this): string { return $"{this}"; }
|
||||
def i64::shown_argument(&this): string { return $"{this}"; }
|
||||
def u8::shown_argument(&this): string { return $"{this}"; }
|
||||
def u16::shown_argument(&this): string { return $"{this}"; }
|
||||
def u32::shown_argument(&this): string { return $"{this}"; }
|
||||
def u64::shown_argument(&this): string { return $"{this}"; }
|
||||
def f32::shown_argument(&this): string { return $"{this}"; }
|
||||
def f64::shown_argument(&this): string { return $"{this}"; }
|
||||
def strview::shown_argument(&this): string { return string::from(this, nothing {}); }
|
||||
def string::shown_argument(&this): string { return string::copy_of(this); }
|
||||
def path::shown_argument(&this): string { return string::from(this.span, nothing {}); }
|
||||
|
||||
def maybe<T>::list_choices(into: &mut page): void {
|
||||
choices_of::<T>(into);
|
||||
}
|
||||
|
||||
def vector<T>::list_choices(into: &mut page): void {
|
||||
choices_of::<T>(into);
|
||||
}
|
||||
Reference in new issue
Block a user