arguments and options and optarg in C

简介:
用C写一个命令行工具的话, 可能会经常用到argument和options.
例如 : 
1. ps -ewf 这里-ewf就是option.
2. ps -eo rs,cmd 这里 rs 和 cmd则是option -o 的 optarg
3. ls /root 这里/root则是ls命令的argument.

在C里面怎么应用呢?
使用到的是unistd.h, 
SYNOPSIS
       #include <unistd.h>

       int getopt(int argc, char * const argv[], const char *optstring);
       extern char *optarg;
       extern int optind, opterr, optopt;

getopt 用于获取定义好的option 字符, 带冒号表示这个option有optarg. 在命令中调用option使用一个横杠.
optarg 是个字符指针.
optind 表示The variable optind is the index of the next element of the argv[] vector to be processed.

举个例子 : 
[root@db-172-16-3-150 zzz]# cat n.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>

int main(int argc, char * argv[]) {
  int digoal;
  char *equipment="";
  char ch;
  int i;
  while((ch = getopt(argc, argv, "de:")) != EOF) {
    switch(ch) {
      case 'd':
        digoal = 1;
        break;
      case 'e':
        equipment = optarg;
        break;
      default:
        fprintf(stderr, "Unknown option: %s\n", optarg);
        return 1;
    }
  }
  if (digoal)
    fprintf(stdout, "yes -d used\n");
  if (*equipment)
    fprintf(stdout, "equipment is:%s\n", equipment);
  for(i=optind;i<argc;i++) {
    fprintf(stdout, "your argument %i is:%s\n", i, argv[i]);
  }
  fprintf(stdout, "your argument %i is:%s\n", 0, argv[0]);  //argv[0] 指命令本身.
  return 0;
}

结果
[root@db-172-16-3-150 zzz]# gcc -O3 -Wall -Wextra -Werror -g ./n.c -o n && ./n -d -e tank -- -1 a b c d e
yes -d used
equipment is:tank
your argument 5 is:-1
your argument 6 is:a
your argument 7 is:b
your argument 8 is:c
your argument 9 is:d
your argument 10 is:e
your argument 0 is:./n


注意如果把两个杠杠去掉会报错, 因为getopt()会一直检索, 直到遇到-- 结束. 它会吧-1 当成option.
[root@db-172-16-3-150 zzz]# gcc -O3 -Wall -Wextra -Werror -g ./n.c -o n && ./n -d -e tank -1 a b c d e
./n: invalid option -- 1
Unknown option: (null)


把-1 放在-e后面是没问题的, 因为-e 后面被任务是optarg.
[root@db-172-16-3-150 zzz]# gcc -O3 -Wall -Wextra -Werror -g ./n.c -o n && ./n -d -e -1 a b c d e
yes -d used
equipment is:-1
your argument 4 is:a
your argument 5 is:b
your argument 6 is:c
your argument 7 is:d
your argument 8 is:e
your argument 0 is:./n

空格是各个选项的分隔符 : 
[root@db-172-16-3-150 zzz]# gcc -O3 -Wall -Wextra -Werror -g ./n.c -o n && ./n -d -e a,b,c,d a b c d e
yes -d used
equipment is:a,b,c,d
your argument 4 is:a
your argument 5 is:b
your argument 6 is:c
your argument 7 is:d
your argument 8 is:e
your argument 0 is:./n

相关文章
|
4月前
|
Python
完美解决丨TypeError: fun() takes 2 positional arguments but 3 were given
完美解决丨TypeError: fun() takes 2 positional arguments but 3 were given
|
5月前
|
开发工具
gdbusauth.c:1302:11: error: ‘%s’ directive argument is null
gdbusauth.c:1302:11: error: ‘%s’ directive argument is null
77 0
|
12月前
TypeError: custom() got an unexpected keyword argument ‘path‘
TypeError: custom() got an unexpected keyword argument ‘path‘
112 0
解决TypeError: __init__() takes from 1 to 3 positional arguments but 6 were given
解决TypeError: __init__() takes from 1 to 3 positional arguments but 6 were given
360 0
解决TypeError: __init__() takes from 1 to 3 positional arguments but 6 were given
|
C语言
error: implicit declaration of function ‘VerifyFixClassname‘ is invalid in C99 [-Werror,-Wimplicit-f
error: implicit declaration of function ‘VerifyFixClassname‘ is invalid in C99 [-Werror,-Wimplicit-f
117 0
error: implicit declaration of function ‘read‘ [-Werror,-Wimplicit-function-declaration]
error: implicit declaration of function ‘read‘ [-Werror,-Wimplicit-function-declaration]
180 0
成功解决TypeError: __init__() got an unexpected keyword argument 'serialized_options'
成功解决TypeError: __init__() got an unexpected keyword argument 'serialized_options'
|
JavaScript
函数arguments讲解
函数arguments讲解
函数arguments讲解
|
计算机视觉
成功解决TypeError: __init__() got an unexpected keyword argument 'indices'
成功解决TypeError: __init__() got an unexpected keyword argument 'indices'