tclap 1.4.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() {}
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 for (std::list<ArgGroup *>::const_iterator it = argSets.begin();
81 it != argSets.end(); ++it) {
82 if (std::find((*it)->begin(), (*it)->end(), arg) != (*it)->end()) {
83 return true;
84 }
85 }
86 return false;
87}
88
89inline void removeArgsInArgGroups(std::list<Arg *> &argList,
90 const std::list<ArgGroup *> &argSets) {
91 for (std::list<Arg *>::iterator it = argList.begin();
92 it != argList.end();) {
93 if (isInArgGroup(*it, argSets)) {
94 it = argList.erase(it);
95 } else {
96 ++it;
97 }
98 }
99}
100
101inline std::string removeSuffix(std::string s, const std::string &suffix) {
102 std::ptrdiff_t start = s.length() - suffix.length();
103 if (start >= 0 && s.substr(start) == suffix) {
104 return s.substr(0, start);
105 }
106
107 return s;
108}
109
110inline std::string basename(std::string s) {
111 size_t p = s.find_last_of(PATH_SEPARATOR);
112 if (p != std::string::npos) {
113 s.erase(0, p + 1);
114 }
115
116 return removeSuffix(s, ".exe");
117}
118
119} // namespace TCLAP
120
121#endif // TCLAP_CMD_LINE_OUTPUT_H
A simple class that defines and argument exception.
A virtual base class that defines the essential data for all arguments.
Definition Arg.h:66
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 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.
virtual ~CmdLineOutput()
Virtual destructor.
Definition Arg.h:52
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)