Initial Commit
This commit is contained in:
1625 files changed
+43424
No files matched your search
@@ -0,0 +1,585 @@
|
||||
# Tests of `std::args`: every form of the syntax, each error and where it is,
|
||||
# the values std's types read, repeats, positional arguments, subcommands, the
|
||||
# help text, and the mistakes in marks that stop the program. The commands are
|
||||
# private to the program, so the walk sees them only by the consent its marks
|
||||
# give.
|
||||
import std::args;
|
||||
import std::io;
|
||||
import std::testing;
|
||||
|
||||
|
||||
@command{ name = "flags", help = "Reads switches and numbers." }
|
||||
struct cli_flags {
|
||||
@option{ short = 'v', help = "Says more." }
|
||||
var verbose: bool;
|
||||
@option{ short = 'q' }
|
||||
var quiet: bool;
|
||||
@option{ short = 'n', value = "N", help = "How many." }
|
||||
var count: i64 = 3;
|
||||
@option{ name = "level", short = 'l' }
|
||||
var depth: i64;
|
||||
@option{ help = "Does nothing." }
|
||||
var dry_run: bool;
|
||||
@argument{ value = "WORD", help = "The words." }
|
||||
var words: vector<strview>;
|
||||
var untouched: i64 = 99;
|
||||
}
|
||||
|
||||
enum cli_colour: u8 {
|
||||
@choice red,
|
||||
@choice{ name = "GREEN", help = "As grass is." } green,
|
||||
@choice light_blue,
|
||||
unmarked,
|
||||
}
|
||||
|
||||
struct cli_heat : args::value {
|
||||
var degrees: f64;
|
||||
}
|
||||
|
||||
def cli_heat::from_argument(text: strview): result<cli_heat, strview> {
|
||||
if text.length < 2 || text[text.length - 1] != 'C' {
|
||||
return "is not a temperature, as `21C`";
|
||||
}
|
||||
return match real::parse::<f64>(text.substr(0, text.length - 1)) {
|
||||
value(v) => cli_heat { degrees = v },
|
||||
error(_) => "is not a temperature, as `21C`",
|
||||
};
|
||||
}
|
||||
|
||||
@command{ name = "values" }
|
||||
struct cli_values {
|
||||
@option var tiny: u8;
|
||||
@option var signed: i8;
|
||||
@option var wide: i64;
|
||||
@option var huge: u64;
|
||||
@option var ratio: f64;
|
||||
@option var small: f32;
|
||||
@option var named: string;
|
||||
@option var file: path;
|
||||
@option var colour: cli_colour = cli_colour::red;
|
||||
@option var heat: maybe<cli_heat>;
|
||||
@option var hues: vector<cli_colour>;
|
||||
}
|
||||
|
||||
@command{ name = "repeats" }
|
||||
struct cli_repeats {
|
||||
@option{ short = 'I', value = "DIR" }
|
||||
var include: vector<strview>;
|
||||
@option{ short = 'v', count = true }
|
||||
var verbosity: u32;
|
||||
@option{ short = 'n' }
|
||||
var number: i32;
|
||||
}
|
||||
|
||||
@command{ name = "places" }
|
||||
struct cli_places {
|
||||
@argument{ value = "FIRST" }
|
||||
var first: strview;
|
||||
@argument{ value = "MIDDLE" }
|
||||
var middle: vector<i64>;
|
||||
@argument{ value = "LAST" }
|
||||
var last: strview;
|
||||
@argument{ value = "MAYBE" }
|
||||
var maybe_one: maybe<strview>;
|
||||
}
|
||||
|
||||
@command{ name = "passes" }
|
||||
struct cli_passes {
|
||||
@option{ short = 'x' }
|
||||
var exact: bool;
|
||||
@argument{ value = "FILE" }
|
||||
var files: vector<strview>;
|
||||
@argument{ value = "ARGUMENT", rest = true, help = "What is passed on." }
|
||||
var passed: vector<strview>;
|
||||
}
|
||||
|
||||
@command{ name = "needs" }
|
||||
struct cli_needs {
|
||||
@option{ short = 'o', value = "OUTPUT", required = true, help = "Where it goes." }
|
||||
var output: strview;
|
||||
@argument{ value = "INPUT" }
|
||||
var input: strview;
|
||||
}
|
||||
|
||||
@command{ name = "host" }
|
||||
struct cli_host {
|
||||
@option{ short = 'h', value = "NAME", help = "The host." }
|
||||
var host: strview;
|
||||
}
|
||||
|
||||
@command{ name = "git", help = "Keeps versions.", version = "git 2.0" }
|
||||
union cli_git {
|
||||
@command{ help = "Names other copies." }
|
||||
var remote: cli_remote;
|
||||
@command
|
||||
var status: cli_status;
|
||||
}
|
||||
|
||||
@command{ help = "Lists, adds, and removes other copies." }
|
||||
struct cli_remote {
|
||||
@option{ short = 'v', help = "Shows each one's address." }
|
||||
var verbose: bool;
|
||||
@argument
|
||||
var action: cli_remote_action;
|
||||
}
|
||||
|
||||
@command
|
||||
union cli_remote_action {
|
||||
@command{ help = "Adds one." }
|
||||
var add: cli_remote_add;
|
||||
@command{ name = "rm", help = "Removes one." }
|
||||
var remove: cli_remote_remove;
|
||||
}
|
||||
|
||||
@command
|
||||
struct cli_remote_add {
|
||||
@option{ help = "Fetches it at once." }
|
||||
var fetch: bool;
|
||||
@argument{ value = "NAME" }
|
||||
var name: strview;
|
||||
@argument{ value = "URL" }
|
||||
var url: strview;
|
||||
}
|
||||
|
||||
@command
|
||||
struct cli_remote_remove {
|
||||
@argument{ value = "NAME" }
|
||||
var name: strview;
|
||||
}
|
||||
|
||||
@command{ help = "Shows what changed." }
|
||||
struct cli_status {
|
||||
@option{ short = 's' }
|
||||
var short_form: bool;
|
||||
}
|
||||
|
||||
@command{ name = "make" }
|
||||
struct cli_make {
|
||||
@option{ short = 'C', value = "DIR" }
|
||||
var directory: strview;
|
||||
@argument{ value = "FILE" }
|
||||
var file: strview;
|
||||
@argument
|
||||
var step: cli_make_step;
|
||||
}
|
||||
|
||||
@command
|
||||
union cli_make_step {
|
||||
@command var build: cli_make_build;
|
||||
@command var clean: cli_status;
|
||||
}
|
||||
|
||||
@command
|
||||
struct cli_make_build {
|
||||
@option{ short = 'j' }
|
||||
var jobs: u8 = 1;
|
||||
}
|
||||
|
||||
# Options several commands share, as a group, which holds a group of its own.
|
||||
@group
|
||||
struct cli_shared {
|
||||
@option{ short = 'O', value = "LEVEL", help = "How much to optimize." }
|
||||
var level: u8 = 2;
|
||||
@option{ short = 'v', help = "Says more." }
|
||||
var verbose: bool;
|
||||
@group
|
||||
var library: cli_library;
|
||||
}
|
||||
|
||||
@group
|
||||
struct cli_library {
|
||||
@option{ name = "std", value = "DIR", required = true, help = "Where the library is." }
|
||||
var directory: strview;
|
||||
@option{ short = 'I', value = "DIR" }
|
||||
var include: vector<strview>;
|
||||
}
|
||||
|
||||
@command{ name = "grouped", help = "Builds with the options it shares." }
|
||||
struct cli_grouped {
|
||||
@option{ short = 'o', value = "OUTPUT", help = "Where it goes." }
|
||||
var output: strview;
|
||||
@group
|
||||
var shared: cli_shared;
|
||||
@option{ short = 'j', value = "N", help = "How many at once." }
|
||||
var jobs: u32 = 1;
|
||||
@argument{ value = "FILE", help = "The files." }
|
||||
var files: vector<strview>;
|
||||
}
|
||||
|
||||
# The commands whose marks are mistaken, which stop the program once read.
|
||||
@command struct cli_two_longs { @option var level: i64; @option{ name = "level" } var depth: i64; }
|
||||
@command struct cli_two_shorts { @option{ short = 'x' } var one: bool; @option{ short = 'x' } var two: bool; }
|
||||
@command struct cli_bad_short { @option{ short = '!' } var bang: bool; }
|
||||
@command struct cli_bad_count { @option{ count = true } var noise: strview; }
|
||||
@command struct cli_bad_rest { @argument{ rest = true } var after: strview; }
|
||||
@command struct cli_two_rests { @argument{ rest = true } var one: vector<strview>; @argument{ rest = true } var two: vector<strview>; }
|
||||
@command struct cli_late_command { @argument var step: cli_make_step; @argument var after: strview; }
|
||||
@command struct cli_many_before { @argument var files: vector<strview>; @argument var step: cli_make_step; }
|
||||
@command union cli_two_commands { @command var one: cli_status; @command{ name = "one" } var two: cli_status; }
|
||||
@command struct cli_group_short { @option{ short = 'v' } var loud: bool; @group var shared: cli_shared; }
|
||||
@command struct cli_group_long { @group var shared: cli_shared; @option{ name = "std" } var other: strview; }
|
||||
|
||||
# The error reading the arguments into a command gives, or one of no kind
|
||||
# where they are read.
|
||||
def cli_error_of<T>(given: span<strview>): args::error where T: zeroable {
|
||||
var read := args::parse::<T>(given) else (wrong) { return wrong; };
|
||||
return args::error { kind = args::error_kind::help_asked, at = 9999 };
|
||||
}
|
||||
|
||||
# The arguments read into flags, or flags of a count of -1 where they are not.
|
||||
def cli_flags_of(given: span<strview>): cli_flags {
|
||||
var read := args::parse::<cli_flags>(given) else (_) { return cli_flags { count = -1 }; };
|
||||
return move(read);
|
||||
}
|
||||
|
||||
@test def options_are_read_by_long_and_short_names(t: &mut tester): void {
|
||||
t.expects(cli_flags_of(["--count", "4"]).count == 4);
|
||||
t.expects(cli_flags_of(["--count=5"]).count == 5);
|
||||
t.expects(cli_flags_of(["-n", "6"]).count == 6);
|
||||
t.expects(cli_flags_of(["-n7"]).count == 7);
|
||||
t.expects(cli_flags_of(["--level", "2"]).depth == 2);
|
||||
t.expects(cli_flags_of(["-l", "8"]).depth == 8);
|
||||
|
||||
val plain := cli_flags_of([]);
|
||||
t.expects(plain.count == 3 && plain.depth == 0 && !plain.verbose && plain.words.length == 0 && plain.untouched == 99);
|
||||
|
||||
# Switches written together, the last of them taking a value.
|
||||
val together := cli_flags_of(["-vq"]);
|
||||
t.expects(together.verbose && together.quiet);
|
||||
val valued := cli_flags_of(["-vn9"]);
|
||||
t.expects(valued.verbose && !valued.quiet && valued.count == 9);
|
||||
val spaced := cli_flags_of(["-qvn", "10"]);
|
||||
t.expects(spaced.verbose && spaced.quiet && spaced.count == 10);
|
||||
|
||||
# A field's `_` is written `-`, and the last of an option written is kept.
|
||||
t.expects(cli_flags_of(["--dry-run"]).dry_run);
|
||||
t.expects(cli_error_of::<cli_flags>(["--dry_run"]).kind == args::error_kind::unknown_option);
|
||||
t.expects(cli_flags_of(["-n", "1", "--count", "2", "-n3"]).count == 3);
|
||||
}
|
||||
|
||||
@test def values_may_begin_with_a_dash_and_positional_arguments_may_be_dashes(t: &mut tester): void {
|
||||
t.expects(cli_flags_of(["--level", "-5"]).depth == -5);
|
||||
t.expects(cli_flags_of(["-n", "-12"]).count == -12);
|
||||
t.expects(cli_flags_of(["--level=-6"]).depth == -6);
|
||||
|
||||
# Options come between positional arguments, which a lone `-` is, and `-` then
|
||||
# a digit, where no short name is a digit.
|
||||
val mixed := cli_flags_of(["one", "-v", "-", "-5", "two"]);
|
||||
t.expects(mixed.verbose && mixed.words.length == 4);
|
||||
t.expects(mixed.words[0] == "one" && mixed.words[1] == "-" && mixed.words[2] == "-5" && mixed.words[3] == "two");
|
||||
|
||||
# After `--`, everything is positional.
|
||||
val ended := cli_flags_of(["a", "--", "-v", "--count", "b"]);
|
||||
t.expects(!ended.verbose && ended.count == 3 && ended.words.length == 4 && ended.words[1] == "-v");
|
||||
|
||||
# `--name=` gives an empty value, which no number is.
|
||||
val empty := cli_error_of::<cli_flags>(["--count="]);
|
||||
t.expects(empty.kind == args::error_kind::invalid_value && empty.text.length == 0);
|
||||
}
|
||||
|
||||
@test def each_error_says_where_it_is(t: &mut tester): void {
|
||||
val unknown := cli_error_of::<cli_flags>(["-v", "--colour", "red"]);
|
||||
t.expects(unknown.kind == args::error_kind::unknown_option && unknown.at == 1 && unknown.text == "--colour");
|
||||
val unknown_short := cli_error_of::<cli_flags>(["-vz"]);
|
||||
t.expects(unknown_short.kind == args::error_kind::unknown_option && unknown_short.at == 0 && unknown_short.name == "-z");
|
||||
|
||||
val missing := cli_error_of::<cli_flags>(["-v", "--count"]);
|
||||
t.expects(missing.kind == args::error_kind::missing_value && missing.at == 2 && missing.name == "--count");
|
||||
val missing_short := cli_error_of::<cli_flags>(["-vn"]);
|
||||
t.expects(missing_short.kind == args::error_kind::missing_value && missing_short.at == 1 && missing_short.name == "-n");
|
||||
|
||||
val invalid := cli_error_of::<cli_flags>(["--count", "ten"]);
|
||||
t.expects(invalid.kind == args::error_kind::invalid_value && invalid.at == 1 && invalid.text == "ten");
|
||||
t.expects(invalid.name == "--count" && invalid.reason == "is not a whole number");
|
||||
val attached := cli_error_of::<cli_flags>(["-nx1"]);
|
||||
t.expects(attached.kind == args::error_kind::invalid_value && attached.at == 0 && attached.text == "x1" && attached.name == "-n");
|
||||
|
||||
val switched := cli_error_of::<cli_flags>(["--verbose=yes"]);
|
||||
t.expects(switched.kind == args::error_kind::switch_given_value && switched.text == "yes" && switched.name == "--verbose");
|
||||
|
||||
val required := cli_error_of::<cli_needs>(["in"]);
|
||||
t.expects(required.kind == args::error_kind::missing_option && required.at == 1 && required.name == "--output");
|
||||
val argument := cli_error_of::<cli_needs>(["-o", "out"]);
|
||||
t.expects(argument.kind == args::error_kind::missing_argument && argument.at == 2 && argument.name == "INPUT");
|
||||
val extra := cli_error_of::<cli_needs>(["-o", "out", "in", "more"]);
|
||||
t.expects(extra.kind == args::error_kind::unexpected_argument && extra.at == 3 && extra.text == "more");
|
||||
|
||||
val command := cli_error_of::<cli_git>(["stats"]);
|
||||
t.expects(command.kind == args::error_kind::unknown_command && command.at == 0 && command.text == "stats");
|
||||
val no_command := cli_error_of::<cli_git>([]);
|
||||
t.expects(no_command.kind == args::error_kind::missing_command && no_command.at == 0);
|
||||
val deeper := cli_error_of::<cli_git>(["remote", "-v"]);
|
||||
t.expects(deeper.kind == args::error_kind::missing_command && deeper.at == 2);
|
||||
|
||||
val helped := cli_error_of::<cli_flags>(["-v", "--help"]);
|
||||
t.expects(helped.kind == args::error_kind::help_asked && helped.at == 1);
|
||||
t.expects(cli_error_of::<cli_flags>(["-vh"]).kind == args::error_kind::help_asked);
|
||||
t.expects(cli_error_of::<cli_git>(["--version"]).kind == args::error_kind::version_asked);
|
||||
t.expects(cli_error_of::<cli_flags>(["--version"]).kind == args::error_kind::unknown_option);
|
||||
}
|
||||
|
||||
@test def errors_say_what_went_wrong(t: &mut tester): void {
|
||||
t.expects(cli_error_of::<cli_values>(["--tiny", "300"]).describe().view() == "`--tiny 300`: 300 is not between 0 and 255.");
|
||||
t.expects(cli_error_of::<cli_flags>(["--colour"]).describe().view() == "`--colour` is not an option.");
|
||||
t.expects(cli_error_of::<cli_flags>(["--count"]).describe().view() == "`--count` needs a value.");
|
||||
t.expects(cli_error_of::<cli_flags>(["-q=1"]).describe().view() == "`-q=1` is not an option.");
|
||||
t.expects(cli_error_of::<cli_flags>(["--quiet=1"]).describe().view() == "`--quiet` takes no value, but is given `1`.");
|
||||
t.expects(cli_error_of::<cli_needs>(["in"]).describe().view() == "`--output` is required.");
|
||||
t.expects(cli_error_of::<cli_needs>(["-o", "out"]).describe().view() == "INPUT is missing.");
|
||||
t.expects(cli_error_of::<cli_places>(["a", "x", "b"]).describe().view() == "MIDDLE `x` is not a whole number.");
|
||||
t.expects(cli_error_of::<cli_needs>(["-o", "o", "a", "b"]).describe().view() == "`b` is more than the arguments taken.");
|
||||
t.expects(cli_error_of::<cli_git>(["stats"]).describe().view() == "`stats` is not a command.");
|
||||
t.expects(cli_error_of::<cli_git>([]).describe().view() == "A command is needed.");
|
||||
}
|
||||
|
||||
@test def numbers_are_read_within_their_ranges(t: &mut tester): void {
|
||||
val read := args::parse::<cli_values>([
|
||||
"--tiny", "255", "--signed=-128", "--wide", "-9223372036854775808", "--huge", "18446744073709551615",
|
||||
"--ratio", "2.5", "--small", "-0.25"
|
||||
]) else (_) { t.expects(false); return; };
|
||||
t.expects(read.tiny == 255 && read.signed == -128 && read.wide == -9223372036854775807 - 1);
|
||||
t.expects(read.huge == 18446744073709551615 && read.ratio == 2.5 && read.small == -0.25);
|
||||
|
||||
# A prefix and `_` between digits are read as the language reads them.
|
||||
val prefixed := args::parse::<cli_values>(["--wide", "0x1_0", "--tiny", "0b11", "--huge", "1_000"]) else (_) { t.expects(false); return; };
|
||||
t.expects(prefixed.wide == 16 && prefixed.tiny == 3 && prefixed.huge == 1000);
|
||||
|
||||
t.expects(cli_error_of::<cli_values>(["--tiny", "256"]).reason == "is not between 0 and 255");
|
||||
t.expects(cli_error_of::<cli_values>(["--tiny", "-1"]).reason == "is not between 0 and 255");
|
||||
t.expects(cli_error_of::<cli_values>(["--signed", "-129"]).reason == "is not between -128 and 127");
|
||||
t.expects(cli_error_of::<cli_values>(["--signed", "128"]).reason == "is not between -128 and 127");
|
||||
t.expects(cli_error_of::<cli_values>(["--wide", "9223372036854775808"]).kind == args::error_kind::invalid_value);
|
||||
t.expects(cli_error_of::<cli_values>(["--huge", "18446744073709551616"]).reason == "is not a whole number");
|
||||
t.expects(cli_error_of::<cli_values>(["--tiny", "0x"]).reason == "is not a whole number");
|
||||
t.expects(cli_error_of::<cli_values>(["--ratio", "much"]).reason == "is not a number");
|
||||
t.expects(cli_error_of::<cli_values>(["--small", "1e39"]).reason == "is too large");
|
||||
}
|
||||
|
||||
@test def text_paths_enums_and_a_programs_own_values_are_read(t: &mut tester): void {
|
||||
val read := args::parse::<cli_values>([
|
||||
"--named", "anything", "--file", "a/b", "--colour", "light-blue", "--heat", "21.5C"
|
||||
]) else (_) { t.expects(false); return; };
|
||||
t.expects(read.named.view() == "anything");
|
||||
t.expects(read.file.view() == path::parse("a/b").value.view());
|
||||
t.expects(read.colour == cli_colour::light_blue);
|
||||
t.expects(read.heat is some(&heat) && heat.degrees == 21.5);
|
||||
t.expects(args::parse::<cli_values>([]).value.heat is none);
|
||||
|
||||
# A member is read by the `name` its mark gives, and an unmarked one not at all.
|
||||
t.expects(args::parse::<cli_values>(["--colour", "GREEN"]).value.colour == cli_colour::green);
|
||||
t.expects(cli_error_of::<cli_values>(["--colour", "green"]).reason == "is not one of the choices");
|
||||
t.expects(cli_error_of::<cli_values>(["--colour", "unmarked"]).kind == args::error_kind::invalid_value);
|
||||
t.expects(cli_error_of::<cli_values>(["--heat", "hot"]).reason == "is not a temperature, as `21C`");
|
||||
t.expects(cli_error_of::<cli_values>(["--file", ""]).reason == "is not a path");
|
||||
|
||||
val listed := args::parse::<cli_values>(["--hues", "red", "--hues", "GREEN"]) else (_) { t.expects(false); return; };
|
||||
t.expects(listed.hues.length == 2 && listed.hues[0] == cli_colour::red && listed.hues[1] == cli_colour::green);
|
||||
}
|
||||
|
||||
@test def options_repeat_into_vectors_and_counts(t: &mut tester): void {
|
||||
val read := args::parse::<cli_repeats>(["-I", "a", "-vvv", "--include=b", "-Ic", "-v"]) else (_) { t.expects(false); return; };
|
||||
t.expects(read.include.length == 3 && read.include[0] == "a" && read.include[1] == "b" && read.include[2] == "c");
|
||||
t.expects(read.verbosity == 4);
|
||||
t.expects(args::parse::<cli_repeats>(["--verbosity", "--verbosity"]).value.verbosity == 2);
|
||||
t.expects(cli_error_of::<cli_repeats>(["--verbosity=2"]).kind == args::error_kind::switch_given_value);
|
||||
|
||||
# A count is no switch to the short names written with it: `-vn5` counts once.
|
||||
val counted := args::parse::<cli_repeats>(["-vn5"]) else (_) { t.expects(false); return; };
|
||||
t.expects(counted.verbosity == 1 && counted.number == 5);
|
||||
}
|
||||
|
||||
@test def positional_arguments_go_to_their_fields_in_turn(t: &mut tester): void {
|
||||
# A vector takes what is left over once the single fields after it have theirs.
|
||||
val read := args::parse::<cli_places>(["a", "1", "2", "3", "z"]) else (_) { t.expects(false); return; };
|
||||
t.expects(read.first == "a" && read.middle.length == 3 && read.middle[2] == 3 && read.last == "z" && read.maybe_one is none);
|
||||
|
||||
val fewest := args::parse::<cli_places>(["a", "z"]) else (_) { t.expects(false); return; };
|
||||
t.expects(fewest.first == "a" && fewest.middle.length == 0 && fewest.last == "z");
|
||||
t.expects(cli_error_of::<cli_places>(["a"]).name == "LAST");
|
||||
|
||||
# What follows `--` goes to the field taking the rest, and only there.
|
||||
val passed := args::parse::<cli_passes>(["x.sakura", "-x", "y.sakura", "--", "-x", "--", "z"]) else (_) { t.expects(false); return; };
|
||||
t.expects(passed.exact && passed.files.length == 2 && passed.files[1] == "y.sakura");
|
||||
t.expects(passed.passed.length == 3 && passed.passed[0] == "-x" && passed.passed[1] == "--" && passed.passed[2] == "z");
|
||||
val none_passed := args::parse::<cli_passes>(["a"]) else (_) { t.expects(false); return; };
|
||||
t.expects(none_passed.files.length == 1 && none_passed.passed.length == 0);
|
||||
}
|
||||
|
||||
@test def subcommands_read_their_own_options_two_deep(t: &mut tester): void {
|
||||
val read := args::parse::<cli_git>(["remote", "-v", "add", "--fetch", "origin", "https://x"]) else (_) { t.expects(false); return; };
|
||||
match read {
|
||||
remote(&r) => {
|
||||
t.expects(r.verbose);
|
||||
match r.action {
|
||||
add(&a) => t.expects(a.fetch && a.name == "origin" && a.url == "https://x");
|
||||
remove(&_) => t.expects(false);
|
||||
}
|
||||
}
|
||||
status(&_) => t.expects(false);
|
||||
}
|
||||
|
||||
val removed := args::parse::<cli_git>(["remote", "rm", "old"]) else (_) { t.expects(false); return; };
|
||||
t.expects(removed is remote(&r) && r.action is remove(&gone) && gone.name == "old" && !r.verbose);
|
||||
t.expects(args::parse::<cli_git>(["status", "-s"]).value is status(&s) && s.short_form);
|
||||
|
||||
# A command's own options come before its subcommand's word, and the
|
||||
# positional arguments before it each take one.
|
||||
val made := args::parse::<cli_make>(["-C", "src", "rules.mk", "build", "-j", "8"]) else (_) { t.expects(false); return; };
|
||||
t.expects(made.directory == "src" && made.file == "rules.mk" && made.step is build(&b) && b.jobs == 8);
|
||||
t.expects(cli_error_of::<cli_make>(["rules.mk", "build", "-C", "x"]).kind == args::error_kind::unknown_option);
|
||||
t.expects(cli_error_of::<cli_make>(["rules.mk"]).kind == args::error_kind::missing_command);
|
||||
}
|
||||
|
||||
@test def the_help_text_is_written_from_the_marks(t: &mut tester): void {
|
||||
t.expects(args::help::<cli_flags>().view() == """
|
||||
usage: flags [options] WORD...
|
||||
|
||||
Reads switches and numbers.
|
||||
|
||||
arguments:
|
||||
WORD... The words.
|
||||
|
||||
options:
|
||||
-v, --verbose Says more.
|
||||
-q, --quiet
|
||||
-n, --count N How many. Default: 3.
|
||||
-l, --level LEVEL Default: 0.
|
||||
--dry-run Does nothing.
|
||||
-h, --help Shows this help.
|
||||
""");
|
||||
|
||||
# The arguments reach a subcommand, whose help is given, and its usage.
|
||||
t.expects(args::help::<cli_git>(["remote", "add", "--help"]).view() == """
|
||||
usage: git remote add [options] NAME URL
|
||||
|
||||
Adds one.
|
||||
|
||||
arguments:
|
||||
NAME
|
||||
URL
|
||||
|
||||
options:
|
||||
--fetch Fetches it at once.
|
||||
-h, --help Shows this help.
|
||||
""");
|
||||
t.expects(args::usage::<cli_git>(["remote"]).view() == "usage: git remote [options] COMMAND ...");
|
||||
t.expects(args::help::<cli_git>(["--help"]).view() == """
|
||||
usage: git [options] COMMAND ...
|
||||
|
||||
Keeps versions.
|
||||
|
||||
commands:
|
||||
remote Names other copies.
|
||||
status Shows what changed.
|
||||
|
||||
options:
|
||||
-h, --help Shows this help.
|
||||
--version Shows the version.
|
||||
""");
|
||||
|
||||
t.expects(args::help::<cli_values>([]).view() == """
|
||||
usage: values [options]
|
||||
|
||||
options:
|
||||
--tiny TINY Default: 0.
|
||||
--signed SIGNED Default: 0.
|
||||
--wide WIDE Default: 0.
|
||||
--huge HUGE Default: 0.
|
||||
--ratio RATIO Default: 0.
|
||||
--small SMALL Default: 0.
|
||||
--named NAMED
|
||||
--file FILE
|
||||
--colour COLOUR Default: red.
|
||||
red
|
||||
GREEN As grass is.
|
||||
light-blue
|
||||
--heat HEAT
|
||||
--hues HUES
|
||||
red
|
||||
GREEN As grass is.
|
||||
light-blue
|
||||
-h, --help Shows this help.
|
||||
""");
|
||||
t.expects(args::usage::<cli_passes>().view() == "usage: passes [options] FILE... [-- ARGUMENT...]");
|
||||
t.expects(args::usage::<cli_places>().view() == "usage: places [options] FIRST MIDDLE... LAST [MAYBE]");
|
||||
}
|
||||
|
||||
@test def help_is_left_to_a_field_taking_its_name(t: &mut tester): void {
|
||||
val read := args::parse::<cli_host>(["-h", "example.org"]) else (_) { t.expects(false); return; };
|
||||
t.expects(read.host == "example.org");
|
||||
t.expects(cli_error_of::<cli_host>(["--help"]).kind == args::error_kind::help_asked);
|
||||
t.expects(args::help::<cli_host>().view() == """
|
||||
usage: host [options]
|
||||
|
||||
options:
|
||||
-h, --host NAME The host.
|
||||
--help Shows this help.
|
||||
""");
|
||||
}
|
||||
|
||||
@test def parse_or_exit_stops_with_the_status_of_what_it_says(t: &mut tester): void {
|
||||
t.expects(!stops(def() { _ = args::parse_or_exit::<cli_flags>(["--help"]); }));
|
||||
t.expects(!stops(def() { _ = args::parse_or_exit::<cli_git>(["--version"]); }));
|
||||
t.expects(stops(def() { _ = args::parse_or_exit::<cli_flags>(["--count", "x"]); }));
|
||||
t.expects(!stops(def() { _ = args::parse_or_exit::<cli_flags>(["-v"]); }));
|
||||
}
|
||||
|
||||
@test def mistaken_marks_stop_the_program_however_it_is_run(t: &mut tester): void {
|
||||
t.expects(stops(def() { _ = args::parse::<cli_two_longs>([]); }));
|
||||
t.expects(stops(def() { _ = args::parse::<cli_two_shorts>(["--help"]); }));
|
||||
t.expects(stops(def() { _ = args::parse::<cli_bad_short>([]); }));
|
||||
t.expects(stops(def() { _ = args::parse::<cli_bad_count>([]); }));
|
||||
t.expects(stops(def() { _ = args::parse::<cli_bad_rest>([]); }));
|
||||
t.expects(stops(def() { _ = args::parse::<cli_two_rests>([]); }));
|
||||
t.expects(stops(def() { _ = args::parse::<cli_late_command>([]); }));
|
||||
t.expects(stops(def() { _ = args::parse::<cli_many_before>([]); }));
|
||||
t.expects(stops(def() { _ = args::parse::<cli_two_commands>([]); }));
|
||||
t.expects(stops(def() { _ = args::parse::<cli_group_short>([]); }));
|
||||
t.expects(stops(def() { _ = args::parse::<cli_group_long>([]); }));
|
||||
t.expects(!stops(def() { _ = args::parse::<cli_flags>([]); }));
|
||||
}
|
||||
|
||||
@test def groups_are_read_as_if_declared_in_place(t: &mut tester): void {
|
||||
val read := args::parse::<cli_grouped>([
|
||||
"-o", "out", "-O1", "--verbose", "--std", "lib", "-Ia", "--include", "b", "-j", "4", "x", "y"
|
||||
]) else (_) { t.expects(false); return; };
|
||||
t.expects(read.output == "out" && read.shared.level == 1 && read.shared.verbose && read.jobs == 4);
|
||||
t.expects(read.shared.library.directory == "lib" && read.shared.library.include.length == 2);
|
||||
t.expects(read.shared.library.include[1] == "b" && read.files.length == 2 && read.files[1] == "y");
|
||||
|
||||
# A group's fields keep their defaults, and its short names are written
|
||||
# together with the command's own.
|
||||
val plain := args::parse::<cli_grouped>(["--std=lib"]) else (_) { t.expects(false); return; };
|
||||
t.expects(plain.shared.level == 2 && !plain.shared.verbose && plain.jobs == 1 && plain.output.length == 0);
|
||||
val together := args::parse::<cli_grouped>(["--std", "lib", "-vj2", "-vO3"]) else (_) { t.expects(false); return; };
|
||||
t.expects(together.shared.verbose && together.jobs == 2 && together.shared.level == 3);
|
||||
|
||||
# Errors name a group's options as they name the command's own.
|
||||
val required := cli_error_of::<cli_grouped>(["x"]);
|
||||
t.expects(required.kind == args::error_kind::missing_option && required.name == "--std");
|
||||
val invalid := cli_error_of::<cli_grouped>(["--std", "lib", "-O", "300"]);
|
||||
t.expects(invalid.kind == args::error_kind::invalid_value && invalid.name == "-O" && invalid.text == "300");
|
||||
val switched := cli_error_of::<cli_grouped>(["--std", "lib", "--verbose=yes"]);
|
||||
t.expects(switched.kind == args::error_kind::switch_given_value && switched.name == "--verbose");
|
||||
t.expects(cli_error_of::<cli_grouped>(["--std", "lib", "--shared"]).kind == args::error_kind::unknown_option);
|
||||
}
|
||||
|
||||
@test def groups_are_shown_in_place_in_the_help_text(t: &mut tester): void {
|
||||
t.expects(args::help::<cli_grouped>().view() == """
|
||||
usage: grouped [options] FILE...
|
||||
|
||||
Builds with the options it shares.
|
||||
|
||||
arguments:
|
||||
FILE... The files.
|
||||
|
||||
options:
|
||||
-o, --output OUTPUT Where it goes.
|
||||
-O, --level LEVEL How much to optimize. Default: 2.
|
||||
-v, --verbose Says more.
|
||||
--std DIR Where the library is. Required.
|
||||
-I, --include DIR
|
||||
-j, --jobs N How many at once. Default: 1.
|
||||
-h, --help Shows this help.
|
||||
""");
|
||||
}
|
||||
Reference in new issue
Block a user