TShellCommand provides a lightweight interface for executing external shell commands and capturing their output. It reads from the command’s standard output stream and accumulates the text in a buffer. The captured output is split into individual lines and stored in the class’s underlying TStringVector;StringVector".
\par Usage options:
- Pure static usage: Call the static Run method with a target buffer
and an optional vector for collecting lines.
The new output is appended to the provided buffer; if a vector is
supplied, the newly captured portion is tokenized into lines and added to it.
- Instance-based usage: Create an instance and call its non-static Run
method.
By default, this clears the instance’s buffer and line vector before executing.
To preserve the current content and append new output, pass
Keep.
Method Run can be sequentially invoked multiple times to aggregate the
output of several shell commands.
With the static Run variant, the same buffer and vector is to be passed
to continue appending.
With the non-static Run, the parameter <span>keepCurrent</span> controls whether
existing content is retained or not.
@see This is a very simple wrapper type. For example, no blocking or timely shell commands
can be handled. While simple, relient commands can be invoked with this class, more
complicated cases should be handled with alternatives, as:
- <a href="https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html" >boost.process <img src="external_link.svg" height="12" width="10"></a>
- <a href="https://docs.pocoproject.org/current/Poco.Process.html" >POCO Process <img src="external_link.svg" height="12" width="10"></a>, or
- <a href="https://github.com/arun11299/cpp-subprocess" >cpp-subprocess <img src="external_link.svg" height="12" width="10">.
Definition at line 43 of file shellcommand.hpp.
#include <shellcommand.hpp>
Public Type Index: | |
| using | AllocatorType = TAllocator |
| The allocator type that TAllocator specifies. | |
| using | base = strings::util::TStringVector<nchar, TAllocator> |
| The vector type that TAllocator specifies. | |
| using | vectorBase = base::vectorBase |
| The vector type that TAllocator specifies. | |
| Public Type Index: inherited from alib::strings::util::TStringVector< nchar, lang::HeapAllocator > | |
| using | AllocatorType |
| The allocator type that TAllocator specifies. | |
Public Static Method Index: | |
| static int | Run (const NCString &cmd, strings::TAString< nchar, AllocatorType > &readBuffer, strings::util::TStringVector< nchar, TAllocator > *lines=nullptr) |
Public Field Index: | |
| strings::TAString< nchar, AllocatorType > | ReadBuffer |
| The input buffer, collecting the output of the invoked shell command(s). | |
Public Method Index: | |
| TShellCommand () | |
| Default constructor. Usable with type HeapAllocator. | |
| TShellCommand (AllocatorType &ma) | |
| ~TShellCommand () | |
| int | Run (const NCString &cmd, lang::CurrentData keepData=lang::CurrentData::Clear) |
| Public Method Index: inherited from alib::strings::util::TStringVector< nchar, lang::HeapAllocator > | |
| TStringVector () | |
| Constructor. | |
| TStringVector (AllocatorType &pAllocator) | |
| ~TStringVector ()=default | |
| Destructor. | |
| integer | Add (const strings::TString< nchar > &src) |
| AllocatorType & | GetAllocator () noexcept |
| integer | Size () const noexcept |
| String | TryGet (integer idx) |
Additional Inherited Members | |
| Protected Type Index: inherited from alib::strings::util::TStringVector< nchar, lang::HeapAllocator > | |
| using | vectorBase |
| The vector type that TAllocator specifies. | |
| using alib::system::TShellCommand< TAllocator >::AllocatorType = TAllocator |
The allocator type that TAllocator specifies.
Definition at line 45 of file shellcommand.hpp.
| using alib::system::TShellCommand< TAllocator >::base = strings::util::TStringVector<nchar, TAllocator> |
The vector type that TAllocator specifies.
Definition at line 48 of file shellcommand.hpp.
| using alib::system::TShellCommand< TAllocator >::vectorBase = base::vectorBase |
The vector type that TAllocator specifies.
Definition at line 51 of file shellcommand.hpp.
| strings::TAString<nchar, AllocatorType> alib::system::TShellCommand< TAllocator >::ReadBuffer |
The input buffer, collecting the output of the invoked shell command(s).
Definition at line 55 of file shellcommand.hpp.
|
inline |
Default constructor. Usable with type HeapAllocator.
Definition at line 58 of file shellcommand.hpp.
|
inline |
Destructor. Deletes all strings in the vector of output lines.
Definition at line 62 of file shellcommand.hpp.
|
inline |
Constructor taking an allocator.
| ma | The allocator to use. |
Definition at line 70 of file shellcommand.hpp.
|
inline |
Executes the given command-line by invoking the static variant of this method passing member ReadBuffer and the inherited string vector (*this).
| cmd | The command to execute. |
| keepData | Denotes whether any prior results are kept or not. |
Definition at line 80 of file shellcommand.hpp.
|
inlinestatic |
Executes the given command-line.
The given readBuffer and vector lines are not reset. Instead, the command result is appended to both. If this is not wanted, the method Reset has to be called on the given readBuffer and the method std::vector::clear on the given lines instance, before calling this method.
| cmd | The command to execute. |
| readBuffer | A string buffer to receive the command's output. |
| lines | An optional pointer to a vector of strings, which receives the lines of the output text. |
Definition at line 98 of file shellcommand.hpp.