CPP-AP 2.7.0
Command-line argument parser for C++20
Loading...
Searching...
No Matches
argument_base.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
10#pragma once
11
13#include "argument_name.hpp"
14
15#include <any>
16#include <iostream>
17#include <vector>
18
19namespace ap {
20
21class argument_parser;
22
23namespace detail {
24
27public:
28 virtual ~argument_base() = default;
29
30 friend class ::ap::argument_parser;
31
32protected:
33 argument_base(const argument_name& name, const bool required = false)
34 : _name(name), _required(required) {}
35
37 [[nodiscard]] const ap::detail::argument_name& name() const noexcept {
38 return this->_name;
39 }
40
42 [[nodiscard]] const std::optional<std::string>& help() const noexcept {
43 return this->_help_msg;
44 }
45
47 [[nodiscard]] bool is_hidden() const noexcept {
48 return this->_hidden;
49 }
50
52 [[nodiscard]] bool is_required() const noexcept {
53 return this->_required;
54 }
55
60 [[nodiscard]] bool bypass_required_enabled() const noexcept {
61 return not this->_required and this->_bypass_required;
62 }
63
68 virtual detail::argument_descriptor desc(const bool verbose) const noexcept = 0;
69
74 virtual bool mark_used() = 0;
75
77 virtual bool is_used() const noexcept = 0;
78
80 virtual std::size_t count() const noexcept = 0;
81
87 virtual bool set_value(const std::string& value) = 0;
88
90 virtual bool has_value() const noexcept = 0;
91
93 virtual bool has_parsed_values() const noexcept = 0;
94
96 virtual std::weak_ordering nvalues_ordering() const noexcept = 0;
97
99 virtual const std::any& value() const = 0;
100
102 virtual const std::vector<std::any>& values() const = 0;
103
104 const ap::detail::argument_name _name;
105 std::optional<std::string> _help_msg;
106
107 bool _required : 1;
108 bool _bypass_required : 1 = false;
109 bool _hidden : 1 = false;
110};
111
119template <c_argument_value_type T>
120[[nodiscard]] bool is_valid_choice(const T& value, const std::vector<T>& choices) noexcept {
121 return choices.empty() or std::ranges::find(choices, value) != choices.end();
122}
123
124} // namespace detail
125} // namespace ap
Defines structures for formatting argument descriptions.
Argument class interface.
virtual bool has_value() const noexcept=0
virtual std::weak_ordering nvalues_ordering() const noexcept=0
const ap::detail::argument_name _name
bool is_required() const noexcept
virtual std::size_t count() const noexcept=0
virtual bool is_used() const noexcept=0
virtual bool set_value(const std::string &value)=0
Set the value for the argument.
bool is_hidden() const noexcept
virtual const std::any & value() const =0
const ap::detail::argument_name & name() const noexcept
virtual ~argument_base()=default
bool bypass_required_enabled() const noexcept
virtual const std::vector< std::any > & values() const =0
const std::optional< std::string > & help() const noexcept
virtual detail::argument_descriptor desc(const bool verbose) const noexcept=0
std::optional< std::string > _help_msg
virtual bool has_parsed_values() const noexcept=0
virtual bool mark_used()=0
Mark the argument as used.
argument_base(const argument_name &name, const bool required=false)
A structure used to represent an argument's description.
The concept is used to verify the validity of the arguments' value types.
Definition concepts.hpp:50
bool is_valid_choice(const T &value, const std::vector< T > &choices) noexcept
Checks if the provided choice is valid for the given set of choices.
Definition utility.hpp:17
Structure holding the argument's name.