0

所以我知道你可以用它getopt()来查找选项参数,但是 argv 数组中的其他东西,比如目标呢?有没有办法做到这一点getopt()?浏览手册页,我什么也没看到……也许我错过了。

4

1 回答 1

2

用于optarg那个(它是一个字符串,用于sscanf从中读取)。

引用自man 3 getopt

   optstring is a string containing the legitimate option characters.  If such a character is followed by a colon, the option requires an argument, so getopt()
   places  a  pointer to the following text in the same argv-element, or the text of the following argv-element, in optarg.  Two colons mean an option takes an
   optional arg; if there is text in the current argv-element (i.e., in the same word as the option name itself, for example, "-oarg"), then it is returned  in
   optarg,  otherwise  optarg  is  set  to zero.  This is a GNU extension.  If optstring contains W followed by a semicolon, then -W foo is treated as the long
   option --foo.  (The -W option is reserved by POSIX.2 for implementation extensions.)  This behavior is a GNU extension, not available with libraries  before
   glibc 2.

如果您有额外的参数 (à la cp -l file1 file2),它们将保留在argv最新的位置,从 . 开始optind

PS:页面末尾还有一个示例man

于 2013-11-02T20:40:51.543 回答