分析命令行参数

来源:互联网 发布:网络教育学历代理 编辑:程序博客网 时间:2024/06/06 11:47
名字
    getopt -- get option character from command line argument list


    标准c库 (libc,-lc)

定义
    #include <unistd.h>

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

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

描述
    这个getopt()函数分析一个命令行参数列argv,并返回下一个已知的选项(option)。如果一个选项在可接受选项串optstring中列出,那么它就是已知的。

    这个选项串optstring可以包含下列元素:单个字符,字符后面接一个冒号说明后面跟随一个选项参数。例如,一个选项字符"x"表示选项"-x",选项字符"x:"表示选项和其参数"-x argument"。一个跟随的参数argument是否引导一个空格与getopt()无关。

    在getopt()的返回中,optarg指向选项参数,如果它是anticipated,并且变量optind包含下一个argv参数作为对getopt()下一次调用的索引。变量optopt保存最后一个由getopt()返回的已知的选项。

    变量opterr和optind都被初始化为1。变量optind可能在对getopt()的调用前已设成其他值的调用前,为了略去或多或少的argv入口。

    为了使用getopt()来处理多组参数,或多次处理单组参数,变量optreset必须在每一个对getopt()附加的调用前设置成1,并且变量optind必须已被初始化。

    当参数列已经到尾时getopt()函数返回-1,或者返回'?'当遇到一个未知的选项。参数列中选项的解释可能会被'--'取消,由于它引起 getopt()给参数处理发送结束信号并返回-1。当所有的选项都被处理后(比如一直到第一个非选项参数),getopt()返回-1。

诊断
    如果getopt()函数遇到一个在optstring中没有的字符或检测到一个缺选项的参数,它会向stderr中写一个错误信息并返回'?'。将 opterr置0可以使它不向stderr中写错误信息。如果optstring以':'开头,那么一个却选项的参数会使':'返回除 suppressing之外的任何错误信息。

    选项参数允许使用"-"开头;这是合理的,但可能减少错误检测的总数。

扩展
    变量optreset的增加使多次调用getopt()函数成为可能。这是IEEE 1003.2(POSIX.2)规范的扩展部分。

举例
    int bflag, ch, fd;

    bflag = 0;
    while ((ch = getopt(argc, argv, "bf:")) != -1)
     switch (ch) {
     case 'b':
      bflag = 1;
      break;
     case 'f':
      if ((fd = open(optarg, O_RDONLY, 0)) < 0)
       err(1, "%s", optarg);
      break;
     case '?':
     default:
      usage();
     }
    argc -= optind;
    argv += optind;

--------------------------------------------------------------------
atoi() -- 将字符串转换成整型数

atoi, atol, atoll, atoq -- convert a string to an integer
相关函数: atof, atol, atrtod, strtol, strtoul
表头文件: #include <stdlib.h>
定义函数: int atoi(const char *nptr);
函数说明: atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字字符串结束时('/0‘)才结束转换,并将结果返回。
返回值:   返回转换后的整型数。
附加说明: atoi(nptr)与strtol(nptr, (char**)NULL, 10)结果相同


#include <stdlib.h>
#include <stdio.h>
main() {
    char     *a = "-100";
    char     *b = "456";
    int     c;
    c = atoi(a) + atoi(b); //356
    printf("c = %d/n", c);
}


--------------------------------------------
strdup() -- 字符串拷贝

原型:char *strdup(char *s);       
用法:#include <string.h>
功能:复制字符串s
说明:返回指向被复制的字符串的指针,所需空间由malloc()分配且可以由free()释放。
    The strdup() function returns a pointer to a new string which is a duplicate of the string s. Memory for the new string is obtained with malloc(), and can be freed with free().


#include <stdio.h>
#include <string.h>     //strdup()
#include <stdlib.h>     //free()

int main()
{
    char *s="Zeng Xiaolong";
    char *d;

    d = strdup(s);
    printf("%s/n", d);
    free(d);
    return 0;
}



深入分析
----------------------------------------------------
char *strdup(const char *str)
{
    size_t len;
    char   *copy;
   
    len = strlen(str) + 1;
    copy = malloc(len);
   
    if(copy != NULL)
        strcpy(cpy, str);
    return copy;
}
当不再需要返回值的时候,就应该(必须)用free()把它释放掉。