【问题标题】:Using argparse or docopt for pdftk replacement使用 argparse 或 docopt 替换 pdftk
【发布时间】:2016-12-23 05:56:58
【问题描述】:

我正在编写一个接受以下形式的争论的程序。

SYNOPSIS
pdf_form.py <input PDF file | - | PROMPT> [ <operation> <operation arguments> ]
    [ output <output filename | - | PROMPT> ] [ flatten ]
Where:
    <operation> may be empty, or: [generate_fdf | fill_form |dump_data | dump_data_fields ]
OPTIONS
--help, -h
    show summary of options.

<input PDF files | - | PROMPT>
    An input PDF file. Use - to pass a single PDF into pdftk via stdin.

[<operation> <operation arguments>]
    Available operations are:
    generate_fdf,fill_form, dump_data,dump_data_fields
    Some operations takes additional arguments, described below.

    generate_fdf
        Reads a single, input PDF file and generates an FDF file suitable for fill_form out of it
        to the given output filename or (if no output is given) to stdout. Does not create a new PDF.

    fill_form <FDF data filename | - | PROMPT>
        Fills the input PDF's form fields with the data from an FDF file, or stdin.
        Enter the data filename after fill_form, or use - to pass the data via stdin, like so:

        ./pdf_form.py form.pdf fill_form data.fdf output form.filled.pdf

        After filling a form, the form fields remain interactive unless flatten is used.
        flatten merges the form fields with the PDF pages. You can also use flatten alone, as shown:

        ./pdf_form.py form.pdf fill_form data.fdf output out.pdf flatten

        or:

        ./pdf_form.py form.filled.pdf output out.pdf flatten

    dump_data
        Reads a single, input PDF file and reports various statistics,  to the given output filename or (if no output is given).

    dump_data_fields
         Reads a single, input PDF file and reports form field statistics to the given output filename

[flatten]
    Use this option to merge an input PDF's interactive form fields and their data with the PDF's pages.

我目前正在使用以下(混乱的)代码解析这些选项

def parse(arg):
    """ Parses commandline arguments. """
    #checking that request is valid
    if len(arg) == 1:
        raise ValueError(info())
    if arg[1] in ('-h', '--help'):
        print(info(verbose=True))
        return
    if len(arg) == 2:
        raise ValueError(info())

    path = arg[1]
    if path == 'PROMPT':
        path = input('Please enter a filename for an input document:')

    func = arg[2]

    if func == 'fill_form':
        fdf = arg[3]
        if len(arg) > 5 and arg[4] == 'output':
            out = arg[5]
        else:
            out = '-'
    else:
        fdf = None
        if len(arg) > 4 and arg[3] == 'output':
            out = arg[4]
        else:
            out = '-'

    if out == 'PROMPT':
        out = input('Output file: ')

    flatten = 'flatten' in arg

    return path, func, fdf, out, flatten

使用 argparse 或 docopt 是否有更好的方法来做到这一点?

【问题讨论】:

  • argparse 不会要求任何inputs。它可以查看sys.argv 字符串,并将值分配给args.pathargs.func 等属性。但是在解析完成后,必须从用户那里获取任何进一步的输入。
  • 我遇到的问题是让它工作,所以如果 func='fill_fom' 它需要一个 fdf,否则它不会寻找一个。

标签: python command-line-arguments argparse docopt


【解决方案1】:

一个参数可以是

parser.add_argument('-o','--output', default='-')

以后

if args.output in ['PROMPT']:
    ... input...

其他:

parser.add_argument('--flatten', action='store_true')
parser.add_argument('--fill_form', dest='ftd')
parser.add_argument('--path')

if args.path in ['PROMPT']:
     args.path = input...

【讨论】:

  • dest='ftd' 是做什么的?
  • 它把值放在args.ftd而不是args.fill_form;它允许您使用一个字符串作为标志,而另一个作为属性名称。
  • 我会玩这个,看看我能不能让它工作。谢谢
  • 我遇到的问题是像 flatten 这样的东西需要被视为可选的位置参数(flattten,而不是--flatten)。如果我是第一次做这个,这会很容易,但我正在替换现有的软件。
  • 您的operation 部分看起来可以转换为subparsers。但是对于主解析器来说,这个论点是一个必需的位置。 subparsers 不能很好地与可选的 (nargs='?') 定位。如果您无法更改现有规范,并且这些规范偏离了常见的 UNIX 实践,则很难使用argparse
猜你喜欢
  • 2018-11-18
  • 2014-01-03
  • 2016-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-14
  • 1970-01-01
  • 2016-09-03
相关资源
最近更新 更多