c++ windows下命令行一种解析方式,linux下命令行解析例子
来源:互联网 发布:js 展开折叠 section 编辑:程序博客网 时间:2024/05/21 17:45
windows:
#pragma once#include <map>#include <vector>#include <exception>using namespace std;class CmdArgs{private:int argc;char** argv;string spec;std::map<char, std::vector<string> >shortOpts;std::map<string, string>longOpts;std::vector<string> params;public:CmdArgs(void);~CmdArgs(void);private:/***@brief looks up spec for the given option*/bool isValidOption(char c) const;/***@brief looks up spec for the given option,return true if has args*/bool hasArgs(char c)const;/***@brief looks up spec for the given option*/bool hasOptionalArg(char c)const;public:/***@brief parse*/void parse(int argc, char *argv[], const char *spec);bool optionGiven(char c)const;std::map<string,string> getLongOption()const;};
#include "CmdArgs.h"CmdArgs::CmdArgs(void){argc = 0;argv = NULL;}CmdArgs::~CmdArgs(void){argc = 0;argv = NULL;}bool CmdArgs::isValidOption(char c) const{}bool CmdArgs::hasArgs(char c)const{const char *p = strchr(spec.c_str(), c);return p && (*(p+1)==':'||*(p+1)=='?');}bool CmdArgs::hasOptionalArg(char c)const{const char *p = strchr(spec.c_str(), c);return p && *(p+1)=='?';}void CmdArgs::parse(int argc, char *argv[], const char *spec){this->argc = argc;this->argv = argv;this->spec = spec;int i;for (i=1; i < argc; i++){if (strcmp(argv[i], "--") == 0){i++;break;}if (argv[i][0] =="-" &&argv[i][1]=='-'){const char *eqpos = strchr(argv[i], '=');if (eqpos){longOpts[string(argv[i]+2, eqpos-argv[i]-2)] = eqpos+1;}else if (i == argc-1){longOpts[argv[i]+2] = "";}else{longOpts[argv[i]+2] = argv[i+1];i++;}}else if(argv[i][0] == '-'){char c = argv[i][1];if (!isValidOption(c)){std::cout<<"Invalid arg option error !"<<std::endl;}}std::vector<string>&v = shortOpts[c];if (hasArgs(c)){if (argv[i][2]){v.push_back(argv[i]+2);}else if(i < argc-1){v.push_back(argv[++i]);}else if (!hasOptionalArg(c)){std::cout<<"No optional arg !"<<std::endl;}}else{break;}for (; i<argc; i++){params.push_back(argv[i]);}}}bool CmdArgs::optionGiven(char c)const{return shortOpts.find(c)!=shortOpts.end();}std::map<string,string> CmdArgs::getLongOption()const{return longOpts;}
Example 1(using getopt)
#include <stdio.h> /* for printf */#include <stdlib.h> /* for exit */#include <unistd.h> /* for getopt */int main (int argc, char **argv) { int c; int digit_optind = 0; int aopt = 0, bopt = 0; char *copt = 0, *dopt = 0; while ( (c = getopt(argc, argv, "abc:d:012")) != -1) { int this_option_optind = optind ? optind : 1; switch (c) { case '0': case '1': case '2': if (digit_optind != 0 && digit_optind != this_option_optind) printf ("digits occur in two different argv-elements.\n"); digit_optind = this_option_optind; printf ("option %c\n", c); break; case 'a': printf ("option a\n"); aopt = 1; break; case 'b': printf ("option b\n"); bopt = 1; break; case 'c': printf ("option c with value '%s'\n", optarg); copt = optarg; break; case 'd': printf ("option d with value '%s'\n", optarg); dopt = optarg; break; case '?': break; default: printf ("?? getopt returned character code 0%o ??\n", c); } } if (optind < argc) { printf ("non-option ARGV-elements: "); while (optind < argc) printf ("%s ", argv[optind++]); printf ("\n"); } exit (0);}
#include <stdio.h> /* for printf */#include <stdlib.h> /* for exit */#include <getopt.h> /* for getopt_long; standard getopt is in unistd.h */int main (int argc, char **argv) { int c; int digit_optind = 0; int aopt = 0, bopt = 0; char *copt = 0, *dopt = 0; static struct option long_options[] = { {"add", 1, 0, 0}, {"append", 0, 0, 0}, {"delete", 1, 0, 0}, {"verbose", 0, 0, 0}, {"create", 1, 0, 'c'}, {"file", 1, 0, 0}, {NULL, 0, NULL, 0} }; int option_index = 0; while ((c = getopt_long(argc, argv, "abc:d:012", long_options, &option_index)) != -1) { int this_option_optind = optind ? optind : 1; switch (c) { case 0: printf ("option %s", long_options[option_index].name); if (optarg) printf (" with arg %s", optarg); printf ("\n"); break; case '0': case '1': case '2': if (digit_optind != 0 && digit_optind != this_option_optind) printf ("digits occur in two different argv-elements.\n"); digit_optind = this_option_optind; printf ("option %c\n", c); break; case 'a': printf ("option a\n"); aopt = 1; break; case 'b': printf ("option b\n"); bopt = 1; break; case 'c': printf ("option c with value '%s'\n", optarg); copt = optarg; break; case 'd': printf ("option d with value '%s'\n", optarg); dopt = optarg; break; case '?': break; default: printf ("?? getopt returned character code 0%o ??\n", c); } } if (optind < argc) { printf ("non-option ARGV-elements: "); while (optind < argc) printf ("%s ", argv[optind++]); printf ("\n"); } exit (0);}
linux example reference:http://en.wikipedia.org/wiki/Getopt
linux source code can be found at: /glibc/posix/getopt.c :http://www.gnu.org/software/libc/
0 0
- c++ windows下命令行一种解析方式,linux下命令行解析例子
- linux c 命令行解析
- jq Linux下命令行json解析工具
- linux下命令行解析getopt函数
- 一种简单的linux命令行解析方法
- 命令行编译C++(Linux下、Windows下)
- c++:改造cmdline用于MSVC下的命令行参数解析
- jq-Linux命令行下解析json的命令
- 模拟Linux命令行方式在Windows下编译C/C++程序(使用VS编译器)
- windows 命令行解析
- c 解析命令行实现
- C#命令行解析工具
- 命令行下Android AVD命令解析
- 命令行下Android AVD命令解析
- 命令行下Android AVD命令解析
- Linux C解析控制台输入命令行参数
- linux解析命令行
- Linux 命令行参数解析
- NYOJ 286 动物统计
- 一举两得
- 终端运行sublime的方法
- oracle字符串连接及months_between函数
- 定期发送消息
- c++ windows下命令行一种解析方式,linux下命令行解析例子
- 一周极客热文:十步完全理解SQL
- 使用消息块筛选器
- Android获取内部和SDCard的存储空间
- StreamTokenizer
- OnOK()、OnCancel()()、OnClose()、OnDestroy()
- Android图片二进制与Bitmap、Drawable之间的转换
- oracle11g更改字符集AL32UTF8为ZHS16GBK
- SCRUM的七经八络