CPP-AP 2.2.6
Command-line argument parser for C++20
Loading...
Searching...
No Matches
predefined_actions.hpp
Go to the documentation of this file.
1// Copyright (c) 2023-2025 Jakub MusiaƂ
2// This file is part of the CPP-AP project (https://github.com/SpectraL519/cpp-ap).
3// Licensed under the MIT License. See the LICENSE file in the project root for full license information.
4
6
7#pragma once
8
9#include "ap/exceptions.hpp"
10#include "detail/utility.hpp"
11
12#include <filesystem>
13#include <ostream>
14
15namespace ap {
16
17class argument_parser;
18std::ostream& operator<<(std::ostream& os, const argument_parser&) noexcept;
19
20namespace action {
21
28inline typename ap::action_type::on_flag::type print_config(
29 const argument_parser& parser,
30 const std::optional<int> exit_code = std::nullopt,
31 std::ostream& os = std::cout
32) noexcept {
33 return [&parser, &os, exit_code]() {
34 os << parser << std::endl;
35 if (exit_code)
36 std::exit(exit_code.value());
37 };
38}
39
42 return [](const std::string& file_path) {
43 if (not std::filesystem::exists(file_path))
44 throw std::filesystem::filesystem_error(
45 "File does not exists!",
46 file_path,
47 std::make_error_code(std::errc::no_such_file_or_directory)
48 );
49 };
50}
51
57template <ap::detail::c_arithmetic T>
59 return [lower_bound](const T& value) {
60 if (not (value > lower_bound))
61 throw std::out_of_range(
62 std::format("Value `{}` must be greater than `{}`!", value, lower_bound)
63 );
64 };
65}
66
72template <ap::detail::c_arithmetic T>
74 return [lower_bound](const T& value) {
75 if (! (value >= lower_bound))
76 throw std::out_of_range(
77 std::format("Value `{}` must be greater than or equal to `{}`!", value, lower_bound)
78 );
79 };
80}
81
87template <ap::detail::c_arithmetic T>
89 return [upper_bound](const T& value) {
90 if (! (value < upper_bound))
91 throw std::out_of_range(
92 std::format("Value `{}` must be less than `{}`!", value, upper_bound)
93 );
94 };
95}
96
102template <ap::detail::c_arithmetic T>
104 return [upper_bound](const T& value) {
105 if (! (value <= upper_bound))
106 throw std::out_of_range(
107 std::format("Value `{}` must be less than or equal to `{}`!", value, upper_bound)
108 );
109 };
110}
111
123template <ap::detail::c_arithmetic T, bool LeftInclusive = true, bool RightInclusive = true>
125 const T lower_bound, const T upper_bound
126) noexcept {
127 return [lower_bound, upper_bound](const T& value) {
128 constexpr char left_brace = LeftInclusive ? '[' : '(';
129 constexpr char right_brace = RightInclusive ? ']' : ')';
130
131 const bool is_valid =
132 (LeftInclusive ? value >= lower_bound : value > lower_bound)
133 and (RightInclusive ? value <= upper_bound : value < upper_bound);
134
135 if (not is_valid)
136 throw std::out_of_range(std::format(
137 "Value `{}` must be in interval {}{}, {}{}!",
138 value,
139 left_brace,
140 lower_bound,
141 upper_bound,
142 right_brace
143 ));
144 };
145}
146
147} // namespace action
148} // namespace ap
Main argument parser class.
detail::callable_type< ap::action_type::observe, T > leq(const T upper_bound) noexcept
Returns an observe action which checks if a parsed value is less than or equal to the given bound.
ap::action_type::on_flag::type print_config(const argument_parser &parser, const std::optional< int > exit_code=std::nullopt, std::ostream &os=std::cout) noexcept
Returns an on-flag action which prints the argument parser's configuration.
detail::callable_type< ap::action_type::observe, T > lt(const T upper_bound) noexcept
Returns an observe action which checks if a parsed value is less than the given bound.
std::ostream & operator<<(std::ostream &os, const argument_parser &) noexcept
detail::callable_type< ap::action_type::observe, T > gt(const T lower_bound) noexcept
Returns an observe action which checks if a parsed value is greater than the given bound.
detail::callable_type< ap::action_type::observe, T > within(const T lower_bound, const T upper_bound) noexcept
Returns an observe action which checks if a parsed value falls within the specified interval.
detail::callable_type< ap::action_type::observe, T > geq(const T lower_bound) noexcept
Returns an observe action which checks if a parsed value is greater than or equal to the given bound.
detail::callable_type< ap::action_type::observe, std::string > check_file_exists() noexcept
Returns an observe action which checks whether lower_bound file with the given name exists.
Defines general action-related utility.
typename AS::template type< T > callable_type
Template argument action callable type alias.
Definition utility.hpp:36