本文介绍了在命令行中链接多个带有选项的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的命令行实用程序应该接受几个相互连接的过滤器(类似于 Unix 管道).

My command line utility should accept several filters attached to each other (similar to Unix pipeline).

每个过滤器都有许多选项.例如 chain 过滤器目前有以下选项:

Each filter has a number of options. For example chain filter currently has the following options:

  -t NAMESPACE, --target NAMESPACE
                        target namespace(s)
  -s {precedence,doc}, --next-script {precedence,doc}
                        "next script" algorithm ("precedence" is not
                        supported)
  -n {ignore,remove,error}, --not-in-target {ignore,remove,error}
                        what if a result is not in target NS
  -u URL, --universal-precedence URL
                        universal precedence
  -W {inverseofsum,sumofinverses}, --weight-formula {inverseofsum,sumofinverses}
                        formula for weighting scripts

如何在命令参数中表示过滤器的工作流程?

How to represent in command arguments the workflow of filters?

第一个想法是使用 JSON 数组.但是 JSON 并不是一个很好的命令行格式.

The first thought is to use a JSON array. But JSON is not quite a good format for a command line.

接下来的想法是用一些分隔符分隔过滤器.它可能是 ----separator 或类似的.但是 -- 已经被用来将命令行选项与其他命令行参数分开,而且 --separator 看起来不太好.

The next thought is to separate the filters with some separator. It may be -- or --separator or like this. But -- is already taken to separate command line options from other command line arguments, and --separator does not look good.

另一个问题是,虽然我使用 Python 3 argparse,但这个包似乎不太适合在一个命令行中链接的几个子命令,例如:

Another trouble is that while I use Python 3 argparse, this package seems not to be well-suited for several subcommands chained in one command line, like:

boiler script filter1 --separator \
  chain -t http://www.w3.org/1999/xhtml -n error --separator \
  transformation filter2 --arg x=y --separator \
  transformation filter3

这里我尝试链接四个过滤器子命令:scriptchaintransformationtransformation.但是 Python argparse(似乎)不支持在一个命令行中包含多个子命令.

Here I attempt to chain four filter subcommands: script, chain, tranformation, tranformation. But Python argparse (seemingly) does not support having several subcommands in one command line.

所以如果不是 argparse 那么使用什么库来处理这样的命令行?

So if not argparse then what library to use to process command lines like this?

我的问题:

  1. 此类命令行有哪些可能的用户友好格式?
  2. 如何用 Python argparse 解析它?
  3. 如果不是argparse,那又是什么?
  1. What are possible user-friendly formats for such command lines?
  2. How to parse it with Python argparse?
  3. If not argparse, then what instead?

另请注意,将来我可能会决定用其他语言重写我的软件(D,可能) 而不是 Python.

Also take note, that in the future I may decide to rewrite my software in some other language (D, probably) than Python.

推荐答案

管道应该作为我程序的单个参数(其中可能有空格,例如使用 shell 引用)提供.

The pipeline should be provided as a single argument (with possible spaces in it, e.g. using shell quoting) of my program.

管道可以通过 +s 即通过 \s+\+\s+ 正则表达式分成几个过滤器.但是,在拆分引用的 + (\+) 时应忽略.

The pipeline could be split into several filters by +s that is by \s+\+\s+ regexp. However while splitting quoted + (\+) shall be ignored.

然后每个过滤器都作为单独的命令行处理(例如使用 argparse).在传递给 argparse 之前,每个过滤器都应在原因空间上拆分.

Then every filter is processed as a separate command line (e.g. using argparse). Before passing to argparse every filter shall be split on whytespace.

此外,\ 引用空格(以便过滤器不会在此空格处拆分)和它本身(\\).

Also, \ quotes whitespaces (so that the filter is not split at this space) and itself (\\).

请注意,通过调用 shell 管道(如 sh -c 'tr a-z A-Z | )来暗示将管道作为单个参数提供.tac'(这里的管道是 sh 程序的单个参数).

Note that providing the pipeline as a single argument is hinted by calling shell pipelines like sh -c 'tr a-z A-Z | tac' (here the pipeline is a single argument of sh program).

这篇关于在命令行中链接多个带有选项的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 10:00