From 75f34376eeee449d8d218ae878f778207e62c196 Mon Sep 17 00:00:00 2001 From: Narthana Epa Date: Fri, 30 Aug 2024 20:06:43 +0530 Subject: [PATCH] Use min args in clap derive to ensure a command is provided --- src/main.rs | 39 ++++++++------------------------------- 1 file changed, 8 insertions(+), 31 deletions(-) diff --git a/src/main.rs b/src/main.rs index e8d84f4..acb8903 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use clap::Parser; use eyre::Result; -use std::{path::PathBuf, str::FromStr}; +use std::path::PathBuf; /// A command runner that optionally logs the I/O streams to files. #[derive(Debug, Parser, PartialEq)] @@ -8,18 +8,18 @@ use std::{path::PathBuf, str::FromStr}; struct Cli { /// The file to log stdin to. #[clap(short, long, env)] - in_file: Option, + in_file: Option, /// The file to log stdout to. #[clap(short, long, env)] - out_file: Option, + out_file: Option, /// The file to log stderr to. #[clap(short, long, env)] - err_file: Option, + err_file: Option, /// The command to run and its arguments. A command must be specified, arguments are space delimited. - #[clap(last = true)] + #[clap(last = true, required = true, num_args = 1..)] exec: Vec, } @@ -27,35 +27,12 @@ fn main() -> Result<()> { let cli = Cli::parse(); let exec = cli.exec.iter().map(String::as_str).collect::>(); - if exec.is_empty() { - use clap::CommandFactory; - eprintln!("EXEC must have at least one argument."); - eprintln!("{}", Cli::command().render_long_help()); - std::process::exit(1); - } - - let in_file = cli - .in_file - .as_ref() - .map(|s| PathBuf::from_str(s)) - .transpose()?; - let out_file = cli - .out_file - .as_ref() - .map(|s| PathBuf::from_str(s)) - .transpose()?; - let err_file = cli - .err_file - .as_ref() - .map(|s| PathBuf::from_str(s)) - .transpose()?; - let code = runner::run( exec[0], &exec[1..], - in_file.as_deref(), - out_file.as_deref(), - err_file.as_deref(), + cli.in_file.as_deref(), + cli.out_file.as_deref(), + cli.err_file.as_deref(), )?; std::process::exit(code);