|
1 /** |
|
2 * cadMousePro |
|
3 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation, either version 3 of the License, or |
|
8 * (at your option) any later version. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 */ |
|
18 #pragma once |
|
19 |
|
20 #include <locale.h> |
|
21 #include <string> |
|
22 #include <vector> |
|
23 #include <sstream> |
|
24 #include <locale> |
|
25 #include <codecvt> |
|
26 #include <iostream> |
|
27 |
|
28 /** |
|
29 * a common header-only library for CLI programs |
|
30 */ |
|
31 class CLI { |
|
32 public: |
|
33 |
|
34 CLI(int argc, char* argv[]) { |
|
35 setlocale(LC_ALL, ""); |
|
36 |
|
37 this->argc = &argc; |
|
38 this->argv = &argv; |
|
39 |
|
40 program = convertor.from_bytes(argv[0]); |
|
41 |
|
42 for (int i = 1; i < argc; i++) { |
|
43 args.insert(args.end(), convertor.from_bytes(argv[i])); |
|
44 } |
|
45 |
|
46 } |
|
47 |
|
48 CLI(const CLI& orig) { |
|
49 } |
|
50 |
|
51 virtual ~CLI() { |
|
52 } |
|
53 |
|
54 const std::wstring programName() { |
|
55 return (const std::wstring) program; |
|
56 } |
|
57 |
|
58 const std::vector<std::wstring> arguments() { |
|
59 return (const std::vector<std::wstring>)args; |
|
60 } |
|
61 |
|
62 static const int EXIT_CODE_SUCCESS = 0; |
|
63 static const int EXIT_CODE_UNEXPECTED_ERROR = 1; |
|
64 static const int EXIT_CODE_BAD_CLI_ARGUMENTS = 6; |
|
65 |
|
66 /** |
|
67 * std::cin and std::cout are "tied" by default i.e. reading from std::cin causes flush of the std::cout. |
|
68 * Call this to "untie" them and have less write() calls (better buffering). |
|
69 */ |
|
70 static void untieStdIO() { |
|
71 std::cin.tie(nullptr); |
|
72 } |
|
73 |
|
74 private: |
|
75 int* argc; |
|
76 char*** argv; |
|
77 std::wstring program; |
|
78 std::vector<std::wstring> args; |
|
79 std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings. |
|
80 }; |