Git Product home page Git Product logo

rust-system's Introduction

system

Cross-platform crate to easily run shell commands, similar to the C system function.

Usage

system and system_output

For simple use cases where you just need the result of a system command, the system and system_output functions can be used.

system inherits the stdout, stderr, and stdin from the parent process whereas system_output captures stdout and stderr and does not inherit an stdin.

An example of using system,

use system::system;

fn main() {
    // Prints "Hello, world!"
    system("echo Hello, world!").expect("Failed to run command.");
}

An example of using system_output,

use system::system_output;

fn main() {
    let out = system_output("echo Hello, world!").expect("Failed to run command.");
    let stdout = String::from_utf8_lossy(&out.stdout);

    #[cfg(target_os = "windows")]
    assert_eq!(stdout, "Hello, world!\r\n");

    #[cfg(not(target_os = "windows"))]
    assert_eq!(stdout, "Hello, world!\n");
}

std::process::Command::system

For more complex uses cases where the underlying Command has to be modified before running the command, the system::System trait is implemented for Command.

The trait adds the function Command::system to create Commands that execute shell commands.

For example,

use std::process::Command;

use system::System;

fn test() {
    let out = Command::system("echo Hello, world!")
        .output()
        .expect("Failed to run command.");
    let stdout = String::from_utf8_lossy(&out.stdout);

    #[cfg(target_os = "windows")]
    assert_eq!(stdout, "Hello, world!\r\n");

    #[cfg(not(target_os = "windows"))]
    assert_eq!(stdout, "Hello, world!\n");
}

rust-system's People

Contributors

brandonxlf avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.