include/relpipe/cli/CLI.h
author František Kučera <franta-hg@frantovo.cz>
Thu, 29 Nov 2018 22:30:11 +0100
branchv_0
changeset 6 114521cca04a
parent 2 117e4145fff8
child 7 95e647bf1922
permissions -rw-r--r--
license: GNU GPLv3+

/**
 * Relational pipes
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#pragma once

#include <locale.h>
#include <string>
#include <vector>
#include <sstream>
#include <locale>
#include <codecvt>

using namespace std;

namespace relpipe {
namespace cli {

/**
 * a common header-only library for CLI programs
 */
class CLI {
public:

	CLI(int argc, char* argv[]) {
		setlocale(LC_ALL, "");

		this->argc = &argc;
		this->argv = &argv;

		program = convertor.from_bytes(argv[0]);

		for (int i = 1; i < argc; i++) {
			args.insert(args.end(), convertor.from_bytes(argv[i]));
		}

	}

	CLI(const CLI& orig) {
	}

	virtual ~CLI() {
	}

	const wstring programName() {
		return (const wstring) program;
	}

	const vector<wstring> arguments() {
		return (const vector<wstring>)args;
	}

	static const int EXIT_CODE_SUCCESS = 0;
	static const int EXIT_CODE_UNEXPECTED_ERROR = 1;
	static const int EXIT_CODE_BAD_SYNTAX = 3;
	static const int EXIT_CODE_UNKNOWN_COMMAND = 4;
	static const int EXIT_CODE_DATA_ERROR = 5;

private:
	int* argc;
	char*** argv;
	wstring program;
	vector<wstring> args;
	wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
};

}
}