getopt是linux下获取程序启动参数的函数

       #include <unistd.h>

int getopt(int argc, char * const argv[],
                  const char *optstring);

extern char *optarg;
       extern int optind, opterr, optopt;

使用了全局参数optarg, optind,opterr, optopt这几个,其中optarg用来保存解析出来的选项参数
下面是一个例子
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
using namespace std;

int main(int argc, char** argv)
{
    int opt = 0;

while(-1  !=  (opt = getopt(argc, argv, "n:t:kl")))
    {
        switch(opt)
        {
            case 'n':
                fprintf(stdout, "n:%s\n", optarg);
            break;
            case 't':
                fprintf(stdout, "t:%s\n", optarg);
            break;
            case 'k':
                fprintf(stdout, "k option get\n");
            break;
            case 'l':
                fprintf(stdout, "l option get\n");
            break;
        }
    }

return 0;
}

这段代码意图解析带有参数和不带有参数的命令选项,其中n和t和带有参数的,k和l是不带有参数的,getopt通过识别第三个参数的格式来区分,n和t后面有冒号,表示选项带有参数,k和l没有。
测试一下这段代码对符合要求的命令行的解析和不符合要求的命令行的解析
smi /project/bm3.5/smi/test/zd/self>tg   -n 100 -t  vi  -k 30  -l 80
n:100
t:vi
k option get
l option get
smi /project/bm3.5/smi/test/zd/self>tg   -n 100 -t  vi  -k   -l
n:100
t:vi
k option get
l option get
smi /project/bm3.5/smi/test/zd/self>tg   -n 100 -t   -k   -l
n:100
t:-k
l option get
smi /project/bm3.5/smi/test/zd/self>tg -n
tg: option requires an argument -- n
smi /project/bm3.5/smi/test/zd/self>tg -n -t
n:-t
smi /project/bm3.5/smi/test/zd/self>tg -o  -n 100
tg: invalid option -- o
n:100
可以看到,解析不尽如人意,在k和l带有多余参数的时候没有报错,这个还可以理解,但在n和t命令少带参数的时候就应该报错,而不是把后面的命令作为参数给解析了。多余的o命令报错还是处理正确的。
这个命令使用比较方便,但有一定的局限性。

getopt使用例子的更多相关文章

  1. linux —— shell 编程(编程语法)

    导读 本文为博文linux —— shell 编程(整体框架与基础笔记)的第4小点的拓展.(本文所有语句的测试均在 Ubuntu 16.04 LTS 上进行) 目录 再识变量 函数 条件语句 循环语句 ...

  2. getopt例子

    (本例基于win7 + python3.4) import getopt, sys ''' getopt 模块专门用来处理命令行参数 函数 getopt(args, shortopts, longop ...

  3. getopt,getoptlong学习

         getopt和getoptlong被用来解析命令行参数.   一.getopt #include <unistd.h> extern char *optarg; extern i ...

  4. [转]Python 命令行参数和getopt模块详解

    FROM : http://www.tuicool.com/articles/jaqQvq 有时候我们需要写一些脚本处理一些任务,这时候往往需要提供一些命令行参数,根据不同参数进行不同的处理,在Pyt ...

  5. 【python】getopt使用

    来源:http://blog.chinaunix.net/uid-21566578-id-438233.html 注意对比:[python]argparse模块 作者:limodou版权所有limod ...

  6. PHP函数getopt详解

    短参数 它返回一个包含命令行参数的数组.比如,要获得-a -b 和-c的值,可以这么做: $arguments = getopt("a:b:c:"); 可以用下面的方式运行脚本(有 ...

  7. Python 命令行参数和getopt模块详解

    有时候我们需要写一些脚本处理一些任务,这时候往往需要提供一些命令行参数,根据不同参数进行不同的处理,在Python里,命令行的参数和C语言很类似(因为标准Python是用C语言实现的).在C语言里,m ...

  8. 在 Perl 中使用 Getopt::Long 模块来接收用户命令行参数

    我们在linux常常用到一个程序需要加入参数,现在了解一下 perl 中的有关控制参数的模块 Getopt::Long ,比直接使用 @ARGV 的数组强大多了.我想大家知道在 Linux 中有的参数 ...

  9. [Perl] Getopt 函数来接收用户参数的使用

    我们在linux常常用到一个程序需要加入参数,现在了解一下perl中的有关控制参数的函数.getopt.在linux有的参数有二种形式.一种是–help,另一种是-h.也就是-和–的分别.–表示完整参 ...

随机推荐

  1. BZOJ2252: [2010Beijing wc]矩阵距离

    题解: 我脑子里都是翔??? bfs一下就行了 我居然还想什么kd tree!真是too naive,,, #include<cstdio> #include<cstdlib> ...

  2. HNOI2010弹飞绵羊

    不得不说块状数组好神奇的啊!这道题的标签可是splay的启发是合并(什么高大上的东西),竟然这么轻松的就解决了! var x,y,i,j,tot,n,m,ch:longint; f,k,l,bl,go ...

  3. ti processor sdk linux am335x evm /bin/setup-uboot-env.sh hacking

    #!/bin/sh # # ti processor sdk linux am335x evm /bin/setup-uboot-env.sh hacking # 说明: # 本文主要对TI的sdk中 ...

  4. ubuntu - chrome 标题栏, 书签乱码 解决

    只要修改/etc/fonts/conf.d/49-sansserif.conf这个文件就行了—— 打开/etc/fonts/conf.d/49-sansserif.conf这个文件: sudo ged ...

  5. POJ:最长上升子序列

    Title: http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2  ...

  6. sharepoint 2010 在aspx 写lambda 时错误

    在sharepoint 2010 中,写lambda时,遇到错误.在aspx里面,写lambda表达式, 运行时报错,就不明道理了.经过百般测试,终于找到方法: 错误提示: "/" ...

  7. java 中的Exception RuntimeException 区别

    在java的异常类体系中: 1.Error和RuntimeException是非检查型异常,其他的都是检查型异常; 2.所有方法都可以在不声明throws的情况下抛出RuntimeException及 ...

  8. context.Response.End()的用法和本质

    using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Web_C ...

  9. WPF MultiBinding 和 IMultiValueConverter

    MultiBinding,描述附加到单个绑定目标属性的Binding对象的集合.可以指定多个数值绑定. IMultiValueConverter通过转换器使用MultiBingding对象,该对象讲根 ...

  10. [Everyday Mathematics]20150201

    设数列 $\sed{a_n}$ 单调递减趋于零, 证明 $\dps{\vsm{n}a_n}$ 收敛当且仅当 $\dps{\vsm{n}3^k a_{3^k}}$ 收敛.