Improve help text and error handling

This commit is contained in:
Chris Wanstrath 2026-04-02 14:05:45 -07:00
parent 1c7c0da4b7
commit 9c516604f2
2 changed files with 47 additions and 4 deletions

1
shout-rs/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
target/

View File

@ -37,18 +37,55 @@ struct TestOpts {
parallel: bool,
}
fn print_usage() {
eprintln!("Usage: shout [options] [command]");
eprintln!();
eprintln!("$ shell output tester");
eprintln!();
eprintln!("Options:");
eprintln!(" -V, --version output the version number");
eprintln!(" -h, --help display help for command");
eprintln!();
eprintln!("Commands:");
eprintln!(" test [options] [files...] Run .shout test files");
eprintln!(" version Print the version");
eprintln!(" example Print an example .shout file");
eprintln!(" help [command] display help for command");
}
fn print_test_help() {
eprintln!("Usage: shout test [options] [files...]");
eprintln!();
eprintln!("Run .shout test files");
eprintln!();
eprintln!("Arguments:");
eprintln!(" files Files or directories to test");
eprintln!();
eprintln!("Options:");
eprintln!(" -u, --update Rewrite expected output in-place with actual output");
eprintln!(" -k, --keep Keep temp directories after run");
eprintln!(" --clean-env Start with empty environment");
eprintln!(" --path <path> Prepend <path> to PATH (repeatable)");
eprintln!(" --timeout <dur> Per-command timeout (default: 10s)");
eprintln!(" -v, --verbose Print each command as it runs");
eprintln!(" --port-from <n> Auto-assign $PORT starting from <n> (default: 5400)");
eprintln!(" -t, --filter <pattern> Only run files matching <pattern> (substring match)");
eprintln!(" --parallel Run files in parallel");
eprintln!(" -h, --help display help for command");
}
fn parse_args() -> Option<(&'static str, TestOpts)> {
let args: Vec<String> = std::env::args().skip(1).collect();
if args.is_empty() {
eprintln!("Usage: shout <test|example|version> [options]");
process::exit(1);
if args.is_empty() || args[0] == "-h" || args[0] == "--help" || args[0] == "help" {
print_usage();
process::exit(if args.is_empty() { 1 } else { 0 });
}
let subcommand = args[0].as_str();
match subcommand {
"version" => {
"-V" | "--version" | "version" => {
println!("{VERSION}");
process::exit(0);
}
@ -59,6 +96,7 @@ fn parse_args() -> Option<(&'static str, TestOpts)> {
"test" => {}
other => {
eprintln!("Unknown command: {other}");
eprintln!("Run 'shout --help' for usage");
process::exit(1);
}
}
@ -79,6 +117,10 @@ fn parse_args() -> Option<(&'static str, TestOpts)> {
let mut i = 1;
while i < args.len() {
match args[i].as_str() {
"-h" | "--help" => {
print_test_help();
process::exit(0);
}
"-u" | "--update" => opts.update = true,
"-k" | "--keep" => opts.keep = true,
"--clean-env" => opts.clean_env = true,