| 5 | using namespace TinyProcessLib; |
| 6 | |
| 7 | int main() { |
| 8 | #if !defined(_WIN32) || defined(MSYS_PROCESS_USE_SH) |
| 9 | // The following examples are for Unix-like systems and Windows through MSYS2 |
| 10 | |
| 11 | |
| 12 | cout << "Example 1a - the mandatory Hello World through a command" << endl; |
| 13 | Process process1a("echo Hello World", "", [](const char *bytes, size_t n) { |
| 14 | cout << "Output from stdout: " << string(bytes, n); |
| 15 | }); |
| 16 | auto exit_status = process1a.get_exit_status(); |
| 17 | cout << "Example 1a process returned: " << exit_status << " (" << (exit_status == 0 ? "success" : "failure") << ")" << endl; |
| 18 | this_thread::sleep_for(chrono::seconds(2)); |
| 19 | |
| 20 | |
| 21 | cout << endl |
| 22 | << "Example 1b - Hello World using arguments" << endl; |
| 23 | Process process1b(vector<string>{"/bin/echo", "Hello", "World"}, "", [](const char *bytes, size_t n) { |
| 24 | cout << "Output from stdout: " << string(bytes, n); |
| 25 | }); |
| 26 | exit_status = process1b.get_exit_status(); |
| 27 | cout << "Example 1b process returned: " << exit_status << " (" << (exit_status == 0 ? "success" : "failure") << ")" << endl; |
| 28 | this_thread::sleep_for(chrono::seconds(2)); |
| 29 | |
| 30 | |
| 31 | #ifndef _WIN32 |
| 32 | cout << endl |
| 33 | << "Example 1c - Hello World through a function on Unix-like systems" << endl; |
| 34 | Process process1c( |
| 35 | [] { |
| 36 | cout << "Hello World" << endl; |
| 37 | exit(0); |
| 38 | }, |
| 39 | [](const char *bytes, size_t n) { |
| 40 | cout << "Output from stdout: " << string(bytes, n); |
| 41 | }); |
| 42 | exit_status = process1c.get_exit_status(); |
| 43 | cout << "Example 1c process returned: " << exit_status << " (" << (exit_status == 0 ? "success" : "failure") << ")" << endl; |
| 44 | this_thread::sleep_for(chrono::seconds(2)); |
| 45 | #endif |
| 46 | |
| 47 | |
| 48 | cout << endl |
| 49 | << "Example 2 - cd into a nonexistent directory" << endl; |
| 50 | Process process2( |
| 51 | "cd a_nonexistent_directory", "", |
| 52 | [](const char *bytes, size_t n) { |
| 53 | cout << "Output from stdout: " << string(bytes, n); |
| 54 | }, |
| 55 | [](const char *bytes, size_t n) { |
| 56 | cout << "Output from stderr: " << string(bytes, n); |
| 57 | // Add a newline for prettier output on some platforms: |
| 58 | if(bytes[n - 1] != '\n') |
| 59 | cout << endl; |
| 60 | }); |
| 61 | exit_status = process2.get_exit_status(); |
| 62 | cout << "Example 2 process returned: " << exit_status << " (" << (exit_status == 0 ? "success" : "failure") << ")" << endl; |
| 63 | this_thread::sleep_for(chrono::seconds(2)); |
| 64 |
nothing calls this directly
no test coverage detected