0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

コマンドライン引数処理(Python の docopt)

Last updated at Posted at 2020-04-18

こんにちは
Python の docopt(コマンドライン引数処理)1 を試してみました。

下記例で、--oo--pp--qq の記述の違いを試しました。--qq の場合には結果がリストとして返ってきました(この記述は公式にはお勧めではないような気もします)。のかもしれません)。

$ ./docopt_test.py
{'--help': False,
 '--oo': None,
 '--pp': None,
 '--qq': [],
 'X': []}
$
$ ./docopt_test.py -o 0 -p 1 -q 2 3 4
{'--help': False,
 '--oo': '0',
 '--pp': '1',
 '--qq': ['2'],
 'X': ['3', '4']}
$
docopt_test.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

def argsdocopt(doc):
    import docopt, textwrap
    return docopt.docopt(textwrap.dedent(doc))    

def main():
    """
    {f}: Test a docopt library.

    usage: {f} [-h] [-o O] [-p <P>|--pp <P>] [-q|--qq <Q>] [X...]
    
    options:
        -h, --help        show this help message and exit
        -o, --oo O        parameter O
        -p <P>, --pp <P>  parameter P
        -q, --qq <Q>      parameter Q
    """
    args = argsdocopt(main.__doc__.format(f=__file__))
    print(args)

if __name__ == '__main__':
    main()
  1. Pythonのdocopt使い方メモ」も参考にしました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?