Skip to content

Commit 176149f

Browse files
committed
feat(cli): add --completions and --man for shell completions and man page
- Add clap_complete + clap_mangen deps - --completions <bash|zsh|fish|powershell|elvish> emits completion script - --man emits a roff-formatted man page - Path arg becomes optional to allow these meta operations
1 parent a71bdd9 commit 176149f

6 files changed

Lines changed: 92 additions & 3 deletions

File tree

Cargo.lock

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ sha2 = "0.10"
4242
signal-hook = "0.3"
4343
# Cross-platform advisory file locking for concurrent atomic execution prevention
4444
fd-lock = "4"
45+
clap_complete = "4"
46+
clap_mangen = "0.2"
4547

4648
[target.'cfg(target_os = "macos")'.dependencies]
4749
# macOS F_FULLFSYNC support for durable fsync

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ toggle --recover --recover-forward # completes the commit
105105
## Distribution
106106

107107
- **From source:** `cargo install --path .`
108-
- **Shell completions:** `toggle completions bash > /etc/bash_completion.d/toggle`
108+
- **Shell completions:** `toggle --completions bash > /etc/bash_completion.d/toggle`
109109
(also `zsh`, `fish`, `powershell`, `elvish`)
110-
- **Man page:** `toggle man > toggle.1 && man ./toggle.1`
110+
- **Man page:** `toggle --man > toggle.1 && man ./toggle.1`
111111
- **crates.io / Homebrew:** not yet published.
112112

113113
---

src/cli.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// Command-line interface for the Toggle CLI
22

33
use clap::Parser;
4+
use clap_complete::Shell;
45
use std::path::PathBuf;
56

67
#[derive(Parser)]
78
#[command(author, version, about)]
89
pub struct Cli {
910
/// File or directory paths to process
10-
#[arg(required = true)]
1111
pub paths: Vec<PathBuf>,
1212

1313
/// Line range in format <start_line>:<end_line> or <start_line>:+<count> (repeatable)
@@ -123,4 +123,14 @@ pub struct Cli {
123123
/// Must be combined with --recover.
124124
#[arg(long = "recover-forward")]
125125
pub recover_forward: bool,
126+
127+
/// Generate shell completions for the given shell to stdout.
128+
/// Example: `toggle --completions bash > /etc/bash_completion.d/toggle`
129+
#[arg(long = "completions", value_name = "SHELL")]
130+
pub completions: Option<Shell>,
131+
132+
/// Generate a roff-formatted man page to stdout.
133+
/// Example: `toggle --man > toggle.1 && man ./toggle.1`
134+
#[arg(long = "man")]
135+
pub man: bool,
126136
}

src/main.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,26 @@ fn classify_error(err: &anyhow::Error) -> ExitCode {
119119
}
120120

121121
fn run(cli: &Cli) -> Result<()> {
122+
// ── Meta short-circuits: completions and man page ──
123+
if let Some(shell) = cli.completions {
124+
let mut command = <Cli as clap::CommandFactory>::command();
125+
let bin = command.get_name().to_string();
126+
clap_complete::generate(shell, &mut command, bin, &mut std::io::stdout());
127+
return Ok(());
128+
}
129+
if cli.man {
130+
let command = <Cli as clap::CommandFactory>::command();
131+
clap_mangen::Man::new(command)
132+
.render(&mut std::io::stdout())
133+
.context("failed to render man page")?;
134+
return Ok(());
135+
}
136+
137+
// Path is required for everything else
138+
if cli.paths.is_empty() {
139+
return Err(UsageError("at least one file or directory path is required".into()).into());
140+
}
141+
122142
// ── Atomic mode: startup journal check ──
123143
let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
124144
let journal_path = cwd.join(journal::JOURNAL_FILENAME);

tests/integration.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2079,3 +2079,33 @@ fn check_unclosed_marker_exits_nonzero() {
20792079
let stdout = String::from_utf8_lossy(&output.stdout);
20802080
assert!(stdout.contains("unclosed"), "stdout: {stdout}");
20812081
}
2082+
2083+
#[test]
2084+
fn completions_bash_emits_completion_script() {
2085+
let output = cmd().args(["--completions", "bash"]).output().unwrap();
2086+
assert!(output.status.success());
2087+
let stdout = String::from_utf8_lossy(&output.stdout);
2088+
assert!(stdout.contains("_toggle()"), "stdout: {stdout}");
2089+
assert!(stdout.contains("--scan"), "expected flag in completions");
2090+
}
2091+
2092+
#[test]
2093+
fn man_page_emits_roff() {
2094+
let output = cmd().args(["--man"]).output().unwrap();
2095+
assert!(output.status.success());
2096+
let stdout = String::from_utf8_lossy(&output.stdout);
2097+
assert!(
2098+
stdout.contains(".TH toggle"),
2099+
"stdout head: {}",
2100+
&stdout[..stdout.len().min(200)]
2101+
);
2102+
assert!(stdout.contains(".SH NAME"), "expected NAME section");
2103+
}
2104+
2105+
#[test]
2106+
fn no_paths_errors() {
2107+
cmd()
2108+
.assert()
2109+
.failure()
2110+
.stderr(predicate::str::contains("path is required"));
2111+
}

0 commit comments

Comments
 (0)