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