linux使用getopt解析参数
getopt是linux下解析命令行参数的api。以linux内核代码的一个例子来说明:
{
int opt;
progname = basename(argv[0]);
while ((opt = getopt(argc, argv, "+lci:m:")) != -1) {
switch (opt) {
case 'l':
if (mode)
print_wrong_arg_exit();
mode = list;
break;
case 'i':
/* only allow -i with -m or no option */
if (mode && mode != show)
print_wrong_arg_exit();
interval = atoi(optarg);
break;
case 'm':
if (mode)
print_wrong_arg_exit();
mode = show;
show_monitors_param = optarg;
break;
case 'c':
wake_cpus = 1;
break;
default:
print_wrong_arg_exit();
}
}
if (!mode)
mode = show_all;
}
getopt仅支持"-v"类型的带参以及不带参数的命令行参数解析,包含"--v"一对短横线的复杂命令行参数请使用其他api。
函数原型
const char *optstring);
argc和argv都懂得,optstring告诉getopt需要哪些命令以及哪些命令需要参数,以"+lci:m:"为例,带"+"的参数不多见忽略掉,'l'和'c'是不带参数的命令选项,'i:'和'm:'是带参数的命令选项,两者通过':'进行区分。getopt获取命令以后参数保存在全局变量optarg中。
包括optarg在内,getopt使用三个全局变量来保存状态(该函数是线程非安全的,多线程使用需要自己加保护)。
optarg,保存命令的参数,如果有的话。
optind,下一次getopt时指向的argv指针的索引 。
optopt,最后一个已知选项。
optind用来获取除了命令行命令和参数以外的其他内容,比如命令行的最后添加一个默认参数配置文件。
上面的例子是一个直观的解析用法,再来一个推荐的解析方式。
switch (opt) {
/* enables "shutdown" command */
settings.shutdown_command = true;
break;
case 'a':
/* access for unix domain socket, as octal mask (like chmod)*/
settings.access= strtol(optarg,NULL,8);
break;
default:
break;
}
settings是一个入参结构体,对应入参的命令和参数
size_t maxbytes;
int maxconns;
int port;
int udpport;
char *inter;
int verbose;
rel_time_t oldest_live; /* ignore existing items older than this */
uint64_t oldest_cas; /* ignore existing items with CAS values lower than this */
int evict_to_free;
char *socketpath; /* path to unix socket if using local socket */
int access; /* access mask (a la chmod) for unix domain socket */
double factor; /* chunk size growth factor */
int chunk_size;
int num_threads; /* number of worker (without dispatcher) libevent threads to run */
int num_threads_per_udp; /* number of worker threads serving each udp socket */
char prefix_delimiter; /* character that marks a key prefix (for stats) */
int detail_enabled; /* nonzero if we're collecting detailed stats */
int reqs_per_event; /* Maximum number of io to process on each
io-event. */
bool use_cas;
enum protocol binding_protocol;
int backlog;
int item_size_max; /* Maximum item size, and upper end for slabs */
bool sasl; /* SASL on/off */
bool maxconns_fast; /* Whether or not to early close connections */
bool lru_crawler; /* Whether or not to enable the autocrawler thread */
bool lru_maintainer_thread; /* LRU maintainer background thread */
bool slab_reassign; /* Whether or not slab reassignment is allowed */
int slab_automove; /* Whether or not to automatically move slabs */
int hashpower_init; /* Starting hash power level */
bool shutdown_command; /* allow shutdown command */
int tail_repair_time; /* LRU tail refcount leak repair time */
bool flush_enabled; /* flush_all enabled */
char *hash_algorithm; /* Hash algorithm in use */
int lru_crawler_sleep; /* Microsecond sleep between items */
uint32_t lru_crawler_tocrawl; /* Number of items to crawl per run */
int hot_lru_pct; /* percentage of slab space for HOT_LRU */
int warm_lru_pct; /* percentage of slab space for WARM_LRU */
int crawls_persleep; /* Number of LRU crawls to run before sleeping */
bool expirezero_does_not_evict; /* exptime == 0 goes into NOEXP_LRU */
};
settings封装了可能有的各个命令及参数。把settings定义为全局变量可在整个程序访问。
linux使用getopt解析参数的更多相关文章
- 使用getopt 解析参数
getopt被用来解析命令行选项参数. #include <unistd.h> extern char *optarg; //选项的参数指针 extern int optind, //下一 ...
- 如何使用getopt()函数解析参数
最近在写程序的过程中,把一部分时间都花费在程序对参数的处理上.今天听了学长说到getopt函数,才发现原来c里面还有一个专门解决参数处理的函数,查询了相关资料,这里简单总结一下. 使用int main ...
- Python3+getopt解析命令行参数
一.说明 在学C语言的时候就知道可以通过argc获取命令行参数个数,可以通过argv获取具体参数.但自己写的程序获取到的参数一是没有键值形式二是写的参数不能乱序,和系统命令不太一样. 再往后点知道有g ...
- linux kernel的cmdline参数解析原理分析【转】
转自:https://blog.csdn.net/skyflying2012/article/details/41142801 版权声明:本文为博主kerneler辛苦原创,未经允许不得转载. htt ...
- 使用getopt_long来解析参数的小函数模板
getopt_long原型 #define no_argument 0 #define required_argument 1 #define optional_argument 2 struct o ...
- Linux Pthread 深入解析(转-度娘818)
Linux Pthread 深入解析 Outline - 1.线程特点 - 2.pthread创建 - 3.pthread终止 - 4.mutex互斥量使用框架 - ...
- Linux Command Line 解析
Linux Command Line 解析 0 处理模型 Linux kernel的启动包括很多组件的初始化和相关配置,这些配置参数一般是通过command line进行配置的.在进行后续分析之前,先 ...
- android和Linux下getopt的差别
1. Linux下如果找不到相对应的参数,则会跳过继续找下一个 Android下如果找不到则会直接返回-1,跳出来 2. Linux下通过getopt后会把找到的元素放到数组的前面,没找到的往后移动( ...
- 理解 Linux backlog/somaxconn 内核参数
https://jaminzhang.github.io/linux/understand-Linux-backlog-and-somaxconn-kernel-arguments/ 各参数的含义:h ...
随机推荐
- Asp.net正则获取html内容
1.获取div内容 string str = "tt<u>ss</u><div id=\"test\"><div>< ...
- PHP运行模式的深入理解
PHP运行模式有4钟:1)cgi 通用网关接口(Common Gateway Interface))2) fast-cgi 常驻 (long-live) 型的 CGI3) cli 命令行运行 ( ...
- Android-Universal-Image-Loader
基本以后都不用了,所以自己就不总结了 http://www.cnblogs.com/kissazi2/p/3886563.html http://www.cnblogs.com/kissazi2/p/ ...
- Java [Leetcode 232]Implement Queue using Stacks
题目描述: Implement the following operations of a queue using stacks. push(x) -- Push element x to the b ...
- 【C#学习笔记】类型转换
using System; namespace ConsoleApplication { class Program { static void Main(string[] args) { " ...
- 【Mysql】安装 mysql-5.7.5 指南
因为同学需要安装mysql,安装过程,一路百度,在这里记录一下步奏.以后还会用到. 1.mysql-5.7.5-m15-winx64.zip下载 官方网站下载地址:http://cdn.mysql.c ...
- Egret应用开发实践(01) Egret与WebPack
Egret Egret引擎是一款使用TypeScript语言构建的开源免费的移动游戏引擎.Egret仅是纯粹的使用TypeScript语言来开发游戏,开发后可以使用Egret来打包为HTML5网页游戏 ...
- .NET/ASP.NET Routing路由(深入解析路由系统架构原理)http://wangqingpei557.blog.51cto.com/1009349/1312422
阅读目录: 1.开篇介绍 2.ASP.NET Routing 路由对象模型的位置 3.ASP.NET Routing 路由对象模型的入口 4.ASP.NET Routing 路由对象模型的内部结构 4 ...
- MySQL基础之第9章 触发器
触发器(TRIGGER)是由事件来触发某个操作.这些事件包括INSERT语句.UPDATE语句和DELETE语句.当数据库系统执行这些事件时,就会激活触发器执行相应的操作.MySQL从5.0.2版本开 ...
- winfrom dataGridView 自定义分页实现
Winfrom 基本处于忘光的阶段.先需要做个winfrom 的软件.然后自己扩展了DataGridView带分页的控件.废话不多说 上图先 现在一步步实现其效果. 1.添加用户控件 上图即可知道 ...