src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Mon, 05 Apr 2021 18:09:25 +0200
branchv_0
changeset 8 b498160cc2ab
parent 7 039b3f8a3442
permissions -rw-r--r--
list screens: --list-screens + support multiple screens in --list-windows and --list-input-events
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <vector>
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <iostream>
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <relpipe/common/type/typedefs.h>
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/cli/CLI.h>
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <relpipe/cli/RelpipeCLIException.h>
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include "Configuration.h"
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace relpipe {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace in {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
namespace x11 {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
class CLIParser {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
private:
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
	relpipe::common::type::StringX readNext(const std::vector<relpipe::common::type::StringX>& arguments, int& i) {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
		if (i < arguments.size()) return arguments[i++];
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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);
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	}
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	/**
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	 * TODO: use a common method
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	 */
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	bool parseBoolean(const relpipe::common::type::StringX& value) {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
		if (value == L"true") return true;
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
		else if (value == L"false") return false;
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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);
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	}
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
public:
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
		Configuration c;
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		for (int i = 0; i < arguments.size();) {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
			relpipe::common::type::StringX option = readNext(arguments, i);
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
		}
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
		return c;
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
	}
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
	virtual ~CLIParser() {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
	}
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
};
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
}
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
}
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
}