5

シンプルな CLI を作成するために docopt を使用しようとしてきましたが、何らかの理由で既定のパラメーターが表示されません。以下は私のテストコードです。docopt.pygithub リポジトリの最新バージョンを使用しています。

"""
Usage:  scrappy <path> ... [options]

-a --auto           Automatically scrape and rename without user interaction.
-l --lang           Specify language code [default: en].
--scan-individual   Evaluate series information individually for each file.
-c --cfg            User alternate config file [default: ../scrappy.conf]
-t --test           Test run.  Do not modify files.
-v --verbose        Print verbose output
"""

from docopt import docopt
arguments = docopt(__doc__, version='0.1.0 alpha')
print arguments  # DEBUG

実行したときの出力は次のとおりです$ scrappy/scrappy.py first_path_parameter second/path/parameter

{'--auto': None,
 '--cfg': None,
 '--lang': None,
 '--scan-individual': None,
 '--test': None,
 '--verbose': None,
 '<path>': ['first_path_parameter', 'second/path/parameter']}

何が起こっているか知っている人はいますか?

編集:

コードを更新しましたが、まだ同様の出力が得られます。さらに、 を渡そうと--scan-individualすると、引数が必要であるというエラーが表示されます。繰り返しになりますが、問題が発生した場合に備えて、docopt.py をプロジェクトの作業ディレクトリにコピーするだけで docopt を実行しています。何が起きてる?

#!/usr/bin/env python

"""Scrappy:  Rename media files based on scraped information.

Usage:  scrappy <path> ... [options]

-a --auto               Automatically scrape and rename without user interaction.
-l LANG --lang LANG     Specify language code [default: en].
--scan-individual       Evaluate series information individually for each file.
-c CONF --cfg CONF      User alternate config file [default: ../scrappy.conf]
-t --test               Test run.  Do not modify files.
-v --verbose            Print verbose output
"""

from docopt import docopt
arguments = docopt(__doc__, version='0.1.0 alpha')
print arguments  # DEBUG

出力:

$ scrappy/scrappy.py first_path_parameter second/path/parameter --scan-individual
--scan-individual requires argument
Usage:  scrappy <path> ... [options]
4

4 に答える 4

5

例を見ると、デフォルト値を渡したい場合は、ターゲット変数を指定する必要があるようです。

naval_fate.py:  --speed=<kn>  Speed in knots [default: 10].
options_example.py-  --exclude=PATTERNS   exclude files or directories which match these comma
options_example.py:                       separated patterns [default: .svn,CVS,.bzr,.hg,.git]
options_example.py-  -f NAME --file=NAME  when parsing directories, only check filenames matching
options_example.py:                       these comma separated patterns [default: *.py]

だからあなたの場合、

-l LANG --lang LANG  Specify language code [default: en].
-c CONFIG            User alternate config file [default: ../scrappy.conf]

生産する

localhost-2:coding $ python doc.py --auto a b c
{'--auto': True,
 '--lang': 'en',
 '--scan-individual': False,
 '--test': False,
 '--verbose': False,
 '-c': '../scrappy.conf',
 '<path>': ['a', 'b', 'c']}

編集:OPが投稿した更新されたコードは、約30分前にダウンロードしたgithubバージョンでうまく機能します:

localhost-2:coding $ ./scrappy.py first_path_parameter second/path/parameter --scan-individual
{'--auto': False,
 '--cfg': '../scrappy.conf',
 '--lang': 'en',
 '--scan-individual': True,
 '--test': False,
 '--verbose': False,
 '<path>': ['first_path_parameter', 'second/path/parameter']}

だから私は途方に暮れています。

于 2012-12-21T18:47:17.050 に答える
3

私は自分のコードでこの問題に出くわし、次のことがわかるまでテストしました(hargriffleとDSMからの他の回答を使用して)。

これはdocopt 0.6.1の時点であることに注意してください

このファイルを実行する場合:

#! /usr/bin/env python
"""scans.py

Usage:
  scans.py [<args>...]

Options:
  -h --help           Show this screen.
  --version           Show version.
  -L INT --limit=INT  Query response row limit [default: 10]

"""

from docopt import docopt

if __name__ == '__main__':
    print docopt(__doc__)

次の出力を受け取ります

{'<args>': []}

しかし、次のように、使用法の行で引数がオプションであることを具体的に書いた場合:

Usage:
  scans.py [-L INT | --limit=INT] [<args>...]

私が望んでいたものを受け取りました:

{'--limit': '10',
 '<args>': []}
于 2013-12-27T19:50:34.477 に答える