int CwxAppFramework::parse(int argc, char** argv) { if (argc > 1) { CwxGetOpt cmd_option(argc, argv, ":f:hsr"); cmd_option.long_option("help", 'h', CwxGetOpt::NO_ARG); cmd_option.long_option("stop", 's', CwxGetOpt::NO_ARG); cmd_option.long_option("restart", 'r', CwxGetOpt::NO_ARG); cmd_option.long_option("file", 'f', CwxGetOpt::ARG_REQUIRED); int option; bool bHelp=false; while( (option = cmd_option.next()) != -1) { switch (option) { case 'f': if (!cmd_option.opt_arg() || (*cmd_option.opt_arg() == '-')) { printf("-f requires an argument.\n"); return -1; } this->m_strConfFile = cmd_option.opt_arg(); break; case 's': this->m_bCmdStop = true; break; case 'r': this->m_bCmdRestart = true; break; case 'h': bHelp = true; break; case ':': printf("%c requires an argument.\n", cmd_option.opt_opt ()); return -1; case '?': break; default: printf("Invalid arg %s.\n", argv[cmd_option.opt_ind()-1]); return -1; } if (bHelp) break; } if (-1 == option) { if (cmd_option.opt_ind() < argc) { printf("Invalid arg %s.\n", argv[cmd_option.opt_ind()]); return -1; } } if (bHelp) return 1; } return 0; }
///-1:失败;0:help;1:成功 int parseArg(int argc, char**argv) { ZkGetOpt cmd_option(argc, argv, "H:n:d:f:a:o:l:eshr"); int option; while( (option = cmd_option.next()) != -1) { switch (option) { case 'h': printf("create zookeeper node.\n"); printf("%s -H host:port -n node [-d data] [-f data file] [-o output file] [-a usr:passwd] [-l privilege]\n", argv[0]); printf("-H: zookeeper's host:port\n"); printf("-n: node name to create, it's full path.\n"); printf("-d: value for node.\n"); printf("-f: data's file. -d is used if it exists\n"); printf("-a: auth user's user:passwd. it can be multi.\n"); printf("-l: node's acl. it can be multi. it's value can be:\n"); printf(" all : any privilege for any user;\n"); printf(" self : any privilege for creator; \n"); printf(" read : read for any user;\n"); printf(" user:passwd:acrwd : digest auth for [user] with [passwd], \n"); printf(" admin(a), create(c), read(r), write(w), delete(d)\n"); printf("-o: output file, default is stdout\n"); printf("-e: node is EPHEMERAL node\n"); printf("-s: node is SEQUENCE node\n"); printf("-r: recursive to create child.\n"); printf("-h: help\n"); return 0; case 'H': if (!cmd_option.opt_arg() || (*cmd_option.opt_arg() == '-')) { printf("-H requires an argument.\n"); return -1; } g_strHost = cmd_option.opt_arg(); break; case 'n': if (!cmd_option.opt_arg() || (*cmd_option.opt_arg() == '-')) { printf("-n requires an argument.\n"); return -1; } g_strNode = cmd_option.opt_arg(); break; case 'd': if (!cmd_option.opt_arg() || (*cmd_option.opt_arg() == '-')) { printf("-d requires an argument.\n"); return -1; } g_strValue = cmd_option.opt_arg(); break; case 'f': if (!cmd_option.opt_arg() || (*cmd_option.opt_arg() == '-')) { printf("-f requires an argument.\n"); return -1; } g_strFile = cmd_option.opt_arg(); break; case 'a': if (!cmd_option.opt_arg() || (*cmd_option.opt_arg() == '-')) { printf("-a requires an argument.\n"); return -1; } g_auth.push_back(cmd_option.opt_arg()); break; case 'l': if (!cmd_option.opt_arg() || (*cmd_option.opt_arg() == '-')) { printf("-l requires an argument.\n"); return -1; } g_priv.push_back(cmd_option.opt_arg()); break; case 'o': if (!cmd_option.opt_arg() || (*cmd_option.opt_arg() == '-')) { printf("-o requires an argument.\n"); return -1; } g_strOut = cmd_option.opt_arg(); break; case 'e': g_ephemeral = true; break; case 's': g_sequence = true; break; case 'r': g_recursive = true; break; case ':': printf("%c requires an argument.\n", cmd_option.opt_opt ()); return -1; case '?': break; default: printf("Invalid arg %s.\n", argv[cmd_option.opt_ind()-1]); return -1; } } if (-1 == option) { if (cmd_option.opt_ind() < argc) { printf("Invalid arg %s.\n", argv[cmd_option.opt_ind()]); return -1; } } if (!g_strHost.length()) { printf("No host, set by -H\n"); return -1; } if (!g_strNode.length()) { printf("No node, set by -n\n"); return -1; } if (!g_strValue.length()) { if (g_strFile.length()) { if (!readFile(g_strFile, g_strValue)){ printf("Failure to read file:%s, errno=%d\n", g_strFile.c_str(), errno); return -1; } } } return 1; }
///-1£ºÊ§°Ü£»0£ºhelp£»1£º³É¹¦ int parseArg(int argc, char**argv) { ZkGetOpt cmd_option(argc, argv, "H:n:a:l:p:hm"); int option; while( (option = cmd_option.next()) != -1) { switch (option) { case 'h': printf("Test for lock zookeeper node.\n"); printf("%s -H host:port -n node [-d data] [-f data file] [-o output file] [-a usr:passwd] [-l privilege]\n", argv[0]); printf("-H: zookeeper's host:port\n"); printf("-n: node name for lock.\n"); printf("-p: prex for lock. default is [lock].\n"); printf("-a: auth user's user:passwd. it can be multi.\n"); printf("-l: node's acl. it can be multi. it's value can be:\n"); printf(" all : any privilege for any user;\n"); printf(" self : any privilege for creator; \n"); printf(" read : read for any user;\n"); printf(" user:passwd:acrwd : digest auth for [user] with [passwd], \n"); printf(" admin(a), create(c), read(r), write(w), delete(d)\n"); printf("-m: watch master node\n"); printf("-h: help\n"); return 0; case 'H': if (!cmd_option.opt_arg() || (*cmd_option.opt_arg() == '-')) { printf("-H requires an argument.\n"); return -1; } g_strHost = cmd_option.opt_arg(); break; case 'n': if (!cmd_option.opt_arg() || (*cmd_option.opt_arg() == '-')) { printf("-n requires an argument.\n"); return -1; } g_strNode = cmd_option.opt_arg(); break; case 'a': if (!cmd_option.opt_arg() || (*cmd_option.opt_arg() == '-')) { printf("-a requires an argument.\n"); return -1; } g_auth.push_back(cmd_option.opt_arg()); break; case 'l': if (!cmd_option.opt_arg() || (*cmd_option.opt_arg() == '-')) { printf("-l requires an argument.\n"); return -1; } g_priv.push_back(cmd_option.opt_arg()); break; case 'p': if (!cmd_option.opt_arg() || (*cmd_option.opt_arg() == '-')) { printf("-p requires an argument.\n"); return -1; } g_strPrev = cmd_option.opt_arg(); break; case 'm': g_bWatchMaster = true; break; case ':': printf("%c requires an argument.\n", cmd_option.opt_opt ()); return -1; case '?': break; default: printf("Invalid arg %s.\n", argv[cmd_option.opt_ind()-1]); return -1; } } if (-1 == option) { if (cmd_option.opt_ind() < argc) { printf("Invalid arg %s.\n", argv[cmd_option.opt_ind()]); return -1; } } if (!g_strHost.length()) { printf("No host, set by -H\n"); return -1; } if (!g_strNode.length()) { printf("No node, set by -n\n"); return -1; } return 1; }
///-1:失败;0:help;1:成功 int parseArg(int argc, char**argv) { ZkGetOpt cmd_option(argc, argv, "H:n:a:o:v:hr"); int option; while( (option = cmd_option.next()) != -1) { switch (option) { case 'h': printf("delete zookeeper node.\n"); printf("%s -H host:port -n node [-a usr:passwd] [-v verion] [-o output file] [-r]\n", argv[0]); printf("-H: zookeeper's host:port\n"); printf("-n: node name to create, it's full path.\n"); printf("-a: auth user's user:passwd. it can be multi.\n"); printf("-v: node's version\n"); printf("-o: output file, default is stdout\n"); printf("-r: recursive to delete child.\n"); printf("-h: help\n"); return 0; case 'H': if (!cmd_option.opt_arg() || (*cmd_option.opt_arg() == '-')) { printf("-H requires an argument.\n"); return -1; } g_strHost = cmd_option.opt_arg(); break; case 'n': if (!cmd_option.opt_arg() || (*cmd_option.opt_arg() == '-')) { printf("-n requires an argument.\n"); return -1; } g_strNode = cmd_option.opt_arg(); break; case 'a': if (!cmd_option.opt_arg() || (*cmd_option.opt_arg() == '-')) { printf("-a requires an argument.\n"); return -1; } g_auth.push_back(cmd_option.opt_arg()); break; case 'v': if (!cmd_option.opt_arg() || (*cmd_option.opt_arg() == '-')) { printf("-l requires an argument.\n"); return -1; } g_version = atoi(cmd_option.opt_arg()); break; case 'o': if (!cmd_option.opt_arg() || (*cmd_option.opt_arg() == '-')) { printf("-o requires an argument.\n"); return -1; } g_strOut = cmd_option.opt_arg(); break; case 'r': g_recursive = true; break; case ':': printf("%c requires an argument.\n", cmd_option.opt_opt ()); return -1; case '?': break; default: printf("Invalid arg %s.\n", argv[cmd_option.opt_ind()-1]); return -1; } } if (-1 == option) { if (cmd_option.opt_ind() < argc) { printf("Invalid arg %s.\n", argv[cmd_option.opt_ind()]); return -1; } } if (!g_strHost.length()) { printf("No host, set by -H\n"); return -1; } if (!g_strNode.length()) { printf("No node, set by -n\n"); return -1; } return 1; }
///-1:失败;0:help;1:成功 int parseArg(int argc, char**argv) { ZkGetOpt cmd_option(argc, argv, "H:n:d:f:a:o:l:v:h"); int option; while( (option = cmd_option.next()) != -1) { switch (option) { case 'h': printf("update zookeeper node.\n"); printf("%s -H host:port -n node [-d data] [-f data file] [-o output file] [-a usr:passwd] [-v version]\n", argv[0]); printf("-H: zookeeper's host:port\n"); printf("-n: node name to create, it's full path.\n"); printf("-d: value for node.\n"); printf("-f: data's file. -d is used if it exists\n"); printf("-a: auth user's user:passwd. it can be multi.\n"); printf("-o: output file, default is stdout\n"); printf("-v: node's version\n"); printf("-h: help\n"); return 0; case 'H': if (!cmd_option.opt_arg() || (*cmd_option.opt_arg() == '-')) { printf("-H requires an argument.\n"); return -1; } g_strHost = cmd_option.opt_arg(); break; case 'n': if (!cmd_option.opt_arg() || (*cmd_option.opt_arg() == '-')) { printf("-n requires an argument.\n"); return -1; } g_strNode = cmd_option.opt_arg(); break; case 'd': if (!cmd_option.opt_arg() || (*cmd_option.opt_arg() == '-')) { printf("-d requires an argument.\n"); return -1; } g_strValue = cmd_option.opt_arg(); break; case 'f': if (!cmd_option.opt_arg() || (*cmd_option.opt_arg() == '-')) { printf("-f requires an argument.\n"); return -1; } g_strFile = cmd_option.opt_arg(); break; case 'a': if (!cmd_option.opt_arg() || (*cmd_option.opt_arg() == '-')) { printf("-a requires an argument.\n"); return -1; } g_auth.push_back(cmd_option.opt_arg()); break; case 'v': if (!cmd_option.opt_arg() || (*cmd_option.opt_arg() == '-')) { printf("-v requires an argument.\n"); return -1; } g_version = atoi(cmd_option.opt_arg()); break; case 'o': if (!cmd_option.opt_arg() || (*cmd_option.opt_arg() == '-')) { printf("-o requires an argument.\n"); return -1; } g_strOut = cmd_option.opt_arg(); break; case ':': printf("%c requires an argument.\n", cmd_option.opt_opt ()); return -1; case '?': break; default: printf("Invalid arg %s.\n", argv[cmd_option.opt_ind()-1]); return -1; } } if (-1 == option) { if (cmd_option.opt_ind() < argc) { printf("Invalid arg %s.\n", argv[cmd_option.opt_ind()]); return -1; } } if (!g_strHost.length()) { printf("No host, set by -H\n"); return -1; } if (!g_strNode.length()) { printf("No node, set by -n\n"); return -1; } if (!g_strValue.length()) { if (g_strFile.length()) { if (!readFile(g_strFile, g_strValue)){ printf("Failure to read file:%s, errno=%d\n", g_strFile.c_str(), errno); return -1; } } } return 1; }