40 lines
1.1 KiB
C++
40 lines
1.1 KiB
C++
#pragma once
|
|
|
|
#include <optional>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
namespace izo {
|
|
|
|
class CommandLine {
|
|
public:
|
|
CommandLine(int argc, const char* const argv[]);
|
|
CommandLine(int argc, char* argv[]);
|
|
|
|
// Has reports whether an option was supplied, including --name=value.
|
|
bool Has(std::string_view name) const noexcept;
|
|
|
|
// Get returns the last value supplied as --name value or --name=value.
|
|
// A flag without a value and a missing option both return std::nullopt.
|
|
std::optional<std::string> Get(std::string_view name) const;
|
|
std::string Get(std::string_view name, std::string_view fallback) const;
|
|
int GetInt(std::string_view name, int fallback) const;
|
|
|
|
// Arguments not consumed as option values. argv[0] is never included.
|
|
const std::vector<std::string>& Positionals() const noexcept { return positionals_; }
|
|
|
|
private:
|
|
struct Option {
|
|
std::string name;
|
|
std::optional<std::string> value;
|
|
};
|
|
|
|
void Parse(int argc, const char* const argv[]);
|
|
|
|
std::vector<Option> options_;
|
|
std::vector<std::string> positionals_;
|
|
};
|
|
|
|
} // namespace izo
|