1

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.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
kaligne
  • 3,098
  • 9
  • 34
  • 60
  • can't you use `do_this` , `do_that` as arguments for one script - `cat my_file > script do_this ...| script do_that ... ` ? – furas Oct 04 '16 at 06:45
  • If you had docstrings or annotations describing the parameters then yes, you could write something to generate command line options from that. However I am not aware of any existing package that does this and either finding it or helping you write it is out of scope for SO. – jonrsharpe Oct 04 '16 at 07:02
  • 1
    Take a look at click - http://click.pocoo.org/5/ its a framework for writing command line apps in Python which could save you a lot of the boilerplate stuff. – shevron Oct 04 '16 at 07:03
  • @shevron Thanks that's he kind of thing I have been looking for! I've been playing with click for a while so far. However I have encountered a little problem. Solution should be easy I guess, but it does not strike me as of now. If interested: http://stackoverflow.com/questions/40091347/call-function-from-click-command – kaligne Oct 17 '16 at 19:18
  • @furas Actually I am looking for an innovative, unconventionnal way of doing this. `click` seems to be pretty close to what I've had in mind! – kaligne Oct 17 '16 at 19:20

1 Answers1

0

As you use cat in you example, I assume that you use a Linux or other Unix-like system. A common way would be to have several links to your python script, one for each function. Then in the script, you parse the arguments once, and depending on the value of sys.argv[0] call the proper function.

...
if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='...')
    ...
    cmd = os.path.basename(sys.argv[0])
    args = parser.parse_args()
    if cmd == 'cmd1':
        cmd1(args)
    elif cmd == 'cmd2':
        cmd2(args)
    ...

and the script would be linked to cmd1, cmd2, ...

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Thanks that's a generic way of doing I guess I already do that actually. However I was looking for an innovative way, such as with using the `click` library – kaligne Oct 17 '16 at 19:16