getopt详解
来源:互联网 发布:苏州天魂网络怎么样 编辑:程序博客网 时间:2024/05/16 12:48
#include <unistd.h>#include <stdio.h>#include <stdlib.h>int main(int argc, char * argv[]){int opt;while ((opt = getopt(argc, argv, ":if:lr")) != -1){switch (opt){case 'i':case 'l':case 'r':printf("option: %c\n", opt);break;case 'f':printf("filename: %s\n", optarg);break;case ':':printf("option needs a value\n");break;case '?':printf("unknown option: %c\n", optopt);break;}}for (; optind < argc; optind++){printf("argument: %s\n", argv[optind]);}exit(0);}
解释:
getopt第三个参数":if:lr"解释:
第一个字符':'说明,如果带参数的选项后面没有参数,那么getopt返回':',如果没有第一个字符':',那么getopt返回'?'
i,l,r表明这是程序的选项,而且没有参数
f:表明这是一个带参数的程序选项
case中的字符解释:
':'是带参数的选项没有带参数
'?'是有未知的选项
一些变量解释:
optarg是选项后面带的参数
optopt是未知参数
optind是当前读的形参位置,这个变量可用于读取选项之外的形参
getopt函数的特殊作用:
getopt会把argv进行重新排序,选项以及选项所带参数放到前面,选项之外的形参放到最后,这样optind才能起作用
- getopt 详解
- getopt详解
- getopt详解
- getopt 详解 getopt与optarg
- getopt 详解 getopt与optarg
- getopt 详解 linux
- getopt 详解 linux
- getopt函数详解
- getopt 详解 linux
- getopt 详解 linux
- getopt命令详解
- getopt 详解 linux
- getopt()用法详解
- linux getopt详解
- getopt、getopt_long、getopt_long_only详解
- getopt,getopt_long函数详解
- getopt() 与 getopt_long() 详解
- getopt、getopt_log详解
- "Hello world”是什么歌曲?
- 05-css实现图文混排(页面效果)
- 键盘小游戏
- c# 对象生命周期
- Ubuntu 12.04 root用户登录设置
- getopt详解
- javascript 的几种写法
- pb小技巧
- 学校管理系统
- 使用SqlDependency时提示当前数据库未启用Service broker
- 让工程师爱上CMMI,实现管理于无形 --- 中标软件CMMI L5之路 (1/2)
- 让工程师爱上CMM,实现管理于无形 --- 中标软件CMMI L5之路 (2/2)
- 键盘字母游戏
- Mysql常用命令