Put operands first in getopt()

半城伤御伤魂 提交于 2019-12-11 02:42:53

问题


Using the getopt() function in C, it is possible to do that:

program -a arg_for_a -b arg_for_b -c operand1 operand2

and it works with no problem.

But, how to make it work this way?:

program operand1 operand2 -a arg_for_a -b arg_for_b -c

In this case, every argument, including the -a, -b etc. are considered to be operands.

I'm trying to make just like like gcc or ssh does:

gcc code.c -o executable
ssh user@host -i file.pem

That is, no matter in what position the options and the operands come, they are recognized properly.

How to make options be recognized properly wherever they are, and every word that do not follow an option be recognized an operand?


回答1:


If you use the GNU C library's implementation of getopt, then it will work like all GNU utilities do, because almost all of them use it. In particular, (quoting from man 3 getopt):

By default, getopt() permutes the contents of argv as it scans, so that eventually all the nonoptions are at the end.

That's not quite the same as gcc. gcc cares about the relative order of optional and positional arguments. (For example, it makes a difference where -l goes in the command line.) To do that, you'll have to tell GNU getopt to not permute the arguments. Then, every time getopt reports that it's done, optind will have the index of the next positional argument (if any). You can then use that argument, increment optind, and continue using getopt, which will continue at the next argument.



来源:https://stackoverflow.com/questions/17690289/put-operands-first-in-getopt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!