简体   繁体   English

使用“from”作为Python中的脚本参数

[英]Using “from” as a script argument in Python

My assignment requires that "from" be used as an argument for the command line input. 我的作业要求“from”用作命令行输入的参数。

p = optparse.OptionParser()
p.add_option("--from")
p.add_option("--to")
p.add_option("--file", default="carla_coder.ics")
options, arguments = p.parse_args()

print options.from

obviously, "from" is a Python keyword... is there any way to get around this? 显然,“from”是一个Python关键字......有什么方法可以解决这个问题吗? Basically, the script should be run using file.py --from=dd/mm/yyyy --to=dd/mm/yyyy --file=file 基本上,脚本应该使用file.py --from=dd/mm/yyyy --to=dd/mm/yyyy --file=file

Use the dest attribute to specify a name: 使用dest属性指定名称:

p.add_option("--from", dest="foo")

print options.foo

Use Python's getattr function: 使用Python的getattr函数:

getattr(options, 'from')

Will behave like options.from , except that the attribute name doesn't have to follow Python's usual variable naming rules (including keyword conflicts). 将表现得像options.from ,除了属性名称不必遵循Python通常的变量命名规则(包括关键字冲突)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM