CPP-AP 2.2.6
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) : _name(name) {}
34
36 [[nodiscard]] const ap::detail::argument_name& name() const noexcept {
37 return this->_name;
38 }
39
41 [[nodiscard]] const std::optional<std::string>& help() const noexcept {
42 return this->_help_msg;
43 }
44
50 virtual detail::argument_descriptor desc(const bool verbose, const char flag_char)
51 const noexcept = 0;
52
54 virtual bool is_required() const noexcept = 0;
55
57 virtual bool bypass_required_enabled() const noexcept = 0;
58
63 virtual bool mark_used() = 0;
64
66 virtual bool is_used() const noexcept = 0;
67
69 virtual std::size_t count() const noexcept = 0;
70
76 virtual bool set_value(const std::string& value) = 0;
77
79 virtual bool has_value() const noexcept = 0;
80
82 virtual bool has_parsed_values() const noexcept = 0;
83
85 virtual std::weak_ordering nvalues_ordering() const noexcept = 0;
86
88 virtual const std::any& value() const = 0;
89
91 virtual const std::vector<std::any>& values() const = 0;
92
93 const ap::detail::argument_name _name;
94 std::optional<std::string> _help_msg;
95};
96
103template <c_argument_value_type T>
104[[nodiscard]] bool is_valid_choice(const T& value, const std::vector<T>& choices) noexcept {
105 // TODO: replace with `std::ranges::contains` after transition to C++23
106 return choices.empty() or std::ranges::find(choices, value) != choices.end();
107}
108
109} // namespace detail
110} // 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
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.
virtual const std::any & value() const =0
const ap::detail::argument_name & name() const noexcept
virtual bool is_required() const noexcept=0
virtual detail::argument_descriptor desc(const bool verbose, const char flag_char) const noexcept=0
virtual const std::vector< std::any > & values() const =0
const std::optional< std::string > & help() const noexcept
virtual bool has_parsed_values() const noexcept=0
virtual bool mark_used()=0
Mark the argument as used.
virtual bool bypass_required_enabled() const noexcept=0
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:43
Structure holding the argument's name.