Skip to content

Simple library for running new processes in C++20, intended as std::system replacement

License

Notifications You must be signed in to change notification settings

RobertBendun/os-exec

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

os-exec

Simple library for calling external programs. It's intended to be simple replacement for std::system or fork & exec pattern uses.

  • Requires C++20
  • Header only
  • Linux only

What library provides?

Two main functions:

  • os_exec::run(program, args...) - execute program with given arguments
  • os_exec::run_echo(program, args...) - execute program with given arguments, but before it, print what you are about to execute

Program and args can be anything that is convertible to char const*, like std::string, std::filesystem::path or char const* itself

Example

#include <filesystem>
#include <iostream>

#include <os-exec.hh>

int main()
{
	// simplest use
	{
		if (os_exec::run_echo("ls", "-la"))
			std::cerr << "ls failed\n";
	}

	// more precise error handling
	{
		auto const error = os_exec::run("echo", std::filesystem::current_path());

		if (!error) {
			std::cout << "Success!\n";
			return 0;
		}

		if (error.category() == os_exec::non_zero_exit_code()) {
			std::cout << "Exited with value: " << error.value() << '\n';
		} else {
			std::cout << "Error: " << error.message() << '\n';
		}
	}
}

Inspired by

About

Simple library for running new processes in C++20, intended as std::system replacement

Topics

Resources

License

Stars

Watchers

Forks

Languages