cross platform subprocess library for c++ similar to design of python subprocess. See subprocess documentation for further documentation.
supports
- very python like style of subprocess. With very nice syntax for c++20.
- Connect output of process A to input of process B. However not pretty API for this.
Shakey elements
- The os error level exceptions is still changing. I'm thinking of having an OSError subclass to abstract the OS differences.
requirements
- c++17
- linked with support for threading, filesystem
Integration
Adhoc
- copy files in src/cpp to your project.
- add the top folder as include.
- make sure cpp files are compiled.
- add
#include <subprocess.hpp>
to start using in source files.
Todo add to cocoapods and perhaps others.
Examples
Simple examples
RunBuilder.cout(PipeOption::pipe));
RunBuilder().cin("hello world"));
PopenBuilder().cin("hello world").cout(PipeOption::pipe));
std::cout <<
"captured: " << process.
cout <<
'\n';
PopenBuilder().cerr(PipeOption::pipe)
.cout(PipeOption::pipe)
.check(true)
);
std::cout <<
"cerr was: " << process.
cerr;
.cout = PipeOption::pipe,
.check = false
});
std::cout <<
"captured: " << process.
cout <<
'\n';
Popen examples
These examples show using the library while the subprocess is still running.
Popen popen =
subprocess::Popen({
"echo",
"hello",
"world"}, RunBuilder().cout(PipeOption::pipe));
char buf[1024] = {0};
std::cout << buf;
popen.close();
Popen popen =
subprocess::Popen({
"cat"}, RunBuilder().cin(PipeOption::pipe).cout(PipeOption::pipe));
std::thread write_thread([&]() {
pipe_write(popen.cin,
"hello world\n", strlen(
"hello world\n"));
popen.cin_close();
});
char buf[1024] = {0};
std::cout << buf;
popen.close();
if (write_thread.joinable())
write_thread.join();
Design
stdin, stdout, stderr are macros, so it's not possible to use those variable names. Instead c++ variable names is used cin, cout, cerr where the std* would have been respectively.
PopenBuilder was a bad idea. Removed, now only RunBuilder remains which is enough. Have both is too confusing.
current progress
All tests are passing on macos, linux. Most tests pass on cross compiling on linux for windows using mingw. The library is almost feature complete.
must to be implemented
- documentation
- bugs, there is lots of them to be discovered.
- main structure is set in place and help is welcome.
not good
- due to types c++ is more wordy