Represents a command to be executed in the operating system shell.
More...
#include <bob.hpp>
|
bool | capture_output = false |
| If true, the command's output will be captured.
|
|
bool | silent = false |
| If true, the command will not print its output to stdout.
|
|
string | output_str = "" |
| The command's output captured during execution (stdout and stderr).
|
|
path | root = "." |
| The root directory from which the command is executed.
|
|
Represents a command to be executed in the operating system shell.
This class allows building command lines from parts (strings or paths), running them synchronously or asynchronously, and optionally capturing their output. It supports managing execution context such as the root directory and controlling output visibility (using the silent
field).
- Example - Synchronous Command Execution
Cmd compile({
"g++",
I(
"src"),
"-o",
"output"});
int status = compile.run();
if (status != 0) {
std::cerr << "Compilation failed:\n" << compile.output_str;
}
Represents a command to be executed in the operating system shell.
Definition bob.hpp:287
bool capture_output
If true, the command's output will be captured.
Definition bob.hpp:291
- Example - Asynchronous Command Execution
while (!fut.done) {
cmd.poll_future(fut);
std::cout << "waiting for command to finish...\n";
usleep(100000);
}
std::cout << "Command finished with exit code: " << fut.exit_code << "\n";
Represents a command that being executed in the background.
Definition bob.hpp:235
- Examples
- minimal/bob.cpp, parallel-cmds/bob.cpp, and recipe/bob.cpp.
◆ Cmd()
bob::Cmd::Cmd |
( |
vector< string > && |
parts, |
|
|
path |
root = "." |
|
) |
| |
Creates a command from a list of parts and optional root directory.
- Parameters
-
parts | The command and its arguments as string parts. |
root | The root directory where the command runs (default is current directory). |
- Example
Cmd compile({
"g++",
"-o",
"app"},
"/home/user/project");
◆ await_future()
int bob::Cmd::await_future |
( |
CmdFuture & |
fut | ) |
|
Waits for an asynchronous command to finish and captures its output.
- Parameters
-
fut | The CmdFuture object representing the running command. |
- Returns
- The command's exit status.
- Example
int exit_code = cmd.await_future(fut);
◆ check()
Runs the command and throws if the exit status is not zero.
- Exceptions
-
std::runtime_error | if the command exits with a non-zero status. |
- Example
Cmd cmd({
"echo",
"Hello, World!"});
◆ clear()
Clears all parts of the command so it can be reused.
- Example
Cmd cmd({
"echo",
"Hello"});
cmd.clear();
cmd.push("echo").push(" world!");
cmd.run();
◆ poll_future()
bool bob::Cmd::poll_future |
( |
CmdFuture & |
fut | ) |
|
Polls an ongoing asynchronous command to check if it has finished. Captures and prints output based on the capture_output
and silent
fields.
- Parameters
-
fut | The CmdFuture object representing the running command. |
- Returns
- True if the command has completed; false otherwise.
- Example
while (!cmd.poll_future(fut)) {
std::cout << "Waiting..." << std::endl;
}
std::cout << "Done!" << std::endl;;
◆ push()
Cmd & bob::Cmd::push |
( |
const string & |
part | ) |
|
Adds a single part (argument or command) to the command.
- Parameters
-
- Returns
- Reference to this command (for chaining).
- Example
Cmd & push(const string &part)
- Examples
- parallel-cmds/bob.cpp, and recipe/bob.cpp.
◆ push_many()
Cmd & bob::Cmd::push_many |
( |
const vector< string > & |
parts | ) |
|
Adds multiple string parts to the command.
- Parameters
-
- Returns
- Reference to this command.
- Example
const vector<string> FLAGS = {"-Wall", "-O2"};
const string COMPILER = "g++";
cmd
Cmd & push_many(const vector< string > &parts)
◆ render()
string bob::Cmd::render |
( |
| ) |
const |
Creates a printable string representation of the command.
- Returns
- The command rendered as a string.
- Example
std::cout << cmd.
render() << std::endl;
◆ run()
◆ run_async()
Runs the command asynchronously and returns a future object.
- Returns
- A CmdFuture object representing the running command.
- Example
-
The documentation for this class was generated from the following file: