2
我想用Python的Click library解析一些命令行参数,并将提供的值保存在对象中。使用Python的点击库保存命令行选项的值
我的第一个猜测是做这样的:
import click
class Configuration(object):
def __init__(self):
# configuration variables
self.MyOption = None
# method call
self.parseCommandlineArguments()
@click.command()
@click.option('--myoption', type=click.INT, default=5)
def parseCommandlineArguments(self, myoption):
# save option's value in the object
self.MyOption = myoption
# create an instance
configuration = Configuration()
print(configuration.MyOption)
然而,这是不行的,而不是我得到:
TypeError: parseCommandlineArguments() takes exactly 2 arguments (1 given)
显然,通过self
的装饰功能不正确的方法来做到这一点。如果我从方法参数中删除self
,那么我可以做print(myoption)
,它会在屏幕上打印5
,但是我的Configuration()
类的任何实例都不会知道该值。
处理这个问题的正确方法是什么?我认为它与context handling in Click有关,但我无法根据提供的示例使其工作。