8000 Treat jetdb without a subcommand as clap treats a missing subcommand, under the name jetdb by dominion525 · Pull Request #168 · dominion525/jetdb · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions crates/jetdb-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use unicode_width::UnicodeWidthStr;

/// Read-only tool for Microsoft Access (.mdb / .accdb) databases
#[derive(Parser)]
#[command(version, about)]
#[command(name = "jetdb", bin_name = "jetdb", version, about, arg_required_else_help = true)]
struct Cli {
/// Database password (for password-protected .accdb files)
#[arg(long = "password", global = true)]
Expand Down Expand Up @@ -509,11 +509,16 @@ fn main() -> ExitCode {
}

let Some(command) = cli.command else {
// No subcommand and no --help-skill: show help
// Options but no subcommand (e.g. only --password): fail as clap does
// for a missing subcommand. No arguments at all is handled by clap
// (`arg_required_else_help`).
use clap::CommandFactory;
Cli::command().print_help().ok();
eprintln!();
return ExitCode::FAILURE;
Cli::command()
.error(
clap::error::ErrorKind::MissingSubcommand,
"'jetdb' requires a subcommand but one was not provided",
)
.exit();
};

let password = cli.password;
Expand Down
71 changes: 71 additions & 0 deletions crates/jetdb-cli/tests/cli_root.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use std::process::Command;

fn jetdb_bin() -> Command {
Command::new(env!("CARGO_BIN_EXE_jetdb"))
}

// ---------------------------------------------------------------------------
// No subcommand
// ---------------------------------------------------------------------------

#[test]
fn no_arguments_prints_help_to_stderr() {
let output = jetdb_bin().output().expect("failed to run jetdb");
assert_eq!(output.status.code(), Some(2));
assert!(output.stdout.is_empty());
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("Usage: jetdb [OPTIONS] [COMMAND]"),
"stderr should contain the usage, got: {stderr}"
);
}

#[test]
fn options_without_subcommand_is_error() {
let output = jetdb_bin()
.args(["--password", "1234567890"])
.output()
.expect("failed to run jetdb");
assert_eq!(output.status.code(), Some(2));
assert!(output.stdout.is_empty());
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("requires a subcommand"),
"stderr should say a subcommand is required, got: {stderr}"
);
assert!(
stderr.contains("Usage: jetdb [OPTIONS] [COMMAND]"),
"stderr should contain the usage, got: {stderr}"
);
}

// ---------------------------------------------------------------------------
// Top-level options
// ---------------------------------------------------------------------------

#[test]
fn help_skill() {
let output = jetdb_bin()
.arg("--help-skill")
.output()
.expect("failed to run jetdb");
assert!(output.status.success());
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
stdout.starts_with("The jetdb Claude Code Skill has moved"),
"stdout should tell where the skill has moved, got: {stdout}"
);
}

#[test]
fn version() {
let output = jetdb_bin()
.arg("--version")
.output()
.expect("failed to run jetdb");
assert!(output.status.success());
assert_eq!(
String::from_utf8_lossy(&output.stdout),
concat!("jetdb ", env!("CARGO_PKG_VERSION"), "\n")
);
}
Loading
0