Skip to content

Commit

Permalink
refactoring custom command registration
Browse files Browse the repository at this point in the history
To make it less error-prone
  • Loading branch information
tao-guo committed Nov 23, 2023
1 parent 7b3bed0 commit 875488e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
18 changes: 9 additions & 9 deletions src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,38 @@ use crate::{CmdEnv, CmdResult};
use std::io::{Read, Write};

pub(crate) fn builtin_echo(env: &mut CmdEnv) -> CmdResult {
let args = env.args();
let msg = if args.len() > 1 && args[1] == "-n" {
args[2..].join(" ")
let args = env.get_args();
let msg = if !args.is_empty() && args[0] == "-n" {
args[1..].join(" ")
} else {
args[1..].join(" ") + "\n"
args.join(" ") + "\n"
};

write!(env.stdout(), "{}", msg)
}

pub(crate) fn builtin_error(env: &mut CmdEnv) -> CmdResult {
error!("{}", env.args()[1..].join(" "));
error!("{}", env.get_args().join(" "));
Ok(())
}

pub(crate) fn builtin_warn(env: &mut CmdEnv) -> CmdResult {
warn!("{}", env.args()[1..].join(" "));
warn!("{}", env.get_args().join(" "));
Ok(())
}

pub(crate) fn builtin_info(env: &mut CmdEnv) -> CmdResult {
info!("{}", env.args()[1..].join(" "));
info!("{}", env.get_args().join(" "));
Ok(())
}

pub(crate) fn builtin_debug(env: &mut CmdEnv) -> CmdResult {
debug!("{}", env.args()[1..].join(" "));
debug!("{}", env.get_args().join(" "));
Ok(())
}

pub(crate) fn builtin_trace(env: &mut CmdEnv) -> CmdResult {
trace!("{}", env.args()[1..].join(" "));
trace!("{}", env.get_args().join(" "));
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@
//! # use cmd_lib::*;
//! # use std::io::Write;
//! fn my_cmd(env: &mut CmdEnv) -> CmdResult {
//! let args = env.args();
//! let args = env.get_args();
//! let (res, stdout, stderr) = spawn_with_output! {
//! orig_cmd $[args]
//! --long-option xxx
Expand Down
9 changes: 7 additions & 2 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ pub struct CmdEnv {
current_dir: PathBuf,
}
impl CmdEnv {
/// Returns the name of this command.
pub fn get_cmd_name(&self) -> &str {
&self.args[0]
}

/// Returns the arguments for this command.
pub fn args(&self) -> &[String] {
&self.args
pub fn get_args(&self) -> &[String] {
&self.args[1..]
}

/// Fetches the environment variable key for this command.
Expand Down
4 changes: 2 additions & 2 deletions tests/test_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,13 @@ fn test_proc_env() {
fn test_export_cmd() {
use std::io::Write;
fn my_cmd(env: &mut CmdEnv) -> CmdResult {
let msg = format!("msg from foo(), args: {:?}", env.args());
let msg = format!("msg from foo(), args: {:?}", env.get_args());
writeln!(env.stderr(), "{}", msg)?;
writeln!(env.stdout(), "bar")
}

fn my_cmd2(env: &mut CmdEnv) -> CmdResult {
let msg = format!("msg from foo2(), args: {:?}", env.args());
let msg = format!("msg from foo2(), args: {:?}", env.get_args());
writeln!(env.stderr(), "{}", msg)?;
writeln!(env.stdout(), "bar2")
}
Expand Down

0 comments on commit 875488e

Please sign in to comment.