Chaining in a command line several tranformations

2019-03-04 05:44发布

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

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?

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

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.

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

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.

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

My questions:

  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?

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

1条回答
Fickle 薄情
2楼-- · 2019-03-04 06:44

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

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

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 (\\).

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).

查看更多
登录 后发表回答