command_runner

Crates.iocommand_runner
lib.rscommand_runner
version0.1.4
sourcesrc
created_at2024-06-28 13:23:08.388481
updated_at2024-06-28 15:21:17.722584
descriptionA library for running system commands
homepage
repositoryhttps://github.com/dbsxdbsx/command_runner
max_upload_size
id1286714
size13,164
老董 (dbsxdbsx)

documentation

https://docs.rs/command_runner

README

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.

Commit count: 63

cargo fmt