Вот как я бы это написал ...
""" My Crazy Program
Usage:
my_prog [options]
Options:
"""
def my_function(file_output, file_input, recursive=False):
"""
output=str: specifies an output path
input=str: specifies an input path
recursive: apply recursively
"""
print("The first argument is %r" % file_output)
print("The second argument is %r" % file_input)
print("The third argument is %r" % recursive)
# This is where the magic happens
__doc__ += '\n'.join(f' --{parsed[0]: <15} {parsed[1]}'
for parsed in (
line.strip().split(': ')
for line in my_function.__doc__.split('\n'))
if len(parsed) == 2)
if __name__ == "__main__":
from docopt import docopt
ARGS = docopt(__doc__)
my_function(ARGS['--output'], ARGS['--input'], ARGS['--recursive'])
Хорошо, вы видите магическую линию (начиная с __doc__ += ...
), она создает документацию для модуля, которая становится:
My Crazy Program
Usage:
my_prog [options]
Options:
--output=str specifies an output path
--input=str specifies an input path
--recursive apply recursively
Затем docopt анализирует и возвращает этот словарь:
$ python my_prog
{'--input': None,
'--output': None,
'--recursive': False}
$ python my_prog --output /dev/null --recursive
{'--input': None,
'--output': '/dev/null',
'--recursive': True}
, который можно использовать для вызова функции и получения результата:
The first argument is '/dev/null'
The second argument is None
The third argument is True
Мне нравится эторешение, потому что это одна строка, но я согласен с вами, что это не красиво, я позволю вам написать свою собственную красивую функцию, которая делает это автоматически для каждого файла: o)