option followed by a option in getopt [where the earlier option was expecting a value]

谁都会走 提交于 2019-12-13 01:04:58

问题


I have a doubt if we have two option -a and -c and option -a needs to have a value and no value for -c

now if i give ->testopt -a -c [testopt is program]

then my program takes -c as a value for -a . is there a way where i can make sure that a option with value does not have other options as its values ?

the thing is that the system accepts - one keyword's value as another keyword which i need to prevent . e.g. - testopt -a -c. in this case there was a mandatory argument (value ) for -a and no value for -c.. but i my getopt interprets this as "-c" is the value for "-a" how can i throw a error on this using getopt.


回答1:


You get -c as the value for the -a option because you specified -a to have a value. If you call your program with the options the other way around

$ testopt -c -a

then getopt will return with a '?' indicating that an option value is missing (as per the getopt manual page), and an error will be printed on the console.

Unfortunately there is no way of telling getopt that an argument has an optional value, then you have to implement your own option parser.



来源:https://stackoverflow.com/questions/12617749/option-followed-by-a-option-in-getopt-where-the-earlier-option-was-expecting-a

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