CPP-AP 2.7.0
Command-line argument parser for C++20
Loading...
Searching...
No Matches
argument_name.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 <cstdint>
10#include <format>
11#include <optional>
12#include <string>
13#include <string_view>
14
15namespace ap {
16
17namespace detail {
18
22 enum class match_type : std::uint8_t {
23 m_any,
24 m_primary,
26 };
27 using enum match_type;
28
29 argument_name() = delete;
30
33
34 argument_name(const argument_name&) = default;
36
44 std::optional<std::string> primary,
45 std::optional<std::string> secondary = std::nullopt,
46 std::optional<char> flag_char = std::nullopt
47 )
48 : primary(std::move(primary)),
49 secondary(std::move(secondary)),
50 flag_char(std::move(flag_char)) {
51 if (not (this->primary or this->secondary))
52 throw std::logic_error("An argument name cannot be empty! At least one of "
53 "primary/secondary must be specified");
54 }
55
57 ~argument_name() = default;
58
64 bool operator==(const argument_name& other) const noexcept {
65 return this->primary == other.primary and this->secondary == other.secondary;
66 }
67
74 [[nodiscard]] bool match(std::string_view arg_name, const match_type m_type = m_any)
75 const noexcept {
76 switch (m_type) {
77 case m_any:
78 return this->primary == arg_name or this->secondary == arg_name;
79 case m_primary:
80 return this->primary == arg_name;
81 case m_secondary:
82 return this->secondary == arg_name;
83 }
84
85 return false;
86 }
87
94 [[nodiscard]] bool match(
95 const argument_name& arg_name, [[maybe_unused]] const match_type m_type = m_any
96 ) const noexcept {
97 if (arg_name.primary and this->match(arg_name.primary.value()))
98 return true;
99
100 if (arg_name.secondary)
101 return this->match(arg_name.secondary.value());
102
103 return false;
104 }
105
110 [[nodiscard]] std::string str() const noexcept {
111 // if flag_char = nullopt, then the fallback character doesn't matter - the string will be empty
112 const std::string fc(this->flag_char.has_value(), this->flag_char.value_or(char()));
113
114 std::string primary_str =
115 this->primary ? std::format("{}{}{}", fc, fc, this->primary.value()) : "";
116 std::string separator = this->primary and this->secondary ? ", " : "";
117 std::string secondary_str =
118 this->secondary ? std::format("{}{}", fc, this->secondary.value()) : "";
119
120 return std::format("{}{}{}", primary_str, separator, secondary_str);
121 }
122
129 friend std::ostream& operator<<(std::ostream& os, const argument_name& arg_name) noexcept {
130 os << arg_name.str();
131 return os;
132 }
133
134 const std::optional<std::string> primary;
135 const std::optional<std::string> secondary;
136 const std::optional<char> flag_char;
137};
138
145 n_primary,
147};
148
149} // namespace detail
150
152
153} // namespace ap
argument_name_discriminator
Argument name member discriminator.
@ n_secondary
Represents the secondary name (used with a short flag prefix –).
@ n_primary
Represents the primary name (used with a long flag prefix –).
Definition utility.hpp:17
Structure holding the argument's name.
argument_name & operator=(const argument_name &)=delete
std::string str() const noexcept
Get a string representation of the argument_name.
bool match(const argument_name &arg_name, const match_type m_type=m_any) const noexcept
Matches the given argument name to the argument_name instance.
const std::optional< char > flag_char
The flag character (used for optional argument names).
bool operator==(const argument_name &other) const noexcept
Equality comparison operator.
argument_name(std::optional< std::string > primary, std::optional< std::string > secondary=std::nullopt, std::optional< char > flag_char=std::nullopt)
Primary and secondary name constructor.
const std::optional< std::string > secondary
The optional (short) name of the argument.
match_type
Specifies the type of argument name match.
@ m_any
Matches either the primary or the secondary name.
@ m_primary
Matches only the primary name.
@ m_secondary
Matches only the secondary name.
~argument_name()=default
Class destructor.
argument_name & operator=(argument_name &&)=delete
const std::optional< std::string > primary
The primary name of the argument.
argument_name(argument_name &&)=default
bool match(std::string_view arg_name, const match_type m_type=m_any) const noexcept
Matches the given string to the argument_name instance.
argument_name(const argument_name &)=default
friend std::ostream & operator<<(std::ostream &os, const argument_name &arg_name) noexcept
Stream insertion operator for argument names.