CPP-AP 2.2.6
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 <format>
10#include <optional>
11#include <string>
12#include <string_view>
13
14namespace ap::detail {
15
18 argument_name() = delete;
19
20 argument_name& operator=(const argument_name&) = delete;
21 argument_name& operator=(argument_name&&) = delete;
22
23 argument_name(const argument_name&) = default;
24 argument_name(argument_name&&) = default;
25
30 argument_name(std::string_view primary) : primary(primary) {}
31
37 argument_name(std::string_view primary, std::string_view secondary)
39
41 ~argument_name() = default;
42
48 bool operator==(const argument_name& other) const noexcept {
49 if (not (this->secondary and other.secondary) and (this->secondary or other.secondary))
50 return false;
51
52 if (this->primary != other.primary)
53 return false;
54
55 return this->secondary ? this->secondary.value() == other.secondary.value() : true;
56 }
57
63 [[nodiscard]] bool match(std::string_view arg_name) const noexcept {
64 return arg_name == this->primary
65 or (this->secondary and arg_name == this->secondary.value());
66 }
67
73 [[nodiscard]] bool match(const argument_name& arg_name) const noexcept {
74 if (this->match(arg_name.primary))
75 return true;
76
77 if (arg_name.secondary)
78 return this->match(arg_name.secondary.value());
79
80 return false;
81 }
82
87 [[nodiscard]] std::string str(const std::optional<char> flag_char = std::nullopt)
88 const noexcept {
89 // if flag_char = nullopt, then the fallback character doesn't matter - the string will be empty
90 const std::string fc(flag_char.has_value(), flag_char.value_or(char()));
91 return this->secondary
92 ? std::format("{}{}{}, {}{}", fc, fc, this->primary, fc, this->secondary.value())
93 : std::format("{}{}{}", fc, fc, this->primary);
94 }
95
102 friend std::ostream& operator<<(std::ostream& os, const argument_name& arg_name) noexcept {
103 os << arg_name.str();
104 return os;
105 }
106
107 const std::string primary;
108 const std::optional<std::string> secondary;
109};
110
111} // namespace ap::detail
Structure holding the argument's name.
bool operator==(const argument_name &other) const noexcept
Equality comparison operator.
std::string str(const std::optional< char > flag_char=std::nullopt) const noexcept
Get a string representation of the argument_name.
bool match(const argument_name &arg_name) const noexcept
Matches the given argument name to the argument_name instance.
const std::optional< std::string > secondary
The optional (short) name of the argument.
argument_name(std::string_view primary, std::string_view secondary)
Primary and secondary name constructor.
~argument_name()=default
Class destructor.
const std::string primary
The primary name of the argument.
bool match(std::string_view arg_name) const noexcept
Matches the given string to the argument_name instance.
argument_name(std::string_view primary)
Primary name constructor.
friend std::ostream & operator<<(std::ostream &os, const argument_name &arg_name) noexcept
Stream insertion operator for argument names.