author | František Kučera <franta-hg@frantovo.cz> |
Sat, 27 Nov 2021 23:15:15 +0100 | |
branch | v_0 |
changeset 12 | b291a7db2d39 |
parent 8 | b498160cc2ab |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2021 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, version 3 of the License. |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 |
*/ |
|
17 |
#pragma once |
|
18 |
||
19 |
#include <vector> |
|
20 |
#include <iostream> |
|
21 |
||
22 |
#include <relpipe/common/type/typedefs.h> |
|
23 |
#include <relpipe/cli/CLI.h> |
|
24 |
#include <relpipe/cli/RelpipeCLIException.h> |
|
25 |
||
26 |
#include "Configuration.h" |
|
27 |
||
28 |
namespace relpipe { |
|
29 |
namespace in { |
|
30 |
namespace x11 { |
|
31 |
||
32 |
class CLIParser { |
|
33 |
private: |
|
34 |
||
35 |
relpipe::common::type::StringX readNext(const std::vector<relpipe::common::type::StringX>& arguments, int& i) { |
|
36 |
if (i < arguments.size()) return arguments[i++]; |
|
37 |
else throw relpipe::cli::RelpipeCLIException(L"Missing CLI argument" + (i > 0 ? (L" after " + arguments[i - 1]) : L""), relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
38 |
} |
|
39 |
||
40 |
/** |
|
41 |
* TODO: use a common method |
|
42 |
*/ |
|
43 |
bool parseBoolean(const relpipe::common::type::StringX& value) { |
|
44 |
if (value == L"true") return true; |
|
45 |
else if (value == L"false") return false; |
|
46 |
else throw relpipe::cli::RelpipeCLIException(L"Unable to parse boolean value: " + value + L" (expecting true or false)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
|
47 |
} |
|
48 |
||
49 |
public: |
|
50 |
||
1
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
51 |
static const relpipe::common::type::StringX OPTION_LIST_INPUT_DEVICES; |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
52 |
static const relpipe::common::type::StringX OPTION_LIST_INPUT_EVENTS; |
7
039b3f8a3442
list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
53 |
static const relpipe::common::type::StringX OPTION_LIST_WINDOWS; |
8
b498160cc2ab
list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
54 |
static const relpipe::common::type::StringX OPTION_LIST_SCREENS; |
0 | 55 |
|
56 |
Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) { |
|
57 |
Configuration c; |
|
58 |
||
6
1b17b8cdbfc3
list input devices as default, only if no other option was specified
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
59 |
if (arguments.size() == 0) c.listInputDevices = true; |
1b17b8cdbfc3
list input devices as default, only if no other option was specified
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
60 |
|
0 | 61 |
for (int i = 0; i < arguments.size();) { |
62 |
relpipe::common::type::StringX option = readNext(arguments, i); |
|
63 |
||
1
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
64 |
if (option == OPTION_LIST_INPUT_DEVICES) { |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
65 |
c.listInputDevices = parseBoolean(readNext(arguments, i)); |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
66 |
} else if (option == OPTION_LIST_INPUT_EVENTS) { |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
67 |
c.listInputEvents = parseBoolean(readNext(arguments, i)); |
7
039b3f8a3442
list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
68 |
} else if (option == OPTION_LIST_WINDOWS) { |
039b3f8a3442
list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
69 |
c.listWindows = parseBoolean(readNext(arguments, i)); |
8
b498160cc2ab
list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
70 |
} else if (option == OPTION_LIST_SCREENS) { |
b498160cc2ab
list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
71 |
c.listScreens = parseBoolean(readNext(arguments, i)); |
0 | 72 |
} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); |
73 |
} |
|
74 |
||
75 |
return c; |
|
76 |
} |
|
77 |
||
78 |
virtual ~CLIParser() { |
|
79 |
} |
|
80 |
}; |
|
81 |
||
1
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
82 |
const relpipe::common::type::StringX CLIParser::OPTION_LIST_INPUT_DEVICES = L"--list-input-devices"; |
3bbf848b3565
Configuration + CLIParser + command for listing X11 devices
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
83 |
const relpipe::common::type::StringX CLIParser::OPTION_LIST_INPUT_EVENTS = L"--list-input-events"; |
7
039b3f8a3442
list windows: first version, --list-windows
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
84 |
const relpipe::common::type::StringX CLIParser::OPTION_LIST_WINDOWS = L"--list-windows"; |
8
b498160cc2ab
list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
85 |
const relpipe::common::type::StringX CLIParser::OPTION_LIST_SCREENS = L"--list-screens"; |
0 | 86 |
|
87 |
} |
|
88 |
} |
|
89 |
} |