Python 3 で初めて docopt を使用しています。コードを実行すると、出力には使用オプション データのみが表示され、モジュール内の関数は実行されません。
ここに例があります。私はファイルにこのコードを持っています。
"""Usage: scratch.py [-h] [--boston | --sandiego] [--prostitution |
--drugs] [--lim VALUE]
Options:
-h --help :Show help information
--boston : work on boston addresses
--sandiego : work on san diego addresses
--prostitution : prostitution incidents
--drugs : drug sale incidents
--lim : number of addresses to work with
"""
from docopt import docopt
def scratch(args):
print(args['--boston'])
print('hello')
if __name__ == '__main__':
arg = docopt(__doc__, help=False)
print(arg)
scratch(arg)
このファイルをさまざまなオプションで実行すると、得られるのは docstring のレプリカだけです。docopt が docstring 引数から作成した辞書のコピーが表示されるはずです。次に、関数呼び出しからの出力結果も表示する必要があります。しかし、docstring 以外は何も表示されません。
したがって、次のようなコマンドライン呼び出しを行うと:
python scratch.py --boston --drugs --lim 100
返されるのは次のすべてです。
Usage: scratch.py <city> [--boston | --sandiego] <crime> [--prostitution |
--drugs] --count=N
Options:
city : city to geocode
--boston : work on boston addresses
--sandiego : work on san diego addresses
crime : crime to select on
--prostitution : prostitution incidents
--drugs : drug sale incidents
-count : number of addresses to work with
定義された関数を実際に実行できるように、何が問題なのかを知りたいです。