C++ Utilities 5.16.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
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 <ostream>
7
8#ifdef PLATFORM_WINDOWS
9#include <memory>
10#include <vector>
11#endif
12
13namespace CppUtilities {
14
18enum class Response { None, Yes, No };
19
20CPP_UTILITIES_EXPORT bool confirmPrompt(const char *message, Response defaultResponse = Response::None);
21
22#ifdef PLATFORM_WINDOWS
23CPP_UTILITIES_EXPORT bool handleVirtualTerminalProcessing();
24CPP_UTILITIES_EXPORT void startConsole();
25CPP_UTILITIES_EXPORT std::pair<std::vector<std::unique_ptr<char[]>>, std::vector<char *>> convertArgsToUtf8();
26#define CMD_UTILS_START_CONSOLE ::CppUtilities::startConsole();
27#define CMD_UTILS_CONVERT_ARGS_TO_UTF8 \
28 auto utf8Args = ::CppUtilities::convertArgsToUtf8(); \
29 argv = utf8Args.second.data(); \
30 argc = static_cast<int>(utf8Args.second.size());
31#define CMD_UTILS_HANDLE_VIRTUAL_TERMINAL_PROCESSING ::CppUtilities::handleVirtualTerminalProcessing();
32#else
33#define CMD_UTILS_START_CONSOLE
34#define CMD_UTILS_CONVERT_ARGS_TO_UTF8
35#define CMD_UTILS_HANDLE_VIRTUAL_TERMINAL_PROCESSING
36#endif
37
44 TerminalSize(unsigned short rows = 0, unsigned short columns = 0, unsigned short width = 0, unsigned short height = 0);
45
47 unsigned short rows;
49 unsigned short columns;
51 unsigned short width;
53 unsigned short height;
54};
55
56inline TerminalSize::TerminalSize(unsigned short rows, unsigned short columns, unsigned short width, unsigned short height)
57 : rows(rows)
58 , columns(columns)
59 , width(width)
60 , height(height)
61{
62}
63
65
70public:
71 Indentation(unsigned char level = 4, char character = ' ')
72 : level(level)
73 , character(character)
74 {
75 }
76
77 Indentation operator+(unsigned char level)
78 {
79 return Indentation(this->level + level, character);
80 }
81
82 unsigned char level;
84};
85
86inline CPP_UTILITIES_EXPORT std::ostream &operator<<(std::ostream &out, Indentation indentation)
87{
88 for (unsigned char i = 0; i < indentation.level; ++i) {
89 out << indentation.character;
90 }
91 return out;
92}
93
94} // namespace CppUtilities
95
96#endif // APPLICATION_UTILITIES_COMMANDLINEUTILS_H
The Indentation class allows printing indentation conveniently, eg.
Indentation(unsigned char level=4, char character=' ')
Indentation operator+(unsigned char level)
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
Contains all utilities provides by the c++utilities library.
CPP_UTILITIES_EXPORT TerminalSize determineTerminalSize()
Returns the current size of the terminal.
CPP_UTILITIES_EXPORT std::ostream & operator<<(std::ostream &out, Indentation indentation)
Response
The Response enum is used to specify the default response for the confirmPrompt() method.
CPP_UTILITIES_EXPORT bool confirmPrompt(const char *message, Response defaultResponse=Response::None)
Prompts for confirmation displaying the specified message.
The TerminalSize struct describes a terminal size.
unsigned short width
width in pixel
unsigned short height
height in pixel
TerminalSize(unsigned short rows=0, unsigned short columns=0, unsigned short width=0, unsigned short height=0)
unsigned short columns
number of columns
unsigned short rows
number of rows
constexpr int i