I wrote a library for manipulating specific files. That library comprises many functions that would be useful to use as command line scripts and piping results.
In my library I have a few functions:
def do_this(p1, p2=[]):
return "something"
def do_that(p1, p2, p3={}):
return "else"
What would be nice on the shell
:
$ cat my_file > do_this -p1 doo -p2 be doo | do_that -p1 be -p2 doo -p3 key1=bee key2=bop
I could write a script for each function parsing arguments - with argparse
, docopt
, etc. However, I have several functions to turn into command line scripts and I can already see the redundancy in writing the function first and then the parsing of arguments before calling the function.
So I would like to know what kind of alternative - if any - could be used in that case? Are there different, more productive approaches to writing Python libraries and their associated command line scripts?
My current idea may be naive but I see something that would parse decorated/tagged functions. Then it would produce the command line script with all required parameters. It could be based on the docstring of the functions. It kind of reminds me of the library docopt
which requires conventions and parses the command's doc string to generate the argument parser.