This is a quick demonstration of how to create a function that can be used as either:
- command-line script with parameters, or
- imported module function
Direct
$ python mod.py abc --list xyz,123
x = 'abc'
y = ['xyz', '123']
Run Module
Alternatively, it can be run as a module. This will search the sys.path
for
a module with the same name and run it like the script above.
$ python -m mod abc --list xyz,123
x = 'abc'
y = ['xyz', '123']
Help Output
$ python mod.py --help
usage: mod.py [-h] [--list LIST] [x]
Do some stuff
positional arguments:
x A value with some meaning
optional arguments:
-h, --help show this help message and exit
--list LIST, -l LIST CSV list of things
>>> from mod import foo
>>> foo('abc', ['xyz', '123'])
x = 'abc'
y = ['xyz', '123']