Giter Site home page Giter Site logo

vishwaas-s / subprocess Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rajatjain1997/subprocess

0.0 0.0 0.0 117 KB

A C++ high level library for running shell processes

Home Page: https://subprocess.thecodepad.com

License: Boost Software License 1.0

C++ 63.32% CMake 36.23% Shell 0.45%

subprocess's Introduction

subprocess

cppver build issues GitHub stars

There are many C++ subprocessing libraries out there, but none of them just work. The aim of this library is to allow you to write shell commands in C++ almost as if you were writing them in shell.

Full documentation for subprocess is available here.

Note: Windows is not currently supported. I would like to pin down the library interface before adding compatibility for the OS.

TL;DR

It works exactly how you would expect it to work.

#include <subprocess.hpp>

using subprocess::command;

int main(int argc, char* argv[])
{
    std::string owners;
    (command{"ls", "-l", argv[1]} | command{"awk", "NR>1{print $3}"} | command{"sort"} | command{"uniq"} > owners).run();
    // Use the owners as you wish
}

Philosophy

Instead of trying to emulate the subprocess interface from libraries in other languages, this library aims to use (and abuse) C++ operator overloading to achieve a shell-like syntax for writing shell commands.

Overview

You can write shell commands using the subprocess::command class and use the resulting object to pipe I/O from/to standard streams, files, and variables.

Examples:

// Running a command
subprocess::command cmd{"touch", file_path}.run();

// Piping the output of a command to another command
cmd | subprocess::command{"uniq"};

Redirecting stdin

You can use operator< to redirect stdin to the command object.

Examples:

// Reading input from a file
cmd < std::filesystem::path{file_name};
cmd < "file_name";

// Reading input from a variable
std::string input{"abc"};
cmd < input;

// Reading from an already created fd
cmd < subprocess::file_descriptor{fd_no};

Redirecting stdout

The following operators are available for redirecting stdout:

  • operator>: Truncates the file and writes output
  • operator>>: Appends output to file if it already exists, otherwise creates one.

Examples:

// Redirecting stdout to stderr
cmd > subprocess::err();

// Redirecting stdout to a file
cmd > std::filesystem::path{file_name};

// or appending to a file
cmd >> std::filesystem::path{file_name};

// Capturing stdout in a variable
std::string var_name;
cmd > var_name;

Redirecting stderr

The following operators are available for redirecting stdout:

  • operator>=: Truncates the file and writes stderr
  • operator>>=: Appends stderr to file if it already exists, otherwise creates one.

Examples:

// Redirecting stderr to stdout
cmd >= subprocess::err();

// Redirecting stderr to a file
cmd >= std::filesystem::path{file_name};

// or appending to a file
cmd >>= std::filesystem::path{file_name};

// Capturing stderr in a variable
std::string var_name;
cmd >= var_name;

Conclusion

I would love your feedback! If you find any issues, feel free to log them.

subprocess's People

Contributors

rajatjain1997 avatar nlohmann 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.