带负数的getopt? [英] getopt with negative numbers?

查看:123
本文介绍了带负数的getopt?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以使用getopt并仍然允许负数作为

args?我可以轻松地编写一个解决方法(预处理

的尾部参数,剥离任何非选项,包括否定

数字到一个单独的序列中并忽略(现在是空的)args list

由getopt返回,但似乎这是一个常见的

要求,可以选择将负值视为

一个参数。请注意,如果第一个非选项

为负值,这只是一个问题,因为getopt会立即停止处理选项

它标识了第一个参数值。


或者,optparse会处理这个吗?我还没有使用optparse(我知道它是更强大的OO $ B $ ,但你倾向于坚持你所知道的,特别是当它是我正常的python编程的一部分

模板时),但如果它处理负数我就是愿意学习它。

Is there an easy way to use getopt and still allow negative numbers as
args? I can easily write a workaround (pre-process the tail end of
the arguments, stripping off any non-options including negative
numbers into a separate sequence and ignore the (now empty) args list
returned by getopt, but it would seem this is such a common
requirement that there would be an option to treat a negative value as
an argument. Note that this is only a problem if the first non-option
is a negative value, since getopt stops processing options as soon as
it identifies the first argument value.

Alternatively, does optparse handle this? I haven''t used optparse (I
know it is more powerful and OO, but you tend to stick with what you
know, especially when it is part of my normal python programming
template), but if it handles negative numbers I''m willing to learn it.

推荐答案

Casey写道:
Casey wrote:

是否有一种简单的方法可以使用getopt并仍然允许负数作为

args?我可以轻松地编写一个解决方法(预处理

的尾部参数,剥离任何非选项,包括否定

数字到一个单独的序列中并忽略(现在是空的)args list

由getopt返回,但似乎这是一个常见的

要求,可以选择将负值视为

一个参数。请注意,如果第一个非选项

为负值,这只是一个问题,因为getopt会立即停止处理选项

它标识了第一个参数值。


或者,optparse会处理这个吗?我还没有使用optparse(我知道它是更强大的OO $ B $ ,但你倾向于坚持你所知道的,特别是当它是我正常的python编程的一部分

模板时),但如果它处理负数我就是愿意学习它。
Is there an easy way to use getopt and still allow negative numbers as
args? I can easily write a workaround (pre-process the tail end of
the arguments, stripping off any non-options including negative
numbers into a separate sequence and ignore the (now empty) args list
returned by getopt, but it would seem this is such a common
requirement that there would be an option to treat a negative value as
an argument. Note that this is only a problem if the first non-option
is a negative value, since getopt stops processing options as soon as
it identifies the first argument value.

Alternatively, does optparse handle this? I haven''t used optparse (I
know it is more powerful and OO, but you tend to stick with what you
know, especially when it is part of my normal python programming
template), but if it handles negative numbers I''m willing to learn it.



optparse可以处理负数int val的选项呃; - 可以用来表示没有更多选项的
信号:

optparse can handle options with a negative int value; "--" can be used to
signal that no more options will follow:


>> import optparse
parser = optparse.OptionParser()
parser.add_option(" -a",type =" int")
>>import optparse
parser = optparse.OptionParser()
parser.add_option("-a", type="int")



< 0xb7d6fd8c的选项:-a>

<Option at 0xb7d6fd8c: -a>


>> options,args = parser.parse_args([" -a" ," -42"," - "," -123"])
options.a
>>options, args = parser.parse_args(["-a", "-42", "--", "-123"])
options.a



-42

-42


>> args
>>args



[''-123'']


没有 - arg你会收到一个错误:

[''-123'']

Without the "--" arg you will get an error:


>> parser.parse_args([" -123"])
>>parser.parse_args(["-123"])



用法:[选项]


:错误:否这样的选择:-1

Usage: [options]

: error: no such option: -1





Peter


Peter


9月27,下午1:34,Peter Otten< __ pete ... @ web.dewrote:
On Sep 27, 1:34 pm, Peter Otten <__pete...@web.dewrote:

optparse可以处理负int值的选项; " - "可用于

表示不再有任何选项:
optparse can handle options with a negative int value; "--" can be used to
signal that no more options will follow:



谢谢,Peter。 getopt支持POSIX - 选项结束

指标也是如此,但这似乎没有优于

能够简单地设置一个告诉解析器的值我不会使用任何

数值作为选项,我想允许负值为

arguments"。在解析器级别实现这将是微不足道的。

我坦率地希望它已经实现,它只是在备件Python getopt库引用中没有提到


Thanks, Peter. getopt supports the POSIX "--" end of options
indicator as well, but that seems a little less elegant than being
able to simply set a value that tells the parser "I don''t use any
numeric values as options, and I want to allow negative values as
arguments". At the parser level implemening this would be trivial and
I frankly was hoping it had been implemented and it just wasn''t
mentioned in the spares Python getopt library reference.


这篇关于带负数的getopt?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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