加入收藏 | 设为首页 | 会员中心 | 我要投稿 源码网 (https://www.900php.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Unix > 正文

使用getopt() 、getopt_long()、getopt_long_only()进行命令行处理

发布时间:2016-08-03 10:06:22 所属栏目:Unix 来源:站长网
导读:引言 在早期的 UNIX 中,其命令行环境(当时的唯一用户界面)包含着数十种小的文本处理工具。这些工具非常小,通常可很好地完成一项工作。这些工具通过较长的命

您已经为 --randomize 选项在 globalArgs 中添加了一个标志(请参见清单 12),并创建了 longOpts 数组来存储关于此程序支持的长选项的信息。除了 --randomize 外,所有的参数都与现有短选项对应(例如,--no-index 等同于 -I)。通过在选项结构中包含其短选项等效项,可以在不向程序添加任何其他代码的情况下处理等效的长选项。

清单 12. 扩展后的参数

struct globalArgs_t {

int noIndex;                /* -I option */

char *langCode;             /* -l option */

const char *outFileName;    /* -o option */

FILE *outFile;

int verbosity;              /* -v option */

char **inputFiles;          /* input files */

int numInputFiles;          /* # of input files */

int randomized;             /* --randomize option */

} globalArgs;

static const char *optString = "Il:o:vh?";

static const struct option longOpts[] = {

{ "no-index", no_argument, NULL, 'I' },

{ "language", required_argument, NULL, 'l' },

{ "output", required_argument, NULL, 'o' },

{ "verbose", no_argument, NULL, 'v' },

{ "randomize", no_argument, NULL, 0 },

{ "help", no_argument, NULL, 'h' },

{ NULL, no_argument, NULL, 0 }

};

(编辑:源码网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读