Skip to content

Commit 1274d1f

Browse files
authored
Add -h/--help flag to testbin (#16196)
# Description As title, this pr introduce `-h` flag to testbin, so if I want to see which testbin I should use, I don't need to look into source code. ### About the change I don't know if there is any way to get docstring of a function inside rust code. So I created a trait, and put docstring into it's `help` method: ```rust pub trait TestBin { // the docstring of original functions are moved here. fn help(&self) -> &'static str; fn run(&self); } ``` Take `cococo` testbin as example, the changes are: ``` original cococo function --> Cococo struct, then 1. put the body of `cococo` function into `run` method 2. put the docstring of `cococo` function into `help` method ``` # User-Facing Changes `-h/--help` flag in testbin is enabled. ``` > nu --testbin -h Usage: nu --testbin <bin> <bin>: chop -> With no parameters, will chop a character off the end of each line cococo -> Cross platform echo using println!()(e.g: nu --testbin cococo a b c) echo_env -> Echo's value of env keys from args(e.g: nu --testbin echo_env FOO BAR) echo_env_mixed -> Mix echo of env keys from input(e.g: nu --testbin echo_env_mixed out-err FOO BAR; nu --testbin echo_env_mixed err-out FOO BAR) echo_env_stderr -> Echo's value of env keys from args to stderr(e.g: nu --testbin echo_env_stderr FOO BAR) echo_env_stderr_fail -> Echo's value of env keys from args to stderr, and exit with failure(e.g: nu --testbin echo_env_stderr_fail FOO BAR) fail -> Exits with failure code 1(e.g: nu --testbin fail) iecho -> Another type of echo that outputs a parameter per line, looping infinitely(e.g: nu --testbin iecho 3) input_bytes_length -> Prints the number of bytes received on stdin(e.g: 0x[deadbeef] | nu --testbin input_bytes_length) meow -> Cross platform cat (open a file, print the contents) using read_to_string and println!()(e.g: nu --testbin meow file.txt) meowb -> Cross platform cat (open a file, print the contents) using read() and write_all() / binary(e.g: nu --testbin meowb sample.db) nonu -> Cross platform echo but concats arguments without space and NO newline(e.g: nu --testbin nonu a b c) nu_repl -> Run a REPL with the given source lines relay -> Relays anything received on stdin to stdout(e.g: 0x[beef] | nu --testbin relay) repeat_bytes -> A version of repeater that can output binary data, even null bytes(e.g: nu --testbin repeat_bytes 003d9fbf 10) repeater -> Repeat a string or char N times(e.g: nu --testbin repeater a 5) ``` # Tests + Formatting None, all existed tests can guarantee the behavior of testbins doesn't change. # After Submitting NaN
1 parent da9615f commit 1274d1f

File tree

2 files changed

+305
-168
lines changed

2 files changed

+305
-168
lines changed

src/main.rs

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -367,28 +367,18 @@ fn main() -> Result<()> {
367367

368368
start_time = std::time::Instant::now();
369369
if let Some(testbin) = &parsed_nu_cli_args.testbin {
370-
// Call out to the correct testbin
371-
match testbin.item.as_str() {
372-
"echo_env" => test_bins::echo_env(true),
373-
"echo_env_stderr" => test_bins::echo_env(false),
374-
"echo_env_stderr_fail" => test_bins::echo_env_and_fail(false),
375-
"echo_env_mixed" => test_bins::echo_env_mixed(),
376-
"cococo" => test_bins::cococo(),
377-
"meow" => test_bins::meow(),
378-
"meowb" => test_bins::meowb(),
379-
"relay" => test_bins::relay(),
380-
"iecho" => test_bins::iecho(),
381-
"fail" => test_bins::fail(),
382-
"nonu" => test_bins::nonu(),
383-
"chop" => test_bins::chop(),
384-
"repeater" => test_bins::repeater(),
385-
"repeat_bytes" => test_bins::repeat_bytes(),
386-
// Important: nu_repl must be called with `--testbin=nu_repl`
387-
// `--testbin nu_repl` will not work due to argument count logic
388-
// in test_bins.rs
389-
"nu_repl" => test_bins::nu_repl(),
390-
"input_bytes_length" => test_bins::input_bytes_length(),
391-
_ => std::process::exit(1),
370+
let dispatcher = test_bins::new_testbin_dispatcher();
371+
let test_bin = testbin.item.as_str();
372+
match dispatcher.get(test_bin) {
373+
Some(test_bin) => test_bin.run(),
374+
None => {
375+
if ["-h", "--help"].contains(&test_bin) {
376+
test_bins::show_help(&dispatcher);
377+
} else {
378+
eprintln!("ERROR: Unknown testbin '{test_bin}'");
379+
std::process::exit(1);
380+
}
381+
}
392382
}
393383
std::process::exit(0)
394384
} else {

0 commit comments

Comments
 (0)