LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1012|回复: 5

getopt对于不是以option方式给出的参数怎么获得

[复制链接]
发表于 2006-1-17 10:09:23 | 显示全部楼层 |阅读模式
int getopt(int argc, char * const argv[], const char *optstring);
optstring以:分隔的原则是什么
就像man里面while ((c = getopt(argc, argv, ":abf:")) != -1)

看不懂
哪位能解释一下 :abf: 的含义  
九号 已解决
继续看man发现新的疑问 在四楼有描述
还请多多帮助
发表于 2006-1-17 11:05:05 | 显示全部楼层
from beginning linux programming 3rd
To help us adhere to these guidelines, Linux gives us the getopt facility, which supports the use of
options with and without values and is simple to use.
#include <unistd.h>
int getopt(int argc, char *const argv[], const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
The getopt function takes the argc and argv parameters as passed to the program’s main function and
an options specifier string that tells getopt what options are defined for the program and whether they
have associated values. The optstring is simply a list of characters, each representing a single character
option. If a character is followed by a colon, it indicates that the option has an associated value that will
be taken as the next argument. The getopts command in bash performs a very similar function.
For example, the following call would be used to handle our preceding example
getopt(argc, argv, “if:lr”);
It allows for simple options -i, -l , -r, and -f, followed by a filename argument. Calling the command
with the same parameters but in a different order will alter the behavior. Try it out when we get to the
sample code in the next “Try It Out” section in this chapter.


:表示指定的参数后面要求一个值,如:
xx -i -f filename -l -r
那个值可以用 optarg 取到。

btw, 要勾中"在文字中禁用表情符号"
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-1-17 11:33:50 | 显示全部楼层
谢谢了
看了半天的man 
愣是没看懂
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-1-17 11:53:33 | 显示全部楼层
就好像
cc -o sock sock.c

cc sock.c -o sock
都可以用一样
若是用getopt获得输入参数 对于sock.c这个参数是怎么获得的
看man似乎是 与optind 有点关系  但是不明白
请解释一下
回复 支持 反对

使用道具 举报

发表于 2006-1-17 17:06:26 | 显示全部楼层

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. int main(int argc, char *argv[])
  4. {
  5. int opt;
  6. while((opt = getopt(argc, argv, “if:lr”)) != -1) {
  7. switch(opt) {
  8. case ‘i’:
  9. case ‘l’:
  10. case ‘r’:
  11. printf(“option: %c\n”, opt);
  12. break;
  13. case ‘f’:
  14. printf(“filename: %s\n”, optarg);
  15. break;
  16. case ‘:’:
  17. printf(“option needs a value\n”);
  18. break;
  19. case ‘?’:
  20. printf(“unknown option: %c\n”, optopt);
  21. break;
  22. }
  23. }
  24. for(; optind < argc; optind++)
  25. printf(“argument: %s\n”, argv[optind]);
  26. exit(0);
  27. }
复制代码


printf(“filename: %s\n”, optarg);
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-1-18 10:31:32 | 显示全部楼层
谢谢
我的意思是那些不是以 -opt para
指出的parameter
man里面说
getopt当遇到此类parameter会返回-1
我也试验了
程序用getopt时 有限制
就是必须把有-opt 指定的参数写在前面
否则的话 若是在 -opt 之间出现不是有选项指定的参数
参数后面的 选项就失效了
假设 f:a:
a.out -f t1 -a t2 t3
这个时候t1 t2 t3都会按照设想的方式获得
a.out -f t1 t3 -a t2
但是这个时候就不会把-a t2认为是一个选项 因为getopt在遇到 t3的时候就退出了
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表