Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
Options

A simple and minimalistic UNIX-style command-line parsing library. More...

Classes

struct  sourcemeta::core::OptionsModifiers
class  sourcemeta::core::Options
class  sourcemeta::core::OptionsError
struct  sourcemeta::core::OptionsUnknownOptionError
struct  sourcemeta::core::OptionsUnexpectedValueFlagError
struct  sourcemeta::core::OptionsMissingOptionValueError

Detailed Description

A simple and minimalistic UNIX-style command-line parsing library.

This functionality is included as follows:

#include <sourcemeta/core/options.h>

Class Documentation

◆ sourcemeta::core::OptionsModifiers

struct sourcemeta::core::OptionsModifiers

Command-line option parsing modifiers

Public Attributes

std::size_t skip {0}
 Ignore the first N command-line arguments.

◆ sourcemeta::core::Options

class sourcemeta::core::Options

This class performs basic command-line argument parsing based on options, flags, and aliases. For example:

#include <sourcemeta/core/options.h>
#include <cstdlib>
#include <iostream>
auto main(int argc, char *argv[]) -> int {
app.option("name", {"n"});
app.flag("shout", {"s"});
app.parse(argc, argv);
if (!app.contains("name")) {
std::cerr << "Missing name\n";
return EXIT_FAILURE;
}
std::cerr << "Hello, " << app.at("name").front();
if (app.contains("shout")) {
std::cerr << "!\n";
} else {
std::cerr << "\n";
}
return EXIT_SUCCESS;
}
auto parse(const int argc, const char *const argv[], const OptionsModifiers options={}) -> void
Parse program arguments given the declared options and flags.
auto at(const std::string_view name) const -> const std::vector< std::string_view > &
Access the values (if any) set for an option or flag, by its main name.
auto contains(const std::string_view name) const -> bool
Check if an option or flag was set, by its main name.
auto option(std::string &&name, std::initializer_list< std::string > aliases) -> void
Declare a new option, which must take a value.
auto flag(std::string &&name, std::initializer_list< std::string > aliases) -> void
Declare a new flag, which must not take a value.
Definition options.h:70

Public Member Functions

 Options (const Options &)=delete
 Options (Options &&) noexcept=default
 Move constructor.
auto option (std::string &&name, std::initializer_list< std::string > aliases) -> void
 Declare a new option, which must take a value.
auto flag (std::string &&name, std::initializer_list< std::string > aliases) -> void
 Declare a new flag, which must not take a value.
auto at (const std::string_view name) const -> const std::vector< std::string_view > &
 Access the values (if any) set for an option or flag, by its main name.
auto contains (const std::string_view name) const -> bool
 Check if an option or flag was set, by its main name.
auto positional () const -> const std::vector< std::string_view > &
 Access the positional arguments, if any.
auto parse (const int argc, const char *const argv[], const OptionsModifiers options={}) -> void
 Parse program arguments given the declared options and flags.

◆ sourcemeta::core::OptionsError

class sourcemeta::core::OptionsError

This class represents a general options error

Inheritance diagram for sourcemeta::core::OptionsError:
sourcemeta::core::OptionsMissingOptionValueError sourcemeta::core::OptionsUnexpectedValueFlagError sourcemeta::core::OptionsUnknownOptionError

Public Member Functions

 OptionsError (const char *message)
 Construct the error given a message.
 OptionsError (std::string message)=delete
 OptionsError (std::string &&message)=delete
 OptionsError (std::string_view message)=delete

◆ sourcemeta::core::OptionsUnknownOptionError

struct sourcemeta::core::OptionsUnknownOptionError

This class represents a unknown option error

Inheritance diagram for sourcemeta::core::OptionsUnknownOptionError:
sourcemeta::core::OptionsError

Public Member Functions

 OptionsUnknownOptionError (const std::string_view option)
 Construct the error given the offending option.
auto option () const noexcept -> std::string_view
 The offending option.
Public Member Functions inherited from sourcemeta::core::OptionsError
 OptionsError (const char *message)
 Construct the error given a message.
 OptionsError (std::string message)=delete
 OptionsError (std::string &&message)=delete
 OptionsError (std::string_view message)=delete

◆ sourcemeta::core::OptionsUnexpectedValueFlagError

struct sourcemeta::core::OptionsUnexpectedValueFlagError

This class represents a value being passed to a flag

Inheritance diagram for sourcemeta::core::OptionsUnexpectedValueFlagError:
sourcemeta::core::OptionsError

Public Member Functions

 OptionsUnexpectedValueFlagError (const std::string_view option)
 Construct the error given the offending option.
auto option () const noexcept -> std::string_view
 The offending option.
Public Member Functions inherited from sourcemeta::core::OptionsError
 OptionsError (const char *message)
 Construct the error given a message.
 OptionsError (std::string message)=delete
 OptionsError (std::string &&message)=delete
 OptionsError (std::string_view message)=delete

◆ sourcemeta::core::OptionsMissingOptionValueError

struct sourcemeta::core::OptionsMissingOptionValueError

This class represents a missing value from an option

Inheritance diagram for sourcemeta::core::OptionsMissingOptionValueError:
sourcemeta::core::OptionsError

Public Member Functions

 OptionsMissingOptionValueError (const std::string_view option)
 Construct the error given the offending option.
auto option () const noexcept -> std::string_view
 The offending option.
Public Member Functions inherited from sourcemeta::core::OptionsError
 OptionsError (const char *message)
 Construct the error given a message.
 OptionsError (std::string message)=delete
 OptionsError (std::string &&message)=delete
 OptionsError (std::string_view message)=delete