📄 parseoptions.cpp
字号:
#include "stdafx.h"#include "WarFtpdlite.h"#ifdef HAVE_GETOPT_H# include <getopt.h>#else# include "jgaa/getopt.h"#endifusing namespace std;extern WarPrioritiesDefE Priority;const priority_map_t PriorityMapTable[WAR_THRD_PRI_INVALID +1] = { {WAR_THRD_PRI_VERYHIGH, "realtime"}, {WAR_THRD_PRI_HIGH, "high"}, {WAR_THRD_PRI_NORMAL, "normal"}, {WAR_THRD_PRI_LOW, "low"}, {WAR_THRD_PRI_VERYLOW, "lower"}, {WAR_THRD_PRI_IDLE, "idle"}, {WAR_THRD_PRI_INVALID, NULL}};/* Parse the command-line arguments provided. */void ParseOptions(int argc, char* argv[]){ enum OptionsE { OPT_LICENSE = 255, OPT_NAME, OPT_PRIORITY, OPT_USERNAME, OPT_PASSWD, OPT_ROOTPATH, OPT_DBLOGOPT, }; static option options[] = { {"verbose", no_argument, &VerboseFlag, 1}, {"debug", optional_argument, &DebugFlag, 1}, {"debug-log-options", required_argument, NULL, OPT_DBLOGOPT}, {"version", no_argument, NULL, 'v'}, {"help", no_argument, NULL, 'h'}, {"license", no_argument, NULL, OPT_LICENSE}, {"daemon", no_argument, &DaemonFlag, 1}, {"profile", no_argument, &ProfilerFlag, 1}, {"not-daemon", no_argument, &DaemonFlag, -1}, {"install", no_argument, &InstallFlag, 1}, {"uninstall", no_argument, &InstallFlag, -1}, {"name", required_argument, NULL, OPT_NAME}, {"priority", required_argument, NULL, OPT_PRIORITY}, {"autostart", no_argument, &AutostartFlag, 1}, {"manual-start", no_argument, &AutostartFlag, -1}, {"user-name", required_argument, NULL, OPT_USERNAME}, {"password", optional_argument, NULL, OPT_PASSWD}, {"root-path", required_argument, NULL, OPT_ROOTPATH}, {NULL, 0, NULL, 0} }; int ch = 0; while((ch = getopt_long(argc, argv, "vh", options, NULL)) >= 0) { switch(ch) { case 'h': ShowHelp(); case 'v': cout << PROGRAM << " " << VERSION << endl; exit(1); case OPT_LICENSE: cout << PROGRAM << endl << COPYRIGHT << endl << LICENSE << endl; exit(1); case OPT_DBLOGOPT: DebugLogOptions = optarg; DebugFlag = 1; break; case OPT_NAME: ServiceName = optarg; { for(const char *p = ServiceName.c_str(); *p; p++) { if (!isalnum(*p)) { cerr << "Invalid service name: " << ServiceName << endl; exit(-1); } } } break; case OPT_PRIORITY: { for(priority_map_t *p = PriorityMapTable ; p->name ; p++) { if (!strcmp(p->name, optarg)) break; } if (p->name) Priority = p->priority; else { cerr << "Invalid priority: " << optarg << endl; exit(-1); } } break; case OPT_USERNAME: UserName = optarg; break; case OPT_PASSWD: if (optarg && *optarg) UserPasswd = optarg; else { cerr << "\nPassword: "; char buffer[124]; *buffer = 0; fgets(buffer, sizeof(buffer), stdin); for (char *p = buffer; *p; p++) { if ((*p == '\n') || (*p == '\r')) *p = 0; } if (*buffer) { cerr << "No password!" << endl; exit(-1); } UserPasswd = buffer; // Clear the password buffer for(p = buffer; p < buffer + sizeof(buffer); p++) *p = 0; } break; case OPT_ROOTPATH: RootPath = optarg; break; case '?': Usage(NULL, NULL); } } if (optind != argc) Usage(argv[optind], NULL); try { if (InstallFlag != 0) DoInstall(); } catch(WarException& e) { cerr << "Failed to install/uninstall. " << e.Explain() << endl; exit(-1); } try { if (AutostartFlag != 0) DoAutostart(); } catch(WarException& e) { cerr << "Failed to change startup mode. " << e.Explain() << endl; exit(-1); } if (ConfigFlag) exit(0); // Configuration only!}void Usage(char *bad_arg, char *explain) { if (bad_arg) cout << myname << ": " << "Unknown argument \"" << bad_arg << '\"' << endl; if (explain) cout << myname << ": " << explain << endl; cout << "Use: " << myname << " --help for help" << endl; exit(-1);}void ShowHelp(void) { cout << "Usage: " << myname << " [options]\n""\nInformational options:\n"" --verbose Show verbose information\n"" --profile Make a profiling report\n""-h --help Show this help\n""-v --version Show version\n""-d --debug Debug mode - extra verbose\n"" --debug-log-options=option[;option]... Verbose debug\n"" logging. The option names are listed\n"" in WarLogEventData.h\n"" --license Print license information\n""\nSystem options:\n"#ifdef WIN32" --daemon Run as a system service\n"#else" --daemon Run as a daemon\n"#endif" --not-daemon Starts the server as a normal application\n"" --user-name=name Name of the user account to run the server as\n"" --password[=pwd] Password for the user account above.\n""\nConfiguration options:\n"" --install Install the server. Creates missing files\n"" and configures the operating system to\n"" run the server. If used with the --deamon flag\n"" the server will start as a deamon when the\n"" machine start up.\n"" --uninstall Uninstalls the server. Changes made by --install\n"" that affects the operating system are reversed.\n"" --name=name Specify a unique one-word service-name for the\n"" service. Under Windows NT, this becomes the NT\n"" service name\n"" --root-path=path Specifies an alternate root-path for the server\n"" --priority=pri Specify priority. Can be: realtime, high, normal,\n"" low, verylow or idle.\n"" --autostart Instructs the system to start the daemon at boot-time.\n"" --manual-start Turns off automatic start.\n"""; exit(1);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -