tclap 1.6.0
CmdLineOutput.h
Go to the documentation of this file.
1// -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2
3/******************************************************************************
4 *
5 * file: CmdLineOutput.h
6 *
7 * Copyright (c) 2004, Michael E. Smoot
8 * Copyright (c) 2017, Google LLC
9 * All rights reserved.
10 *
11 * See the file COPYING in the top directory of this distribution for
12 * more information.
13 *
14 * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 *
22 *****************************************************************************/
23
24#ifndef TCLAP_CMD_LINE_OUTPUT_H
25#define TCLAP_CMD_LINE_OUTPUT_H
26
27#include <tclap/Arg.h>
28#include <tclap/ArgGroup.h>
29
30#include <algorithm>
31#include <cstddef>
32#include <iomanip>
33#include <iostream>
34#include <list>
35#include <string>
36#include <vector>
37
38namespace TCLAP {
39const char PATH_SEPARATOR =
40#ifdef _WIN32
41 '\\';
42#else
43 '/';
44#endif
45
47class ArgException;
48
53public:
57 virtual ~CmdLineOutput() = default;
58
63 virtual void usage(CmdLineInterface &c) = 0;
64
69 virtual void version(CmdLineInterface &c) = 0;
70
76 virtual void failure(CmdLineInterface &c, ArgException &e) = 0;
77};
78
79inline bool isInArgGroup(const Arg *arg, const std::list<ArgGroup *> &argSets) {
80 return std::ranges::any_of(argSets, [arg](const ArgGroup *group) {
81 return std::ranges::find(*group, arg) != group->end();
82 });
83}
84
85inline void removeArgsInArgGroups(std::list<Arg *> &argList,
86 const std::list<ArgGroup *> &argSets) {
87 std::erase_if(argList, [&argSets](const Arg *arg) {
88 return isInArgGroup(arg, argSets);
89 });
90}
91
92inline std::string removeSuffix(std::string s, const std::string &suffix) {
93 std::ptrdiff_t start = s.length() - suffix.length();
94 if (start >= 0 && s.substr(start) == suffix) {
95 return s.substr(0, start);
96 }
97
98 return s;
99}
100
101inline std::string basename(std::string s) {
102 size_t p = s.find_last_of(PATH_SEPARATOR);
103 if (p != std::string::npos) {
104 s.erase(0, p + 1);
105 }
106
107 return removeSuffix(s, ".exe");
108}
109
110} // namespace TCLAP
111
112#endif // TCLAP_CMD_LINE_OUTPUT_H
A simple class that defines and argument exception.
ArgGroup is the base class for implementing groups of arguments that are mutually exclusive (it repla...
Definition ArgGroup.h:42
iterator end() noexcept
Definition ArgGroup.h:113
A virtual base class that defines the essential data for all arguments.
Definition Arg.h:69
The base class that manages the command line definition and passes along the parsing to the appropria...
The interface that any output object must implement.
virtual ~CmdLineOutput()=default
Virtual destructor.
virtual void usage(CmdLineInterface &c)=0
Generates some sort of output for the USAGE.
virtual void failure(CmdLineInterface &c, ArgException &e)=0
Generates some sort of output for a failure.
virtual void version(CmdLineInterface &c)=0
Generates some sort of output for the version.
Definition Arg.h:54
bool isInArgGroup(const Arg *arg, const std::list< ArgGroup * > &argSets)
void removeArgsInArgGroups(std::list< Arg * > &argList, const std::list< ArgGroup * > &argSets)
const char PATH_SEPARATOR
std::string basename(std::string s)
std::string removeSuffix(std::string s, const std::string &suffix)