C处理命令行参数 getopt 用法
2019独角兽企业重金招聘Python工程师标准>>>
http://www.youtube.com/watch?v=SjyR74lbZOc 这个视频比较专业,可爱
http://www.youtube.com/watch?v=15nClbf4gAY 这个视频比较实在
https://www.cs.rutgers.edu/~pxk/416/notes/c-tutorials/getopt.html
http://www.ibm.com/developerworks/aix/library/au-unix-getopt.html
命令行的:http://wiki.bash-hackers.org/howto/getopts_tutorial
中文的:http://blog.sina.com.cn/s/blog_6aea878e0100stkd.html
Terminology(术语)
假设有程序
./order_pizza -t -d now anchovies pineapple
那么./order_pizza叫做name of the program
-t是第一个option, 不带参数 (表示是否为厚皮 thick crust)
-d now是第二个option, 表示delivery(派送时间), d是option的名称, now是d的参数.
anchovies和pineapple是第1个argument和第2个argument, 表示pizza的配料.
options 和 arguments的关系
P155 有句话很精典:
Command-line options are command-line arguments prefixed with “-”.
用法
前面已经给出了很多参考链接,讲的都很过细, 简单说一下,查看man 3 getopt可以得知,头文件unistd.h中有定义
int getopt(int argc, char* const argv[], const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
那么getopt每次返回一个option(包括其带的参数),option的名称保存在optopt中(char型), 选项值保存在optarg中 (char*类型)
第三个参数char *optstring形如x:y:z , 如果跟:表示这个选项带一个参数,比如这里的x, 和y, 如果跟两个::表示这个选项的参数是可选的
如果getopt碰到不认识的option,它返回?, 如果options部分已经结束了(没有以-开始的参数或碰到--), 它返回-1
optarg是option argument的缩写
optind是option index的缩写,表示下一个将要处理的参数的下标.
通过argc-=optind及 argv+=optind可以让程序在处理完options部分后直接定位到真正的arguments部分.
关于什么是options什么是arguments,前面的术语部分已经给出.
代码
#include <stdio.h>
#include <unistd.h>int main(int argc, char *argv[]){char* delivery = "";int thick = 0;int count = 0;char ch;while((ch = getopt(argc, argv, "d:t")) != EOF){switch(ch){case 'd':delivery = optarg;break;case 't':thick = 1;break;default:fprintf(stderr, "Unknown option: '%s'\n", optarg); return 1;}} argc -= optind;argv += optind;if(thick)puts("Thick crust.");if(delivery[0])printf("To be delivered %s.\n", delivery);puts("Ingredients:");for(count = 0; count < argc; count++){puts(argv[count]);}return 0;
}
编译,运行
gcc -o order_pizza 150.c
./order_pizza -t -d now Anchovies Pineapple
或
./order_pizza -td now Anchovies Pineapple
那么Anchovies之前的部分-t -d now 叫 options, 从Anchovies开始的参数叫 arguments,
now是option d的argument, 保存在全局变量char *optarg中,
什么时候使用--?
问:But what if I want to pass negative numbers as command-line arguments like set_temperature -c -4?
Won't it think that the 4 is an option, not an argument?
答: In order to avoid ambiguity, you can split your main arguments from the options using --.
So you would write set_temperature -c -- -4.
getopt() will stop reading options when it sees the --, so the rest of the line will be read as simple arguments.
生单词:anchovy (凤尾鱼)
pineapple 菠萝
参考:head first c
转载于:https://my.oschina.net/uniquejava/blog/263724
C处理命令行参数 getopt 用法相关推荐
- python命令行参数有什么用_一文看懂命令行参数的用法——Python中的getopt神器
参考原文: Python模块之命令行参数解析 - 每天进步一点点!!! - 博客园 https://www.cnblogs.com/madsnotes/articles/5687079.html py ...
- 函数 —— 分析命令行参数 getopt() getopt_long() getopt_long_only()
为什么需要命令行解析函数? 只按顺序处理参数的话,一些"可选参数"的功能将很难实现. 在Linux中,我们可以使用getopt.getopt_long.getopt_long_on ...
- python 命令行参数 argparse用法总结
目录 一.使用到的具体函数 二.程序用法说明 三.位置参数 positional arguments 四.可选参数 optional arguments 五.类型 六.可选值 七.互斥参数 八.参数默 ...
- C语言中使用库函数解析命令行参数
在编写需要命令行参数的C程序的时候,往往我们需要先解析命令行参数,然后根据这些参数来启动我们的程序. C的库函数中提供了两个函数可以用来帮助我们解析命令行参数:getopt.getopt_long. ...
- Linux进程-命令行参数和环境列表
命令行参数 在C中,main函数有很多的变种,比如 main(), int main(), int main(int argc, char *argv[]), int main(int argc, c ...
- 【Python】 配置解析ConfigParser 命令行参数解析optparser
ConfigParser ConfigParser包装了配置文件的读取和写入,使得python程序可以更加轻松操作配置文件了.这里的配置文件是指.ini的那种文件,基本格式如下 [section_a] ...
- python中config命令_【Python】 配置解析ConfigParser 命令行参数解析optparser
ConfigParser ConfigParser包装了配置文件的读取和写入,使得python程序可以更加轻松操作配置文件了.这里的配置文件是指.ini的那种文件,基本格式如下 [section_a] ...
- Shell命令 getopts/getopt用法详解 命令行参数
Shell命令行参数解析getopts/getopt用法详解 在Linux bash中,可以用以下三中方式解析命令行参数: 直接处理:使用$1.$2.$3- 进行解析 getopts:短选项的情况,例 ...
- python getopt argparse_python OptParse模块和argparse命令行解析的用法详解
https://www.cnblogs.com/wj-1314/p/8974021.html OptParse模块的简单介绍 Python 有两个内建的模块用于处理命令行参数: 一个是 getopt只 ...
最新文章
- 图灵奖公布:高性能计算先驱、为超算铺平道路的Jack Dongarra获奖
- 计算机网络·ip地址计算+ip地址配置+静态路由配置
- 训练深度神经网络的时候需要注意的一些小技巧
- TF之DD:利用Inception模型+GD算法生成更高质量的Deep Dream高质量图片
- cookie 跨域问题
- 机器学习 ML.NET 发布 1.0 RC
- linux内核I2C子系统学习(二)
- 如何安装dubbo的管理控制台
- 信息系统项目管理通关指南
- bzoj 1668: [Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富(DP)
- Ruby on Rails -跑起来吧hello World!
- 【.Net】win10 uwp unix timestamp 时间戳 转 DateTime
- Atitit.软件仪表盘(4)--db数据库子系统-监测
- fork和vfork,exec
- mysql数据库获得树的节点
- 蚂蚁庄园 php源码,求一个基于Auto.js的蚂蚁庄园脚本
- 支付宝、财付通、网银、百度钱包、京东钱包接口费率
- 昂达 v891 v1 终于 删除 windows 分区 并且恢复了容量。
- 网络冗余备份之浮动路由
- PostGIS搭建、空间数据库创建