我可以使用getopt的工艺选择按照一定的顺序? [英] Can I use getopt to process options in a certain order?

查看:126
本文介绍了我可以使用getopt的工艺选择按照一定的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我执行一个命令行工具,我需要能够处理一堆选项。他们完成后的一些选项必须终止程序。比如我有一个选择,b和c。如果A和B后,他们的DONE终止,我给了命令

I'm implementing a command line tool, and I need to be able to handle a bunch of options. Some of the options must terminate the program after they're done. For example I have options a, b and c. If a and b terminate after their done, and I give the command

./myprogram -bca [file] 

有没有办法让一个precedence在这样getopt的使用情况()?

Is there a way to give "a" precedence in a situation like this using getopt()?

编辑:

我解决了这个运行的选项开关,如果选择了选项设置的标志。于是找来所有的标志是看标志,以功能。

I solved this by running a switch on the options and set a flag if an option was selected. Then sent all the flags to a function that looks at the flags in order.

推荐答案

有处理由阅读选项getopt的两种主要方式()。一个是做一个选择,因为 getopt的()阅读的选项的其余部分之前将其标识相关的操作;另一种是做任何处理之前阅读所有选项。 (程序并不罕见实施混合解决方案;有些选项是采取行动,在分析论证,但有些人只是注意到和参数被处理后,所采取的行动)

There are two main ways to process the options read by getopt(). One is to do the action associated with an option as getopt() identifies it before reading the rest of the options; the other is to read all the options before doing any processing. (Programs not infrequently implement hybrid solutions; some options are acted on while parsing the arguments but others are simply noted and the actions taken after the arguments are processed.)

在您的情况,这很可能是你需要做任何处理之前解析所有选项。如果两个 -a -b 终止程序做出相应的动作后,再他们是互相排斥的选项,你也许应该诊断,作为你的程序的错误调用。你需要决定是否OK,让 -c 以及为 -a -b

In your situation, it seems likely that you need to parse all the options before doing any processing. If both -a and -b terminate the program after doing the action, then they're mutually exclusive options and you should probably diagnose that as an erroneous invocation of your program. You will need to decide whether it is OK to allow -c as well as either -a or -b.

很多时候,你将不得不对所需要的共同努力选项,或者你需要知道的,其他的,还是决定做什么之前,双方已被指定。另一个需要考虑的问题是什么意思,如果是重复的命令行选项。

Quite often, you will have pairs of options that are required to work together, or you need to know whether one, the other, or both have been specified before deciding what to do. Another issue to consider is what it means if an option is repeated on the command line.

这篇关于我可以使用getopt的工艺选择按照一定的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆