mirror of
https://github.com/sametersoylu/argument-parser.git
synced 2026-05-28 20:08:10 +00:00
Merge pull request #9 from sametersoylu/refaction
feat: add reference capabilities to positional arguments. use existing
This commit is contained in:
@@ -52,7 +52,7 @@ auto main() -> int {
|
|||||||
|
|
||||||
argument::start()
|
argument::start()
|
||||||
.positional("count")
|
.positional("count")
|
||||||
.position(0)
|
.position(1)
|
||||||
.help_text("How many times to repeat the action.")
|
.help_text("How many times to repeat the action.")
|
||||||
.action<int>([](int const& count) {
|
.action<int>([](int const& count) {
|
||||||
std::cout << "count action configured for " << count << '\n';
|
std::cout << "count action configured for " << count << '\n';
|
||||||
@@ -66,6 +66,13 @@ auto main() -> int {
|
|||||||
.reference(captured_value)
|
.reference(captured_value)
|
||||||
.build(parser);
|
.build(parser);
|
||||||
|
|
||||||
|
argument::start()
|
||||||
|
.positional("captured")
|
||||||
|
.position(0)
|
||||||
|
.help_text("Store the parsed value through a reference.")
|
||||||
|
.reference(captured_value)
|
||||||
|
.build(parser);
|
||||||
|
|
||||||
// parser.add_argument<int>({
|
// parser.add_argument<int>({
|
||||||
// {ShortArgument, "c"},
|
// {ShortArgument, "c"},
|
||||||
// {HelpText, "capture count"},
|
// {HelpText, "capture count"},
|
||||||
|
|||||||
@@ -368,12 +368,8 @@ private:
|
|||||||
throw std::logic_error("reference() was selected without a target.");
|
throw std::logic_error("reference() was selected without a target.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pairs[argument_parser::v2::flags::Reference] = target;
|
||||||
parser.template add_argument<store_type>(pairs);
|
parser.template add_argument<store_type>(pairs);
|
||||||
parser.on_complete([target, key](argument_parser::base_parser const& completed_parser) {
|
|
||||||
if (auto value = completed_parser.template get_optional<store_type>(key)) {
|
|
||||||
*target = value.value();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto build_parametered_action(argument_parser::v2::base_parser& parser) const -> void {
|
auto build_parametered_action(argument_parser::v2::base_parser& parser) const -> void {
|
||||||
|
|||||||
@@ -154,9 +154,13 @@ namespace argument_parser::v2 {
|
|||||||
found_params[extended_add_argument_flags::Action] = true;
|
found_params[extended_add_argument_flags::Action] = true;
|
||||||
if constexpr (!std::is_same_v<T, void>) {
|
if constexpr (!std::is_same_v<T, void>) {
|
||||||
auto ref = get_or_throw<T*>(argument_pairs.at(add_argument_flags::Reference), "reference");
|
auto ref = get_or_throw<T*>(argument_pairs.at(add_argument_flags::Reference), "reference");
|
||||||
action = helpers::make_parametered_action<T>([ref](T const& t) {
|
if (action) {
|
||||||
*ref = t;
|
throw std::logic_error("Cannot use both action and reference for the same argument");
|
||||||
}).clone();
|
} else {
|
||||||
|
action = helpers::make_parametered_action<T>([ref](T const& t) {
|
||||||
|
*ref = t;
|
||||||
|
}).clone();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
throw std::logic_error("Reference argument must not be void");
|
throw std::logic_error("Reference argument must not be void");
|
||||||
}
|
}
|
||||||
@@ -255,6 +259,26 @@ namespace argument_parser::v2 {
|
|||||||
position = get_or_throw<int>(argument_pairs.at(add_argument_flags::Position), "position");
|
position = get_or_throw<int>(argument_pairs.at(add_argument_flags::Position), "position");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (argument_pairs.find(add_argument_flags::Reference) != argument_pairs.end()) {
|
||||||
|
if (!IsTyped) {
|
||||||
|
throw std::logic_error("Reference argument must be typed");
|
||||||
|
}
|
||||||
|
|
||||||
|
if constexpr (!std::is_same_v<T, void>) {
|
||||||
|
auto ref = get_or_throw<T*>(argument_pairs.at(add_argument_flags::Reference), "reference");
|
||||||
|
if (action) {
|
||||||
|
throw std::logic_error("Cannot use both action and reference for the same argument");
|
||||||
|
} else {
|
||||||
|
action = helpers::make_parametered_action<T>([ref](T const& t) {
|
||||||
|
*ref = t;
|
||||||
|
}).clone();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw std::logic_error("Reference argument must not be void");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if (help_text.empty()) {
|
if (help_text.empty()) {
|
||||||
if constexpr (IsTyped) {
|
if constexpr (IsTyped) {
|
||||||
if constexpr (internal::sfinae::has_format_hint<parsing_traits::parser_trait<T>>::value &&
|
if constexpr (internal::sfinae::has_format_hint<parsing_traits::parser_trait<T>>::value &&
|
||||||
|
|||||||
Reference in New Issue
Block a user