Code Monkey home page Code Monkey logo

subprocess's Introduction

Subprocess

An asynchronous and synchronous subprocess implementation in C++

Creation of a simple subprocess, where we want to redirect the standard output

  lp::Process sp("ls -C -l --color=yes");

  sp.redirectStdout(true);
  sp.start();
  sp.wait();

  std::cout << sp.getStdout().str() << std::endl;
  std::cout << "Exit code: " << sp.getStatus() << std::endl;

A little more tricky ?

Creation of a subprocess, where we redirect the standard output and error output, thus, we want to call a custom lambda function each time the subprocess wrote something in the error output

  lp::Process sp("ls ImAFileThatDoesNotExist");

  sp.redirectStdout(true);
  sp.redirectStderr(true);
  sp.onReadStderr([](lp::Process &process, std::stringstream &stream)
  {
    std::cout << "subprocess: \"" << stream.str() << "\"" << std::endl;
    //here we can clear the stream from it's content for example
    stream.str().clear();
  });

  sp.start();
  sp.wait();

  std::cout << "command: \"" << sp.getCommand() << "\"" << std::endl;
  std::cout << "exit code: " << sp.getStatus() << std::endl;

Ah, you wanted to simulate an user input into the subprocess ? alright !

Creation of a subprocess, where we redirect the standard input

  lp::Process sp("bash");

  sp.redirectStdin(true);
  sp.start();
  sp.writeStdin("ls && cd .. && mkdir ImAnEmptyFolder && cd ImAnEmptyFolder && touch file.txt\n");
  sp.writeStdin("exit\n");
  sp.wait();

subprocess's People

Contributors

leandrebl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

erikzhou

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.