/**
* 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>
#include <iostream>
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;
static const int EXIT_CODE_BAD_CLI_ARGUMENTS = 6;
/**
* std::cin and std::cout are "tied" by default i.e. reading from std::cin causes flush of the std::cout.
* Call this to "untie" them and have less write() calls (better buffering).
*/
static void untieStdIO() {
std::cin.tie(nullptr);
}
private:
int* argc;
char*** argv;
wstring program;
vector<wstring> args;
wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
};
}
}