A python CLI for busy scientists

cli, command, argparse, click, script, function, bash, py2sh
pip install py2sh==0.0.3


py2sh: instantaneous, zero baggage python CLI for busy scientists


This is a micro argparse for turning any python function into a bash script. For scientists who are busy and need to throw a CLI together in 3 seconds. It works for args and kwargs, autoparses strings and integers, handles arrays through kwargs, and autogenerates help messages


The philosophy underlying py2sh is threefold:

  • It be barebones simple yet effectively powerful and versatile
  • Require zero mental energy to setup
  • Have no configurability as a feature

py2sh was designed to work out of the box. It should take no more than 20 seconds of writing to setup a powerful CLI to an arbitrary python function.

py2sh is so simple that no configuration is necessary nor should be for its use case. This reduces the mental burden of setting up the CLI because no documentation ever needs to be consulted

When to use py2sh

When you need to setup scrappy python scripts to do a whole lot of minutiae.

When not to use py2sh

When you are building a production level command line app.


There really is no documentation necessary other than the single function that py2sh exports, the @py2sh.shify decorator.

Keeping discussion to a minimum, here is an illustration of how to use @py2sh.shify

Suppose the following code is located in

import py2sh as py2sh

       usage="Connect to your mongodb database")                         
def dbconnect(
  print "Name: "+ name
  print "Port: "+ str(port)
  print "Nodename: " + nodename
  print "Autoconnect: " + str(autoconnect)
  print pids

With those two lines of initialization we have built a powerful bash command line interface to dbconnect.

Typing python dbconnect help or python dbconnect help=1 yields the following automatically generated usage message.

usage: dbconnect

    Connect to your mongodb database

      [<arg>, ...]

And if we run it from bash

python name=mongod port=8889 nodename=node189 autoconnect=True pids=1 pids=2 pids=3

Name: mongod
Port: 8889
Nodename: node189
Autoconnect: True
[1, 2, 3]

Would you look at that! A robust command line interface with parsing and autogenerated help messages with nothing more than 2 lines of python.

This really will save you tons of time and is actually much quicker than the click library. Whereas click requries decorators for specifying the arguments, py2sh figures them out based on the function signature

Important Notes

  • How to run: @py2sh.shify only parses from the command line if it is called with either of two flags: cli=<string> and name=<name> and one of those two flags are set to a particular value.

If either name=="__main__" or cli="allways", py2sh will parse argc for command line arguments and invoke the function on them. Otherwise nothing happens and the function can be used in the rest of the python script as normal.