该文档对于子类化 CommandLineApp 确实含糊不清,仅提及一个示例:
class YourApp(cli.app.CommandLineApp):
def main(self):
do_stuff()
因此,根据我发现的信息,我将这段代码拼凑在一起:
#!/usr/bin/env python
import os
import sys
from cli.app import CommandLineApp
# Append the parent folder to the python path
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
import tabulardata
from addrtools import extract_address
class SplitAddressApp(CommandLineApp):
def main(self):
"""
Split an address from one column to separate columns.
"""
table = tabulardata.from_file(self.params.file)
def for_each_row(i, item):
addr = extract_address(item['Address'])
print '%-3d %-75s %s' % (i, item['Address'], repr(addr))
table.each(for_each_row)
def setup(self):
self.add_param('file', metavar='FILE', help='The data file.')
self.add_param(
'cols', metavar='ADDRESS_COLUMN', nargs='+',
help='The name of the address column. If multiple names are ' + \
'passed, each column will be checked for an address in order'
)
if __name__ == '__main__':
SplitAddressApp().run()
这对我来说似乎是正确的。setup
该文档没有提供有关在使用子类化时如何处理方法或运行应用程序的示例。我得到错误:
回溯(最近一次通话最后): 文件“bin/split_address_column”,第 36 行,在 SplitAddressApp().run() 文件“/Users/tomas/.pythonbrew/venvs/Python-2.7.3/address_cleaner/lib/python2.7/site-packages/cli/app.py”,第 440 行,在 __init__ Application.__init__(self, main, **kwargs) 文件“/Users/tomas/.pythonbrew/venvs/Python-2.7.3/address_cleaner/lib/python2.7/site-packages/cli/app.py”,第 129 行,在 __init__ self.setup() 文件“bin/split_address_column”,第 28 行,在设置中 self.add_param('file', metavar='FILE', help='数据文件。') 文件“/Users/tomas/.pythonbrew/venvs/Python-2.7.3/address_cleaner/lib/python2.7/site-packages/cli/app.py”,第 385 行,在 add_param action = self.argparser.add_argument(*args, **kwargs) AttributeError:“SplitAddressApp”对象没有属性“argparser”
所以大概我做错了什么,但是什么?