Skip to content

Commit

Permalink
add the command_args! macro
Browse files Browse the repository at this point in the history
  • Loading branch information
magiclen committed Jun 12, 2020
1 parent 33cab7b commit 3b2ab50
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
4 changes: 2 additions & 2 deletions execute-command-macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]
name = "execute-command-macro"
version = "0.1.0"
version = "0.1.1"
authors = ["Magic Len <[email protected]>"]
edition = "2018"
repository = "https://github.com/magiclen/execute"
homepage = "https://magiclen.org/execute"
keywords = ["execute", "command"]
categories = ["parser-implementations"]
description= "Create `Command` instances using the `command!` macro."
description= "Create `Command` instances using the `command!` macro or the `command_args!` marco."
readme = "README.md"
license = "MIT"

Expand Down
8 changes: 7 additions & 1 deletion execute-command-macro/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Execute Command Macro

[![Build Status](https://travis-ci.org/magiclen/execute.svg?branch=master)](https://travis-ci.org/magiclen/execute)

Create `Command` instances using the `command!` macro.
Create `Command` instances using the `command!` macro or the `command_args!` macro.

Also see [`execute`](https://crates.io/crates/execute).

Expand All @@ -15,6 +15,12 @@ Also see [`execute`](https://crates.io/crates/execute).
let command = command!("program arg1 arg2 'arg 3' -opt1 -opt2");
```

```rust
#[macro_use] extern crate execute_command_macro;

let command = command_args!("program", "arg1", "arg2", "-opt1", "-opt2");
```

## Crates.io

https://crates.io/crates/execute
Expand Down
31 changes: 30 additions & 1 deletion execute-command-macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*!
# Execute Command Macro
Create `Command` instances using the `command!` macro.
Create `Command` instances using the `command!` macro or the `command_args!` macro.
Also see [`execute`](https://crates.io/crates/execute).
Expand All @@ -12,6 +12,12 @@ Also see [`execute`](https://crates.io/crates/execute).
let command = command!("program arg1 arg2 'arg 3' -opt1 -opt2");
```
```rust
#[macro_use] extern crate execute_command_macro;
let command = command_args!("program", "arg1", "arg2", "-opt1", "-opt2");
```
*/

#[macro_use]
Expand All @@ -28,3 +34,26 @@ extern crate execute_command_macro_impl;
/// ```
#[proc_macro_hack]
pub use execute_command_macro_impl::command;

/// Create a `Command` instance by inputting args separately.
///
/// ```rust
/// #[macro_use] extern crate execute_command_macro;
///
/// let command = command_args!("program", "arg1", "arg2", "-opt1", "-opt2");
/// ```
#[macro_export]
macro_rules! command_args {
($program:expr $(,)*) => {
std::process::Command::new($program)
};
($program:expr, $arg:expr $(, $args:expr)* $(,)*) => {
{
let mut command = std::process::Command::new($program);

command.arg(&$arg)$(.arg(&$args))*;

command
}
};
}
4 changes: 2 additions & 2 deletions execute-command-macro/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
extern crate execute_command_macro;

#[test]
fn command(){
fn command() {
let mut command = command!("sh -c \"echo '123 456' | cut -d ' ' -f 1\"");

let output = command.output().unwrap();

assert_eq!(b"123\n", output.stdout.as_slice());
}
}

0 comments on commit 3b2ab50

Please sign in to comment.