|
可以输入启动参数。不过我觉得optarg在getopt.h定义有点过于生硬。
虽说是C的标准做法,但是是否能传入一个指针进去更为好些?
虽说有C的写法,但是我还是比较喜欢一些C++的特性,于是一般这么用,在这里记录一下。以便以后查阅。- #include <getopt.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- //add by freeeyes
- //支持长命名,可以任意添加
- static struct option long_options[] = {
- { "help", no_argument, NULL, 'h' },
- { "version", required_argument, NULL, 'v' },
- { "index", required_argument, NULL, 'i' },
- { "run", required_argument, NULL, 'r' },
- { "args", required_argument, NULL, 'a' },
- { NULL, 0, NULL, 0 }
- };
- //短命名
- static char short_options[] = "hv:i:r:a:";
- struct _ProcessArgs
- {
- int nIndex;
- bool blIsRun;
- int nVersion;
- char szArgText[100];
-
- _ProcessArgs()
- {
- nIndex = 0;
- nVersion = 0;
- blIsRun = false;
- szArgText[0] = '\0';
- }
-
- void display()
- {
- printf("[display]nIndex=%d.\n", nIndex);
- if(blIsRun == true)
- {
- printf("[display]blIsRun is true.\n");
- }
- else
- {
- printf("[display]blIsRun is false.\n");
- }
- printf("[display]nVersion=%d.\n", nVersion);
- printf("[display]szArgText=%s.\n", szArgText);
- }
- };
- static void HelpCommand()
- {
- printf("[Help]**************************************.\n");
- printf("[Help] -h, --help : this help.\n");
- printf("[Help] -v, --version : this version.\n");
- printf("[Help] -i, --index : this index.\n");
- printf("[Help] -r, --run : this run.\n");
- printf("[Help] -a, --args : this args.\n");
- printf("[Help]**************************************.\n");
- }
- static int get_options(int argc, char **argv, struct _ProcessArgs *pProcessArgs)
- {
- int nRet = -1;
- for (;;)
- {
- nRet = getopt_long(argc, argv, short_options, long_options, NULL);
- if (nRet == -1)
- {
- //没有更多的命令了
- break;
- }
- switch (nRet)
- {
- case 'h':
- HelpCommand();
- break;
- case 'v':
- pProcessArgs->nVersion = atoi(optarg);
- break;
- case 'i':
- pProcessArgs->nIndex = atoi(optarg);
- break;
- case 'r':
- if(strcmp(optarg, "true") == 0)
- {
- pProcessArgs->blIsRun = true;
- }
- else
- {
- pProcessArgs->blIsRun = false;
- }
- break;
- case 'a':
- sprintf(pProcessArgs->szArgText, "%s", optarg);
- break;
- }
- }
- }
- int main(int argc, char **argv)
- {
- _ProcessArgs* pProcessArgs = new _ProcessArgs();
- get_options(argc, argv, pProcessArgs);
- pProcessArgs->display();
- delete pProcessArgs;
- return 0;
- }
复制代码 编译:
g++ -o Testarg Testarg.cpp
运行:
./Testarg -h
输出:
[Help]**************************************.
[Help] -h, --help : this help.
[Help] -v, --version : this version.
[Help] -i, --index : this index.
[Help] -r, --run : this run.
[Help] -a, --args : this args.
[Help]**************************************.
[display]nIndex=0.
[display]blIsRun is false.
[display]nVersion=0.
[display]szArgText=.
运行:
./Testarg -h -v 10 -i 11 -r true -a freeeyes
结果:
[Help]**************************************.
[Help] -h, --help : this help.
[Help] -v, --version : this version.
[Help] -i, --index : this index.
[Help] -r, --run : this run.
[Help] -a, --args : this args.
[Help]**************************************.
[display]nIndex=11.
[display]blIsRun is true.
[display]nVersion=10.
[display]szArgText=freeeyes.
|
|