diff --git a/README.md b/README.md index 90c47dc..14bee6b 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,16 @@ # ArgsParser -A C++ library for parsing command arguments. \ No newline at end of file +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 + diff --git a/include/ArgsParser.hpp b/include/ArgsParser.hpp index a39249c..6663153 100644 --- a/include/ArgsParser.hpp +++ b/include/ArgsParser.hpp @@ -1,4 +1,5 @@ #pragma once +#include #include #include #include @@ -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& flags); @@ -68,22 +70,17 @@ public: std::optional consume_flag_arg(const std::initializer_list& flags); + /// @return all tokens that have not yet been matched and consumed by other methods. [[nodiscard]] std::vector get_remaining_args() const; - [[nodiscard]] std::vector 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 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 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; diff --git a/main.cpp b/main.cpp index 36b5f76..4e8cc1d 100644 --- a/main.cpp +++ b/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 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"); diff --git a/src/ArgsParser.cpp b/src/ArgsParser.cpp index a8883a9..10a4a94 100644 --- a/src/ArgsParser.cpp +++ b/src/ArgsParser.cpp @@ -98,7 +98,9 @@ std::vector ArgsParser::get_remaining_args() const { return args; } -std::vector 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 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))