Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

WesleyCh3n/process-cpp

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Process cpp

A c++ implementation similar to Rust's std::process.

Quick Start

Following quick start is for linux and mac

  1. get the library to your directory
wget https://github.com/WesleyCh3n/process-cpp/raw/refs/heads/main/src/process.hpp
wget https://github.com/WesleyCh3n/process-cpp/raw/refs/heads/main/src/unix.cpp
  1. create a new file named example.cpp with following:
#include "process.hpp"

#include <iomanip>
#include <iostream>

int main(int argc, char *argv[]) {
  auto output =
      process::Command("sh").arg("-c").arg("echo hello world").output();
  std::cout << std::boolalpha << output.status.success() << '\n';
  std::cout << std::quoted(output.std_out) << '\n';
  std::cout << std::quoted(output.std_err) << '\n';
}
  1. Compile
c++ -std=c++20 -o example example.cpp unix.cpp
  1. ./example
true
"hello world
"
""

Usage

For more detail, check examples and process.hpp.

#include "process.hpp"

#include <iomanip>
#include <iostream>

int main() {
    {
        // windows. wait for output
        Output output = Command("cmd").args({"/c", "echo hello world"}).output();
        std::cout << std::quoted(output.std_out) << '\n';
        std::cout << std::quoted(output.std_err) << '\n';
    }
    {
        // unix. get child handle and wait for output
        Child child = Command("sh")
            .arg("-c")
            .arg("echo hello world")
            .std_out(Stdio::pipe())
            .std_err(Stdio::pipe())
            .spawn();
        Output output = child.wait_with_output();

        std::cout << std::quoted(output.std_out) << '\n';
        std::cout << std::quoted(output.std_err) << '\n';
    }
    {
        // write to process stdin
        std::string app = "some_app_need_stdin";
        auto child = Command(app).std_in(Stdio::pipe()).spawn();

        std::vector<std::string> strs{"hello\n", "from\n", "parent\n"};
        for (auto &str : strs) {
            child.io_stdin->write(std::as_bytes(std::span{str}));
        }
        ExitStatus status = child.wait();
    }
    {
        // pipe process one stdout to process 2 stdin
        // p1.stdout -> p2.stdin
        auto child1 = Command(app1).args(args1).std_out(Stdio::pipe()).spawn();
        auto child2 = Command(app2)
            .args(args2)
            .std_in(Stdio::from(std::move(*child1.io_stdout)))
            .spawn();
        ExitStatus status = child2.wait();
    }
}

Build and Install

cmake -Bbuild .
cmake --build build -j

For more CMake options, see CMakeLists.txt

Run Test

After test built,

ctest --output-on-failure --test-dir ./build/test/

License

About

A rust-like C++ API to easily create process, pipe stdio, etc on win/unix

Topics

Resources

Stars

Watchers

Forks

Contributors

Morty Proxy This is a proxified and sanitized view of the page, visit original site.