sptk2 logo
SPTK Home Page
CommandLine.h
1/*
2╔══════════════════════════════════════════════════════════════════════════════╗
3║ SIMPLY POWERFUL TOOLKIT (SPTK) ║
4╟──────────────────────────────────────────────────────────────────────────────╢
5║ copyright © 1999-2022 Alexey Parshin. All rights reserved. ║
6║ email alexeyp@gmail.com ║
7╚══════════════════════════════════════════════════════════════════════════════╝
8┌──────────────────────────────────────────────────────────────────────────────┐
9│ This library is free software; you can redistribute it and/or modify it │
10│ under the terms of the GNU Library General Public License as published by │
11│ the Free Software Foundation; either version 2 of the License, or (at your │
12│ option) any later version. │
13│ │
14│ This library is distributed in the hope that it will be useful, but │
15│ WITHOUT ANY WARRANTY; without even the implied warranty of │
16│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library │
17│ General Public License for more details. │
18│ │
19│ You should have received a copy of the GNU Library General Public License │
20│ along with this library; if not, write to the Free Software Foundation, │
21│ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. │
22│ │
23│ Please report all bugs and problems to alexeyp@gmail.com. │
24└──────────────────────────────────────────────────────────────────────────────┘
25*/
26
27#pragma once
28
29#include <iostream>
30#include <list>
31#include <map>
32#include <set>
33#include <sptk5/RegularExpression.h>
34#include <sptk5/cutils>
35
36namespace sptk {
37
46class SP_EXPORT CommandLine
47{
48public:
56 class SP_EXPORT Visibility
57 {
58 public:
64 Visibility(const String& pattern, bool mustMatch = true);
65
69 bool any() const;
70
74 bool matches(const String& command) const;
75
76 private:
77 bool m_inverted;
78 std::shared_ptr<RegularExpression> m_regexp;
79 };
80
86 static void printLine(const String& ch, size_t count);
87
96 void printCommands(const String& onlyForCommand, size_t screenColumns, size_t nameColumns,
97 const Strings& sortedCommands, size_t helpTextColumns) const;
98
107 void printOptions(const String& onlyForCommand, size_t screenColumns, size_t nameColumns,
108 const Strings& sortedOptions, size_t helpTextColumns) const;
109
116 CommandLine(const String& programVersion, const String& description, const String& commandLinePrototype);
117
128 void defineOption(const String& fullName, const String& shortName, const Visibility& useForCommands, const String& help);
129
140 void defineParameter(const String& fullName, const String& shortName, const String& valueName,
141 const String& validateValue,
142 const Visibility& useForCommands, const String& defaultValue, const String& help);
143
149 void defineArgument(const String& fullName, const String& helpText);
150
158 void init(size_t argc, const char** argv);
159
164 String getOptionValue(const String& name) const;
165
170 bool hasOption(const String& name) const;
171
177 void setOptionValue(const String& name, const String& value = "yes");
178
182 const Strings& arguments() const;
183
188 void printHelp(size_t screenColumns) const;
189
195 void printHelp(const String& onlyForCommand, size_t screenColumns) const;
196
200 void printVersion() const;
201
208 static Strings preprocessArguments(const std::vector<const char*>& argv);
209
215 static Strings rewriteArguments(const Strings& arguments);
216
220 class SP_EXPORT CommandLineElement
221 {
222 public:
226 enum class Type
227 {
228 IS_UNKNOWN,
229 IS_COMMAND,
230 IS_OPTION,
231 IS_VALUE_OPTION
232 };
233
241 CommandLineElement(const String& name, const String& shortName, const String& help,
242 const Visibility& useWithCommands);
243
247 virtual ~CommandLineElement() = default;
248
252 virtual Type type() const;
253
257 virtual String name() const;
258
262 virtual String shortName() const;
263
267 virtual bool hasValue() const;
268
275 virtual void validate(const String& value) const;
276
280 virtual String printableName() const;
281
285 bool useWithCommand(const String& command) const;
286
292 void formatHelp(size_t textWidth, Strings& formattedText) const;
293
301 void printHelp(size_t nameWidth, size_t textWidth, const String& optionDefaultValue) const;
302
303 private:
307 String m_name;
308
312 String m_shortName;
313
317 String m_help;
318
322 Visibility m_useWithCommands;
323 };
324
331 class SP_EXPORT CommandLineArgument
332 : public CommandLineElement
333 {
334 public:
340 CommandLineArgument(const String& name, const String& help);
341
345 ~CommandLineArgument() override = default;
346 };
347
354 class SP_EXPORT CommandLineOption
355 : public CommandLineElement
356 {
357 public:
365 CommandLineOption(const String& name, const String& shortName, const Visibility& useWithCommands,
366 const String& help);
367
371 ~CommandLineOption() override = default;
372
376 bool hasValue() const override;
377
382
386 String printableName() const override;
387 };
388
397 class SP_EXPORT CommandLineParameter
398 : public CommandLineElement
399 {
400 public:
410 CommandLineParameter(const String& name, const String& shortName, const String& valueName,
411 const String& validateValue,
412 const Visibility& useWithCommands, const String& help);
413
417 String printableName() const override;
418
423 void validate(const String& value) const override;
424
428 bool hasValue() const override;
429
433 Type type() const override;
434
435 private:
436 String m_valueInfo;
437 std::shared_ptr<RegularExpression> m_validateValue;
438 };
439
440private:
441 String m_programVersion;
442 String m_description;
443 String m_commandLinePrototype;
444 std::map<String, std::shared_ptr<CommandLineElement>> m_optionTemplates;
445
449 std::map<String, std::shared_ptr<CommandLineArgument>> m_argumentTemplates;
450 std::map<String, String> m_values;
451 Strings m_arguments;
452 fs::path m_executablePath;
453 std::list<std::shared_ptr<CommandLineElement>> m_allElements;
454
455 static String preprocessArgument(String& arg, String& quote, String& quotedString);
456
457 void readOption(const Strings& digestedArgs, size_t& i);
458};
459
463} // namespace sptk
Definition: CommandLine.h:333
CommandLineArgument(const String &name, const String &help)
Definition: CommandLine.h:221
bool useWithCommand(const String &command) const
virtual void validate(const String &value) const
Type
Definition: CommandLine.h:227
virtual String shortName() const
void printHelp(size_t nameWidth, size_t textWidth, const String &optionDefaultValue) const
virtual String printableName() const
CommandLineElement(const String &name, const String &shortName, const String &help, const Visibility &useWithCommands)
void formatHelp(size_t textWidth, Strings &formattedText) const
Definition: CommandLine.h:356
CommandLineElement::Type type() const override
String printableName() const override
CommandLineOption(const String &name, const String &shortName, const Visibility &useWithCommands, const String &help)
Definition: CommandLine.h:399
void validate(const String &value) const override
CommandLineParameter(const String &name, const String &shortName, const String &valueName, const String &validateValue, const Visibility &useWithCommands, const String &help)
String printableName() const override
Definition: CommandLine.h:57
bool matches(const String &command) const
Visibility(const String &pattern, bool mustMatch=true)
Definition: CommandLine.h:47
static void printLine(const String &ch, size_t count)
bool hasOption(const String &name) const
static Strings preprocessArguments(const std::vector< const char * > &argv)
void init(size_t argc, const char **argv)
void defineOption(const String &fullName, const String &shortName, const Visibility &useForCommands, const String &help)
CommandLine(const String &programVersion, const String &description, const String &commandLinePrototype)
void printHelp(const String &onlyForCommand, size_t screenColumns) const
String getOptionValue(const String &name) const
const Strings & arguments() const
void printCommands(const String &onlyForCommand, size_t screenColumns, size_t nameColumns, const Strings &sortedCommands, size_t helpTextColumns) const
void defineParameter(const String &fullName, const String &shortName, const String &valueName, const String &validateValue, const Visibility &useForCommands, const String &defaultValue, const String &help)
void setOptionValue(const String &name, const String &value="yes")
void printOptions(const String &onlyForCommand, size_t screenColumns, size_t nameColumns, const Strings &sortedOptions, size_t helpTextColumns) const
static Strings rewriteArguments(const Strings &arguments)
void printVersion() const
void defineArgument(const String &fullName, const String &helpText)
void printHelp(size_t screenColumns) const
Definition: String.h:49
Definition: Strings.h:52

Fri Oct 14 2022 09:58:31: SPTK 5.4.1