ArgsParser methods implemented.
This commit is contained in:
15
README.md
15
README.md
@@ -1,3 +1,16 @@
|
||||
# ArgsParser
|
||||
|
||||
A C++ library for parsing command arguments.
|
||||
A C++ library for parsing command arguments.
|
||||
|
||||
## Brief
|
||||
|
||||
The library in it's current state performs only the parsing of tokens from a command string.
|
||||
Implementing the functionality of flags, options, subcommands, etc is left up to the user.
|
||||
Plans for a more comprehensive parsing suite are in the works, however.
|
||||
|
||||
## API Overview
|
||||
|
||||
## Usage
|
||||
|
||||
## Known Bugs
|
||||
|
||||
|
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#include <algorithm>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -53,7 +54,8 @@ public:
|
||||
bool consume_flag(const std::string& option);
|
||||
|
||||
|
||||
/// Searches the arguments for any given flag tokens, and removes them.
|
||||
/// 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);
|
||||
|
||||
@@ -68,22 +70,17 @@ public:
|
||||
|
||||
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;
|
||||
|
||||
[[nodiscard]] std::vector<std::string> get_unmatched() const;
|
||||
|
||||
|
||||
/// Searches for a given string in the arguments list.
|
||||
/// @return true if found, false otherwise.
|
||||
[[nodiscard]] bool contains(const std::string& token) const;
|
||||
|
||||
[[nodiscard]] bool has_flag_contains(const std::string& match) const;
|
||||
|
||||
std::optional<std::string> get_flag_contains(std::string& match) {
|
||||
for (auto& token : args) {
|
||||
if (token.find(match) != std::string::npos)
|
||||
return token;
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
/// 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;
|
||||
|
9
main.cpp
9
main.cpp
@@ -15,8 +15,7 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
|
||||
ArgsParser args(fakeArgs);
|
||||
//ArgsParser args(argc, argv);
|
||||
//args.consume_next(); // Consume file-name.
|
||||
|
||||
|
||||
if (args.has_flag("--help")) {
|
||||
|
||||
@@ -26,10 +25,16 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
//for (auto args.)
|
||||
|
||||
for (auto& arg : args.get_remaining_args()) {
|
||||
if (arg == "--color")
|
||||
}
|
||||
|
||||
|
||||
std::vector<std::string> color_codes = args.consume_flag_arg_multiple("--color");
|
||||
|
||||
|
||||
|
||||
|
||||
if (args.has_flag("--file")) {
|
||||
if (args.has_flag_arg("--file")) {
|
||||
auto filename = args.consume_flag_arg("--file");
|
||||
|
@@ -98,7 +98,9 @@ std::vector<std::string> ArgsParser::get_remaining_args() const {
|
||||
return args;
|
||||
}
|
||||
|
||||
std::vector<std::string> ArgsParser::get_unmatched() const { return get_remaining_args(); }
|
||||
bool ArgsParser::contains(const std::string &token) const {
|
||||
return std::ranges::find(args, token) != args.end();
|
||||
}
|
||||
|
||||
bool ArgsParser::has_flag_contains(const std::string &match) const {
|
||||
for (auto& token : args) {
|
||||
@@ -109,6 +111,15 @@ bool ArgsParser::has_flag_contains(const std::string &match) const {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::optional<std::string> ArgsParser::get_flag_contains(std::string &match) {
|
||||
for (auto& token : args) {
|
||||
if (token.find(match) != std::string::npos)
|
||||
return token;
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
bool ArgsParser::has_flag_starts_with(const std::string &value) const {
|
||||
for (auto& token : args) {
|
||||
if (token.starts_with(value))
|
||||
|
Reference in New Issue
Block a user