101 lines
3.7 KiB
C++
101 lines
3.7 KiB
C++
#pragma once
|
|
#include <algorithm>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
struct FlagInfo {
|
|
std::string canonical_name;
|
|
bool expects_arg;
|
|
std::optional<std::string> arg;
|
|
};
|
|
|
|
struct ShortFlag {
|
|
|
|
};
|
|
|
|
struct LongFlag {};
|
|
|
|
struct Subcommand {};
|
|
|
|
struct ArgFlag {};
|
|
|
|
|
|
/// A class that provides a convenient interface for parsing commands.
|
|
class ArgsParser {
|
|
public:
|
|
/// Constructor from traditional argc, argv.
|
|
ArgsParser(int argc, char** argv, bool keep_program_name = false);
|
|
/// Constructor from an existing vector of strings,
|
|
explicit ArgsParser(const std::vector<std::string>& value): args(value) {}
|
|
|
|
/// @return Whether the unconsumed-arguments list is empty.
|
|
[[nodiscard]] bool empty() const;
|
|
|
|
/// @return True if there is another unconsumed token in the argument list.
|
|
[[nodiscard]] bool has_next() const;
|
|
|
|
/// Retrieves the next unconsumed token in the argument list.
|
|
/// @return The next token in the argument list.
|
|
[[nodiscard]] std::string get_next() const;
|
|
|
|
/// Retrieves the next unconsumed token in the argument list, returns the value, and removes it from the list.
|
|
std::string consume_next();
|
|
|
|
/// Searches the arguments for the given option flag, and returns whether it was found.
|
|
/// @return True if the flag is present, false otherwise.
|
|
bool has_flag(const std::string& option);
|
|
|
|
/// Searches the arguments for an option flag that matches the given strings, and returns whether any were found.
|
|
bool has_flag(const std::initializer_list<std::string>& flags);
|
|
|
|
/// Searches the arguments for the given flag token, and removes all occurrences of it.
|
|
/// @return true if the flag was found in the arguments.
|
|
bool consume_flag(const std::string& option);
|
|
|
|
|
|
/// Searches the arguments for any of the given flag tokens, and removes them.
|
|
/// @note All strings in the list will be removed from the arguments.
|
|
/// @return True if the flag was found in the arguments.
|
|
bool consume_flag(const std::initializer_list<std::string>& flags);
|
|
|
|
/// Searches for a specific flag and its argument.
|
|
bool has_flag_arg(const std::string& option);
|
|
|
|
/// Retrieves the argument for the given flag.
|
|
std::optional<std::string> get_flag_arg(const std::string& option);
|
|
|
|
/// Searches for a specific flag and its argument, removes both, and returns the argument.
|
|
std::optional<std::string> consume_flag_arg(const std::string& flag);
|
|
|
|
std::optional<std::string> consume_flag_arg(const std::initializer_list<std::string>& flags);
|
|
|
|
/// @return all tokens that have not yet been matched and consumed by other methods.
|
|
[[nodiscard]] std::vector<std::string> get_remaining_args() const;
|
|
|
|
/// Searches for a given string in the arguments list.
|
|
/// @return true if found, false otherwise.
|
|
[[nodiscard]] bool contains(const std::string& token) const;
|
|
|
|
/// Searches for an argument that contains the given string.
|
|
[[nodiscard]] bool has_flag_contains(const std::string& match) const;
|
|
|
|
/// Searches for an argument that contains the given string.
|
|
std::optional<std::string> get_flag_contains(std::string& match);
|
|
|
|
[[nodiscard]] bool has_flag_starts_with(const std::string& value) const;
|
|
[[nodiscard]] std::string get_flag_starts_with(const std::string& prefix) const;
|
|
|
|
|
|
std::vector<std::string> consume_flag_arg_multiple(const std::string& flag);
|
|
|
|
std::vector<std::string> consume_flag_arg_multiple(const std::initializer_list<std::string>& flag_aliases);
|
|
|
|
protected:
|
|
/// The list of arguments that have yet to be consumed.
|
|
std::vector<std::string> args{};
|
|
/// The full list of arguments originally passed to the instance. This vector will not be modified.
|
|
std::vector<std::string> original_args{};
|
|
private:
|
|
};
|