我正在编写一个脚本,它有两个互斥的参数,以及一个只对其中一个参数有意义的选项.我试图将argparse设置为失败,如果你用没有意义的参数调用它.
要明确:
-m -f 说得通
-s 说得通
-s -f 应该抛出错误
没有争论是好的.
我的代码是:
parser = argparse.ArgumentParser(description='Lookup servers by ip address from host file')
parser.add_argument('host', nargs=1,
help="ip address to lookup")
main_group = parser.add_mutually_exclusive_group()
mysql_group = main_group.add_argument_group()
main_group.add_argument("-s", "--ssh", dest='ssh', action='store_true',
default=False,
help='Connect to this machine via ssh, instead of printing hostname')
mysql_group.add_argument("-m", "--mysql", dest='mysql', action='store_true',
default=False,
help='Start a mysql tunnel to the host, instead of printing hostname')
mysql_group.add_argument("-f", "--firefox", dest='firefox', action='store_true',
default=False,
help='Start a firefox session to the remotemyadmin instance')
Run Code Online (Sandbox Code Playgroud)
哪个不起作用,因为它吐出来
usage: …Run Code Online (Sandbox Code Playgroud) 好的,我会先前承认这是一个巨大的问题,我可以肯定地更好地实现这一点.只有病态的好奇心才能让我知道如何做到这一点.
class SomeClass(object):
def __init__(self):
def __(self, arg):
self.doStuff(arg)
self.overLoaded = __
def doStuff(self, string):
print string
SomeClass().overLoaded("test string")
Run Code Online (Sandbox Code Playgroud)
这会返回一个参数错误,因为我只为overLoaded()提供一个参数而不是两个参数.是否有一些魔法告诉解释器它现在是一个类的方法(我尝试用@classmethod进行装饰,我总是理解这是它的目的??)
我需要渲染wikitext(从相关的mediawiki数据库中提取)并以其他格式显示(最终呈现为PDF,但基本上任何其他格式都可以).
我可以肯定地将一些能够完成这项工作的东西放在一起,但最终我会随着时间的推移编写它,我可以看到,当我团队中的人使用它们时,实施新标签的开销会耗费我很多时间.
有没有这样做的项目?
我看到了用python编写的TiddlyWiki,我将考虑借用他们的库,但与此同时我认为可能有一个项目更有利于有人知道吗?
干杯