加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
commandline.h 2.38 KB
一键复制 编辑 原始数据 按行查看 历史
//
// Created by GPL on 2021/7/24.
//
#ifndef C___COMMANDLINE_H
#define C___COMMANDLINE_H
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
/**
* @brief: Commandline parsing
* command format `git {action} [option | args...]`
* commands need to be done:
* cmdline, add, rm, mv, commit, diff, hash, differ
*/
class Commandline {
public:
// argument classes
class Option{
public:
// long options, short options, aliased options
vector<string> options;
// kind of values
vector<string> values;
string help;
Option(vector<string> options, string help = ""):options(move(options)), help(help){};
Option(string option, string value, string help = "")
{
this->options.emplace_back(option);
this->values.emplace_back(value);
this->help = help;
}
Option(vector<string> options,string values, string help){
this->options = move(options);
this->values.emplace_back(values);
this->help = help;
};
};
class Argument{
public:
string action;
string description;
vector<Option> options;
Argument(string action, string description=""):action(action),description(description){};
};
void add_action(string action){
this->arguments.emplace_back(Argument(action));
};
void add_help(string action, string description) {
bool if_find = false;
for(vector<Argument>::iterator it = arguments.begin();it != arguments.end();it++)
{
if(it->action == action)
{
if_find = true;
it->description = description;
break;
}
}
if(not if_find)
{
cout << "[" << action << "]" << "is not registered, please call add_action first" << endl;
}
}
void add_option(string action, string short_opt, string long_opt, string opt_help="");
void parse(int argc, char* argv[]) ;
void printHelp(ostream &os = cout) const;
string get_action(void){
return action;
}
vector<string> get_option(void);
private:
vector<Argument> arguments;
string action;
// pair of option and arguments
vector<pair<string,string>> opt_args;
};
#endif //C___COMMANDLINE_H
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化