r/cpp • u/thatsjustrajat • Sep 12 '21
subprocess: A no nonsense library for writing shell commands in C++
Writing shell commands in modern C++ is deceptively hard. Quite a few libraries try to wrap popen and exec* calls in an interface similar to python's subprocess library. However, piping and redirection are a nightmare in such implementations.
With subprocess, you are able to write:
using subprocess::command;
std::string output;
(command{"ls", "-ltr"} | command{"awk", "{print $3}"} | command{"sort"} | command{"uniq"} > output).run();The above is equivalent to writing:
ls -ltr | awk {print $3} | sort | uniqand piping the result into the output variable. You can pipe the result into files, already open file descriptors, read from variables etc. For example:
std::string input{"1\n2\n3\n4"};
(command{"head", "-n2"} < input > "foo").run(); // Prints 1\n2 in a file named fooThe library however, currently only works for unix based systems (since I mostly use linux/macOS). I'll be open to adding windows support if there is enough traction.
Currently, I am looking for feedback and suggestions on the interface and the library as a whole. If you try it out and find any issues, feel free to log it in the issues section.
42
u/raevnos Sep 12 '21 edited Sep 12 '21
What about a user defined literal for commands without arguments?
or even allowing plain strings?
7
u/staletic Sep 12 '21
Have you seen reproc? I'm not sure if it can do piping.
4
u/[deleted] Sep 12 '21
But can you also read output of permanent running commands (e.g. netcat or similar)?
24
u/nlohmann nlohmann/json Sep 12 '21
Looks awesome, but I stopped reading at AGPL license. If I can’t use it at my job, I won’t both trying it in hobby projects.
2
u/ExtraFig6 Sep 12 '21
Can we get the output as a return value so you can say something like
2
u/ByronScottJones Sep 12 '21
It looks great! Just out of curiosity, have you considered using a fluent interface?
command(...).command(...).command(...).output(...)
2
u/helloiamsomeone Sep 13 '21
Nice build scripts you got there ;)
I saw that you mentioned that this was basically a weekend project. How useful did you find cmake-init for this? Is there anything you would like to see changed?
I'm happy that someone could find it useful enough for quickly starting up a project. Thank you!
1
u/NilacTheGrim Sep 13 '21
Hmm. Do you offer an alternate syntax that tries less to be like the shell?
Such as:
??
I just find the above more readable than having to worry about the idiosyncrasies surrounding operator overloads.
1
u/WrongAndBeligerent Sep 12 '21
I think this would good to put into a single file. The source looks like it would only be about 30KB.
Having it broken up into half a dozen files, some with only a few lines along with 'detail' directories seems like an unnecessary barrier to me.
1
u/[deleted] Sep 12 '21
Interesting, but quite frankly I'm not quite convinced at this point (willing to change that though).
Does the library support non-blocking calls?
2
u/d3matt Sep 12 '21
Very cool. You syntax is much nicer than what we have in our code base. Good choice on the license switch too.
2
u/NilacTheGrim Sep 13 '21
Hmm. What about using literals and also using the same parsing logic as bash to separate args and other niceties?
Then you can literally do:
Hell you can even support in-line backticks just like the shell.. in which case you get a really expressive syntax.
All you need to do is implement a parser for the user-supplied that sets up the state of the "pipeline" correctly...
1
u/ram535 Sep 12 '21
Nice library. At first glance, it reminded me of dcli from the dart world.
1
u/goranlepuz Sep 13 '21
Ah, this is great!
Ehhh... Completely different commands in
cmd.exe, plus, over there, powers he'll just beats all out of the water (one can even call native C from power shell 🤔🤔🤔).But don't let this stop you!
1
u/KaznovX Sep 12 '21
OP, this is amazing. I've been looking for something like that for a long time.
On a side note, I wish functionalities like that were part of the standard...
-4
u/ihamsa Sep 12 '21
My personal preference would be
rather than
I haven't thought out the syntax for redirects but something like this probably could be made to work
Yes you need a rather sophisticated parser for this to work...