C++ Utilities: application/commandlineutils.h Source File

C++ Utilities 5.33.0 Useful C++ classes and routines such as argument parser, IO and conversion utilities

Loading...
Searching...
No Matches
commandlineutils.h
Go to the documentation of this file.
1#ifndef APPLICATION_UTILITIES_COMMANDLINEUTILS_H
2#define APPLICATION_UTILITIES_COMMANDLINEUTILS_H
3
4#include "../global.h"
5
6#include <optional>
7#include <ostream>
8
9#ifdef PLATFORM_WINDOWS
10#include <memory>
11#include <vector>
12#endif
13
14namespace CppUtilities {
15
19 enum class Response { None, Yes, No };
20
21 CPP_UTILITIES_EXPORT bool confirmPrompt(const char *message, Response defaultResponse = Response::None);
22 CPP_UTILITIES_EXPORT std::optional<bool> isEnvVariableSet(const char *variableName);
23
24#ifdef PLATFORM_WINDOWS
25 CPP_UTILITIES_EXPORT bool handleVirtualTerminalProcessing();
26 CPP_UTILITIES_EXPORT void startConsole();
27 CPP_UTILITIES_EXPORT std::pair<std::vector<std::unique_ptr<char[]>>, std::vector<char *>> convertArgsToUtf8();
28#define CMD_UTILS_START_CONSOLE ::CppUtilities::startConsole();
29#define CMD_UTILS_CONVERT_ARGS_TO_UTF8 \
30 auto utf8Args = ::CppUtilities::convertArgsToUtf8(); \
31 argv = utf8Args.second.data(); \
32 argc = static_cast<int>(utf8Args.second.size());
33#define CMD_UTILS_HANDLE_VIRTUAL_TERMINAL_PROCESSING ::CppUtilities::handleVirtualTerminalProcessing();
34#else
35#define CMD_UTILS_START_CONSOLE
36#define CMD_UTILS_CONVERT_ARGS_TO_UTF8
37#define CMD_UTILS_HANDLE_VIRTUAL_TERMINAL_PROCESSING
38#endif
39
45 struct TerminalSize {
46 TerminalSize(unsigned short rows = 0, unsigned short columns = 0, unsigned short width = 0, unsigned short height = 0);
47
49 unsigned short rows;
51 unsigned short columns;
53 unsigned short width;
55 unsigned short height;
56};
57
58 inline TerminalSize::TerminalSize(unsigned short rows, unsigned short columns, unsigned short width, unsigned short height)
59 : rows(rows)
60 , columns(columns)
61 , width(width)
62 , height(height)
63{
64}
65
66 CPP_UTILITIES_EXPORT TerminalSize determineTerminalSize();
67
71 class CPP_UTILITIES_EXPORT Indentation {
72public:
73 Indentation(unsigned char level = 4, char character = ' ')
74 : level(level)
75 , character(character)
76 {
77 }
78
79 Indentation operator+(unsigned char level)
80 {
81 return Indentation(this->level + level, character);
82 }
83
84 unsigned char level;
85 char character;
86};
87
88 inline CPP_UTILITIES_EXPORT std::ostream &operator<<(std::ostream &out, Indentation indentation)
89{
90 for (unsigned char i = 0; i < indentation.level; ++i) {
91 out << indentation.character;
92 }
93 return out;
94}
95
96} // namespace CppUtilities
97
98#endif // APPLICATION_UTILITIES_COMMANDLINEUTILS_H
CppUtilities::Indentation
The Indentation class allows printing indentation conveniently, eg.
Definition commandlineutils.h:71
CppUtilities::Indentation::Indentation
Indentation(unsigned char level=4, char character=' ')
Definition commandlineutils.h:73
CppUtilities::Indentation::level
unsigned char level
Definition commandlineutils.h:84
CppUtilities::Indentation::character
char character
Definition commandlineutils.h:85
CppUtilities::Indentation::operator+
Indentation operator+(unsigned char level)
Definition commandlineutils.h:79
global.h
CPP_UTILITIES_EXPORT
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
Definition global.h:14
CppUtilities
Contains all utilities provided by the c++utilities library.
Definition argumentparser.h:18
CppUtilities::determineTerminalSize
CPP_UTILITIES_EXPORT TerminalSize determineTerminalSize()
Returns the current size of the terminal.
Definition commandlineutils.cpp:75
CppUtilities::operator<<
CPP_UTILITIES_EXPORT std::ostream & operator<<(std::ostream &out, Indentation indentation)
Definition commandlineutils.h:88
CppUtilities::Response
Response
The Response enum is used to specify the default response for the confirmPrompt() method.
Definition commandlineutils.h:19
CppUtilities::Response::None
@ None
Definition commandlineutils.h:19
CppUtilities::Response::Yes
@ Yes
Definition commandlineutils.h:19
CppUtilities::Response::No
@ No
Definition commandlineutils.h:19
CppUtilities::confirmPrompt
CPP_UTILITIES_EXPORT bool confirmPrompt(const char *message, Response defaultResponse=Response::None)
Prompts for confirmation displaying the specified message.
Definition commandlineutils.cpp:29
CppUtilities::ValueCompletionBehavior::None
@ None
Definition argumentparser.h:118
CppUtilities::isEnvVariableSet
CPP_UTILITIES_EXPORT std::optional< bool > isEnvVariableSet(const char *variableName)
Returns whether the specified env variable is set to a non-zero and non-white-space-only value.
Definition commandlineutils.cpp:53
CppUtilities::TerminalSize
The TerminalSize struct describes a terminal size.
Definition commandlineutils.h:45
CppUtilities::TerminalSize::width
unsigned short width
width in pixel
Definition commandlineutils.h:53
CppUtilities::TerminalSize::height
unsigned short height
height in pixel
Definition commandlineutils.h:55
CppUtilities::TerminalSize::TerminalSize
TerminalSize(unsigned short rows=0, unsigned short columns=0, unsigned short width=0, unsigned short height=0)
Definition commandlineutils.h:58
CppUtilities::TerminalSize::columns
unsigned short columns
number of columns
Definition commandlineutils.h:51
CppUtilities::TerminalSize::rows
unsigned short rows
number of rows
Definition commandlineutils.h:49
i
constexpr int i
Definition traitstests.cpp:106