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;}



linux:

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);}


Example 2(using GNU long option variation)
#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
原创粉丝点击