5 releases

0.1.4 Jun 28, 2024
0.1.3 Jun 28, 2024
0.1.2 Jun 28, 2024
0.1.1 Jun 28, 2024
0.1.0 Jun 28, 2024

#362 in Command-line interface

MIT/Apache

13KB
191 lines

command_runner

command_runner is a cross-platform Rust crate designed for executing terminal commands interactively. It wraps various features in a struct to provide a seamless command execution experience.

Key Features

  1. Execute Command Line Instructions: Run any command line instruction from within your Rust application.
  2. Check Command Execution Status: Determine if a command executed successfully.
  3. Fetch Command Output: Retrieve the real-time output of the command, similar to what you would see in a terminal.
  4. Handle User Input: If a running command requires user input, the crate provides a way to input data easily while still capturing the command's output.
  5. Cross-Platform Compatibility: Works seamlessly across different platforms, including Linux, macOS, Windows, and mobile platforms like Android.

Installation

Add the following to your Cargo.toml:

[dependencies]
command_runner = "*"

Usage Example

use anyhow::Result;
use command_runner::CommandRunner;

fn main() -> Result<()> {
    let mut runner = CommandRunner::new("echo Hello, World!", 1024)?;
    // Check if the command was executed successfully
    if runner.get_status() == CommandStatus::Terminated {
        println!("Command executed successfully!");
    }
    // Get the command output
    while let Some(output) = runner.get_output() {
        println!("Command output: {}", output);
    }
    // Handle user input
    runner.execute("read -p 'Enter your name: ' name && echo \"Hello, $name\"")?;
    runner.provide_input("John Doe\n")?;
    // Get the final output
    while let Some(final_output) = runner.get_output() {
        println!("Final output: {}", final_output);
    }

    Ok(())
}

Documentation

For more detailed usage instructions and API documentation, please visit docs.rs.

Contribution

We welcome issues and pull requests. For major changes, please open an issue first to discuss what you would like to change.

License

This crate is dual-licensed under either:

Choose the license that best fits your needs.

Dependencies

~3.5MB
~121K SLoC