ksh"getopts" -取消选项 [英] ksh "getopts" -- Unsetting an Option

查看:109
本文介绍了ksh"getopts" -取消选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用内置的"getopts" ksh来处理命令行选项,并且我正在寻找一种干净/标准的方式来取消"命令行上的选项.我不知道这是关于getopts的技术问题还是更多关于样式/标准的问题.无论如何,我知道getopts从左到右处理其args(默认为$ *),并且我希望使用它来允许选项的第二个规范来关闭"一个选项.

I use the "getopts" ksh built-in to handle command-line options, and I'm looking for a clean/standard way to "unset" an option on the command line. I don't know if this is a technical question about getopts or more of a style/standards question. Anyway, I understand that getopts processes its args (by default $*) from left to right, and I'm hoping to use that to allow a second specification of an option to "turn off" an option.

对于带有选项参数的选项,这不是问题.在我的getopts处理案例语句中,我只是说"-x"选项,例如:

For options that take an option argument, this isn't a problem. In my getopts processing case statement I just say, for the "-x" option, for example:

x ) x_arg=$OPTARG
    ;;

并且在命令行上最后一个"-x"实例获胜,这就是我想要的.

and the last instance of "-x" on the command line wins, which is what I want.

该问题来自不带选项参数的选项.如果该选项只是一个开关/标志,那么我的case语句将具有以下内容:

The problem comes in with options that don't take an option argument. If the option is just a switch/flag then my case statement would just have something like:

x ) x_specified=1
    ;;

使用这样的选项,我真正能做的就是在命令行上再次添加"-x",并且发生同样的事情:再次执行"x_specified = 1". 取消设置x_specified"的最佳/最简单/最简单/最标准的方法是什么?这是我的问题.

With an option like this all I can really do is add "-x" again on the command line, and the same thing just happens: "x_specified=1" is executed again. What's the best/easiest/simplest/standardest way to "unset x_specified"? That's my question.

我曾希望调用getopts功能,该功能允许在选项字母前加上"+"或-",但是似乎无法在getopts处理循环中判断是否使用了"+"或-".如果有办法告诉我,那么我可以采用"+"表示关闭设置的标准.我可以做的另一件事是:

I had hoped to invoke the getopts functionality that allows either "+" or "-" to precede option letters, but it seems there's no way to tell within the getopts processing loop whether "+" or "-" was used. If there were a way to tell then I could adopt the standard that "+" means to turn off the setting. The other thing I could do is something like:

x ) if [[ -z $x_specified ]] ; then
        x_specified=1
    else
        unset x_specified
    fi
    ;;

现在,-x"选项将是一个切换;每次在命令行上看到设置都会更改.但是,我想在命令行上说关闭-x设置",而不仅仅是切换-x设置".我还可以选择其他一些字母,例如"X"作为关闭它的方式,但是我的许多脚本已经使用同一字母的大写和小写版本来表示不同的内容,如果我必须选择其他一些随机字母来表示转", off -x",那么这并不是很助记,而且我的脚本已经足够学习如何使用它了.

Now the "-x" option would be a toggle; each time it's seen on the command line the setting changes. However, I'd like a way on the command line to say, "turn off the -x setting," and not just "toggle the -x setting." I could also pick some other letter, e.g. "X", as the way to turn it off, but many of my scripts already use both the upper case and lower case versions of the same letter to mean different things, and if I have to pick some other random letter to mean "turn off -x" then that's not very mnemonic, and my scripts are hard enough to learn how to use already.

最后,您可能会问:如果要在命令行上关闭-x,为什么首先要打开它?"我的脚本中的选项处理经历了多步过程.会检查命令行选项,还会检查配置文件中的选项,并且两者之间有优先级.对于带有选项参数的选项,这一切都可以正常工作,但是对于没有选项的选项,该方案会分崩离析.

Finally, you may ask, "If you want -x off on the command line, why are you turning it on in the first place?" Option processing in my scripts goes through a multi-step process. Command-line options are checked, and also a config file is checked for options, and there's a precedence between the two. This all works fine for options that take an option argument, but the scheme falls apart for options that don't.

推荐答案

我有ksh93,如果optstring以"+"开头,它允许 + 选项:

I've got ksh93, and it allows + options if the the optstring starts with "+":

while getopts +abc opt; do
    echo "have option '$opt'"
done

运行此:

$ ./args.ksh -a -b -c +b -b foo 
have option 'a'
have option 'b'
have option 'c'
have option '+b'
have option 'b'
$ ksh --version
  version         sh (AT&T Research) 93u+ 2012-08-01

我在手册页中看到一个歧义:它表示:"选项也必须是optstring中的前导"字符.

One ambiguity I see in the man page: it says that the ":" option must also be the "leading" character in the optstring.

http://linux.die.net/man/1/ksh93

这篇关于ksh"getopts" -取消选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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