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.

5
  • can't you use do_this , do_that as arguments for one script - cat my_file > script do_this ...| script do_that ... ? Commented Oct 4, 2016 at 6: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. Commented Oct 4, 2016 at 7:02
  • 1
    Take a look at click - click.pocoo.org/5 its a framework for writing command line apps in Python which could save you a lot of the boilerplate stuff. Commented Oct 4, 2016 at 7: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: stackoverflow.com/questions/40091347/… Commented Oct 17, 2016 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! Commented Oct 17, 2016 at 19:20

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.