Skip to content

Commit 7058888

Browse files
committed
feat: add version flag support
Add --version (-v) flag to display package name and version. Includes integration tests for both long and short flag variants.
1 parent 70d5ff2 commit 7058888

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/main.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ use std::io::{self, Write};
66
#[derive(FromArgs)]
77
/// Copy data to clipboard using OSC52 escape sequences
88
struct Args {
9+
#[argh(switch, short = 'v')]
10+
/// show version information
11+
version: bool,
12+
913
#[argh(positional)]
1014
/// file to copy (reads from stdin if not provided)
1115
file: Option<String>,
@@ -58,6 +62,11 @@ fn stream_to_clipboard_from_file(path: &str) -> io::Result<()> {
5862
fn main() -> io::Result<()> {
5963
let args: Args = argh::from_env();
6064

65+
if args.version {
66+
println!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
67+
return Ok(());
68+
}
69+
6170
if let Some(file_path) = &args.file {
6271
stream_to_clipboard_from_file(file_path)?;
6372
} else {

tests/integration_test.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use std::process::Command;
2+
3+
#[test]
4+
fn test_version_flag() {
5+
let output = Command::new("cargo")
6+
.args(&["run", "--", "--version"])
7+
.output()
8+
.expect("Failed to execute command");
9+
10+
assert!(output.status.success());
11+
let stdout = String::from_utf8(output.stdout).expect("Invalid UTF-8");
12+
let expected = format!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
13+
assert_eq!(stdout.trim(), expected);
14+
}
15+
16+
#[test]
17+
fn test_version_flag_short() {
18+
let output = Command::new("cargo")
19+
.args(&["run", "--", "-v"])
20+
.output()
21+
.expect("Failed to execute command");
22+
23+
assert!(output.status.success());
24+
let stdout = String::from_utf8(output.stdout).expect("Invalid UTF-8");
25+
let expected = format!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
26+
assert_eq!(stdout.trim(), expected);
27+
}

0 commit comments

Comments
 (0)