I have a useful Python script that I've been invoking from the command line. It has decent number of options, maybe 20, and it's not unusual to run the script with six or seven flags. Then the rest of the input comes via stdin.
Now I have some other Python code from which I'd like to call this useful little utility. Two options I can think of are:
- I can use subprocess.call and invoke my little script
- (A little better) I can cobble together a command line and then pass it as a list of strings to
argparse
- I can totally refactor the program so that the entry point of my utility is Python function call and then have my command line utility just call this function. In principle this seems like the responsible thing to do, but it does leave my managing two separate interfaces to my function. For example, I have to decide whether I want to let argparse know the default values for my options or have the function know the defaults (or have two sets of defaults). Any validation I do using, say,
ArgumentParser.add_mutually_exclusive_group
will not apply when my tool is run as a library instead of a command line script.
Is there a standard paradigm for creating a single interface in Python that is well-suited to being invoked both from Python and from the command line?