#! @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; var lefts: vector; var rights: vector; } # Makes a help text with no rows. def page::new(): page { return page { places = vector::::new(), lefts = vector::::new(), rights = vector::::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(): strview { if const T is switch || T is value || T is repeated { return ""; } else { val given := attached::(decl_of::())[0].name; return given.length != 0 ? given : decl_of::().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(words: span, 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::().kind == target::unions { if depth < words.length { for const field in members_of(decl_of::()) { if const field.kind == target::fields && attached::(field).length > 0 { if named_as(words[depth], field.name, attached::(field)[0].name) { val deeper := $"{path} {words[depth]}"; return help_at::(words, depth + 1, deeper.span, attached::(field)[0].help, whole); } } } } return union_help::(path, about, whole); } else { if depth < words.length { for const field in members_of(decl_of::()) { if const field.kind == target::fields && attached::(field).length > 0 { if is_command::() { return help_within::(words, depth, path, about, whole); } } } } return struct_help::(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(words: span, 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::().kind == target::unions && attached::(decl_of::()).length > 0 { if const V is zeroable { return help_at::(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(path: strview, about: strview, whole: bool): string { var made := $"usage: {path} [options] COMMAND ..."; return made if !whole; made.append("\n"); val own := attached::(decl_of::())[0].help; val said := own.length != 0 ? own : about; if said.length != 0 { made.append($"\n{said}\n"); } var rows := page::new(); commands_of::(&mut rows); helped(&mut rows, true, true, attached::(decl_of::())[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(path: strview, about: strview, whole: bool): string where T: zeroable { var made := $"usage: {path} [options]"; for const field in members_of(decl_of::()) { if const field.kind == target::fields && attached::(field).length > 0 { if is_command::() { made.append(" COMMAND ..."); } else { val marked := attached::(field)[0]; val name := value_name(field.name, marked.value); made.append(" "); made.append(argument_shown(name.span, shape_of::(), marked.rest).span); } } } return made if !whole; made.append("\n"); val own := attached::(decl_of::())[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::()) { if const field.kind == target::fields && attached::(field).length > 0 { if is_command::() { commands_within::(&mut rows); } else { val marked := attached::(field)[0]; val name := value_name(field.name, marked.value); var left := string::from(" ", nothing {}); var shown := argument_shown(name.span, shape_of::(), 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::(&mut rows, &defaults); helped(&mut rows, helps::(true), helps::(false), attached::(decl_of::())[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(rows: &mut page, defaults: &T): void { for const field in members_of(decl_of::()) { if const field.kind == target::fields && attached::