问题描述
我的命令行实用程序应该接受彼此连接的多个过滤器(类似于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
在这里,我尝试链接四个过滤器子命令:script
,chain
,tranformation
,tranformation
.但是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?
我的问题:
- 此类命令行可能有哪些用户友好格式?
- 如何使用Python
argparse
解析它? - 如果不是
argparse
,那么该怎么办?
- What are possible user-friendly formats for such command lines?
- How to parse it with Python
argparse
? - 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+
正则表达式将+
拆分为多个过滤器.但是,在拆分时,引用的+
(\+
)将被忽略.
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
之前,应在Whytespace上拆分每个过滤器.
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 (\\
).
请注意,通过调用诸如sh -c 'tr a-z A-Z | tac'
之类的shell管道(此处是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).
这篇关于在命令行中链接带有选项的多个转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!