-
Notifications
You must be signed in to change notification settings - Fork 7
Introduce CliArgs (new design) and use to further generalize ThreeBusBasicJson example
#341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PhilipFackler
wants to merge
11
commits into
develop
Choose a base branch
from
PhilipFackler/threebusbasic-cliargs
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
39f00d5
Introduce CliArgs and use in ThreeBusBasicJson example
PhilipFackler f5ce6a6
Addressed formatting issues
PhilipFackler fed7a2b
Documentation; more consistent terms; hidden impl; improved usage
PhilipFackler 0c59de7
Restructured files and made CliArgs its own proper library target
PhilipFackler 27d7d5a
Added tests and fixed some issues
PhilipFackler b3ec8c0
Fixed formatting and added to CHANGELOG.md
PhilipFackler 675ae51
Fix compiler warnings
PhilipFackler db415de
Fixed stream issue; removed unnecessary helper functions
PhilipFackler d2c6d3e
Fixed libc++ errors and logic
PhilipFackler b9cc8f5
Reorganize, document, and fix some heres and theres
PhilipFackler 7f1b29f
Address more error cases
PhilipFackler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /** | ||
| * @file ArgValue.hpp | ||
| * @author Philip Fackler (facklerpw@ornl.gov) | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <any> | ||
| #include <sstream> | ||
| #include <string> | ||
|
|
||
| #include <GridKit/Utilities/String.hpp> | ||
|
|
||
| namespace GridKit | ||
| { | ||
| namespace Utilities | ||
| { | ||
|
|
||
| /** | ||
| * @brief Represents a single value of arbitrary type | ||
| * | ||
| * The value is internally represented as a std::string that is parsed when | ||
| * requested as a specific type | ||
| */ | ||
| class ArgValue | ||
| { | ||
| /** | ||
| * @brief Check that T != ArgValue | ||
| * | ||
| */ | ||
| template <typename T> | ||
| static constexpr bool notAnArgValue = | ||
| !std::is_same_v<std::remove_reference_t<T>, ArgValue>; | ||
|
|
||
| /** | ||
| * @brief Check that T != std::initializer_list<ArgValue> | ||
| */ | ||
| template <typename T> | ||
| static constexpr bool notAnArgValueList = | ||
| !std::is_same_v<std::remove_reference_t<T>, std::initializer_list<ArgValue>>; | ||
|
|
||
| /** | ||
| * @brief Check that T is not an ArgValue or list of ArgValues | ||
| */ | ||
| template <typename T> | ||
| static constexpr bool notArgValue = notAnArgValue<T> && notAnArgValueList<T>; | ||
|
|
||
| public: | ||
| /** | ||
| * @brief Default construction results in empty value | ||
| * | ||
| * @note the SFINAE parameter is used to work around an issue with libc++ | ||
| */ | ||
| ArgValue() = default; | ||
|
|
||
| /** | ||
| * @brief Copy constructor | ||
| */ | ||
| ArgValue(const ArgValue&) = default; | ||
|
|
||
| /** | ||
| * @brief Move constructor | ||
| */ | ||
| ArgValue(ArgValue&&) = default; | ||
|
|
||
| /** | ||
| * @brief Construct from any value | ||
| */ | ||
| template <typename T> | ||
| ArgValue(T&& val, std::enable_if_t<notArgValue<T>, int> = 0) | ||
| : value_((std::stringstream() << val).str()) | ||
| { | ||
| } | ||
|
|
||
| ArgValue& operator=(const ArgValue&) = default; | ||
| ArgValue& operator=(ArgValue&&) = default; | ||
|
|
||
| /** | ||
| * @brief Assign any value | ||
| */ | ||
| template <typename T> | ||
| ArgValue& operator=(T&& val) | ||
| { | ||
| value_ = (std::stringstream() << val).str(); | ||
| return *this; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Check if no value is contained | ||
| */ | ||
| bool empty() const | ||
| { | ||
| return value_.empty(); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Get string representation | ||
| */ | ||
| const std::string& get() const | ||
| { | ||
| return value_; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Get string representation | ||
| */ | ||
| const std::string& operator()() const | ||
| { | ||
| return get(); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Get value of specific expected type | ||
| */ | ||
| template <typename T> | ||
| T as() const | ||
| { | ||
| return GridKit::Utilities::parse<T>(value_); | ||
| } | ||
|
|
||
| private: | ||
| /// Internal representation | ||
| std::string value_{}; | ||
| }; | ||
|
|
||
| } // namespace Utilities | ||
| } // namespace GridKit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| /** | ||
| * @file ArgVector.hpp | ||
| * @author Philip Fackler (facklerpw@ornl.gov) | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <array> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include <GridKit/Utilities/CliArgs/ArgValue.hpp> | ||
|
|
||
| namespace GridKit | ||
| { | ||
| namespace Utilities | ||
| { | ||
|
|
||
| // Forward declaration | ||
| struct CliArgsImpl; | ||
|
|
||
| /** | ||
| * @brief Represents a set of 0 or more values associated with a given | ||
| * command-line option | ||
| * | ||
| * This class allows the internal data to be interpreted as a discrete set | ||
| * of values (std::array) of a given type or as a single value, hopefully | ||
| * making it more intuitive for users. For example... | ||
| * | ||
| * If an ArgVector v is expected to hold one value of type `double`, that | ||
| * value can be extracted with `auto r = v.as<double>();`. This means `r` | ||
| * will be a `double` value assigned with the contents of the first element | ||
| * of `v`. | ||
| * | ||
| * If an ArgVector v is expected to hold two values of type `int`, on the | ||
| * other hand, the values can be extracted as a `std::array<int, 2>` using | ||
| * `as<int, 2>()`. Using `std::array` allows structured bindings for the | ||
| * user: `auto [a, b] = v.as<int, 2>();` | ||
| */ | ||
| class ArgVector | ||
| { | ||
| public: | ||
| /** | ||
| * @brief Default is empty (no values) | ||
| */ | ||
| ArgVector() = default; | ||
|
|
||
| /** | ||
| * @brief Construct with a single value | ||
| */ | ||
| template <typename T> | ||
| ArgVector(T&& val) | ||
| : vec_{val} | ||
| { | ||
| } | ||
|
|
||
| ArgVector(std::initializer_list<ArgValue> vals) | ||
| : vec_{vals} | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * @brief Interpret as `N` values of type `T` | ||
| */ | ||
| template <typename T, std::size_t N> | ||
| std::array<T, N> as() const | ||
| { | ||
| assert(vec_.size() == N); | ||
| std::array<T, N> ret; | ||
| for (std::size_t i = 0; i < N; ++i) | ||
| { | ||
| ret[i] = vec_[i].as<T>(); | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Interpret as `N` values of type `std::string` | ||
| */ | ||
| template <std::size_t N> | ||
| decltype(auto) as() const | ||
| { | ||
| return as<std::string, N>(); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Interpret as single value of type `T` | ||
| */ | ||
| template <typename T> | ||
| decltype(auto) as() const | ||
| { | ||
| return vec_[0].as<T>(); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Interpret as single `std::string` value | ||
| */ | ||
| const std::string& operator()() const | ||
| { | ||
| return vec_[0].get(); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Get one of the contained `ArgValue` objects | ||
| */ | ||
| const ArgValue& operator[](std::size_t i) const | ||
| { | ||
| return vec_[i]; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Check for existence of values | ||
| */ | ||
| bool empty() const | ||
| { | ||
| return vec_.empty(); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Get number of `ArgValue` objects | ||
| */ | ||
| std::size_t size() const | ||
| { | ||
| return vec_.size(); | ||
| } | ||
|
|
||
| private: | ||
| /// Internal set of values | ||
| std::vector<ArgValue> vec_; | ||
|
|
||
| friend struct CliArgsImpl; | ||
| }; | ||
|
|
||
| } // namespace Utilities | ||
| } // namespace GridKit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| gridkit_add_library(utilities_cli_args | ||
| SOURCES | ||
| CliArgs.cpp | ||
| HEADERS | ||
| CliArgs.hpp | ||
| ArgValue.hpp | ||
| ArgVector.hpp | ||
| Option.hpp | ||
| LINK_LIBRARIES | ||
| PUBLIC GridKit::utilities_logger | ||
| ) | ||
|
|
||
| target_link_libraries(Utilities INTERFACE GridKit::utilities_cli_args) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| #include <algorithm> | ||
| #include <cassert> | ||
| #include <filesystem> | ||
| #include <iomanip> | ||
| #include <iostream> | ||
| #include <sstream> | ||
| #include <unordered_map> | ||
| #include <vector> | ||
|
|
||
| #include <GridKit/Utilities/CliArgs/CliArgs.hpp> | ||
| #include <GridKit/Utilities/Logger/Logger.hpp> | ||
|
|
||
| #include "CliArgsImpl.hpp" | ||
|
|
||
| namespace GridKit | ||
| { | ||
| namespace Utilities | ||
| { | ||
| CliArgs::CliArgs(std::initializer_list<Option> opts) | ||
| : pImpl_(std::make_unique<CliArgsImpl>(opts)) | ||
| { | ||
| } | ||
|
|
||
| CliArgs::~CliArgs() | ||
| { | ||
| } | ||
|
|
||
| std::ostream& operator<<(std::ostream& os, const CliArgs& args) | ||
| { | ||
| os << *(args.pImpl_); | ||
| return os; | ||
| } | ||
|
|
||
| void CliArgs::parseArgs(int argc, const char* argv[]) | ||
| { | ||
| pImpl_->parseArgs(argc, argv); | ||
| } | ||
|
|
||
| void CliArgs::printUsage(std::ostream& os) const | ||
| { | ||
| pImpl_->printUsage(os); | ||
| } | ||
|
|
||
| void CliArgs::printHelp(std::ostream& os) const | ||
| { | ||
| pImpl_->printHelp(os); | ||
| } | ||
|
|
||
| const std::string& CliArgs::getAppName() const | ||
| { | ||
| return pImpl_->app_name_; | ||
| } | ||
|
|
||
| const ArgVector& CliArgs::operator[](const std::string& name) const | ||
| { | ||
| return (*pImpl_)[name]; | ||
| } | ||
|
|
||
| } // namespace Utilities | ||
| } // namespace GridKit |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.