Turn your python functions into CLI-friendly scripts.
Project description
CLI-cake
Easily pass CLI args into functions. Turn your functions into scripts.
CLI-cake takes this:
import sys
def echo(*args, capitalize = False):
if capitalize:
args = [a.upper() for a in args]
print(' '.join(args))
caps = False
for arg in args:
if arg == '--capitalize':
caps = True
echo(sys.argv[1:], caps)
and turns it into this
import cake
@cake.runnable
def echo(*args, capitalize = False):
if capitalize:
args = [a.upper() for a in args]
print(' '.join(args))
echo.runCLI()
$ python echo.py hello world --capitalize
HELLO WORLD
Why
I realized that when I'm building quick, simple scripts for internal use, I resort to one of two approaches:
- I just hardcode the values into my script, and just change the values each time I use it
- I build out some type of custom CLI or gui interface for the script
Both of these approaches have downsides- with the first approach, you end up spending more time between runs editing code, and end up with the bad practice of directly editing your source code for non-permanent changes. With the second approach, you end up spending extra time up front writing code, and it can make concise scripts more messy by adding in this extra code.
clicake is a bunch of convenience methods that solve these problems, making it super quick and easy to transform your functions into fully-featured CLI-callable scripts.
Installing
Getting Started
clicake is really easy to use! Try creating the file 'echo.py' and add the following code:
import cake
@cake.runnable
def echo(*args, capitalize = False):
if capitalize:
args = [a.upper() for a in args]
print(' '.join(args))
if __name__ == '__main__':
echo()
OR
import cake
def echo(*args, capitalize = False):
if capitalize:
args = [a.upper() for a in args]
print(' '.join(args))
if __name__ == '__main__':
cake.run(echo)
And in your terminal
$ python echo.py hello world --capitalize
HELLO WORLD
Documentation
Decorators
def runnable(callback, args, print_output):
pass
Methods
def run(callback):
pass
Objects
class Cake():
def runnable(callback, args, print_output):
pass
def run(callback):
pass
Examples
A simple example:
import cake
@cake.runnable
def echo(*args, capitalize = False):
if capitalize:
args = [a.upper() for a in args]
print(' '.join(args))
echo.runCLI()
$ python echo.py hello world --capitalize
HELLO WORLD
Multiple Methods
Using the Cake() object allows you to build runners that can handle multiple functions from the command line. The 'first' argument the script recieves (argv[1]) is interpreted as the name of the function, which can be set explicitly through the name
parameter, or defaults to the method name.
Take the following code as example.py
:
import cake
cli = cake.Cake()
@cli.runnable
def echo(*args):
return (' '.join([str(a) for a in args]))
@cli.runnable(name="add")
def sum(*args):
_sum = 0
for arg in args:
_sum += arg
return _sum
cli.run()
$ python example.py echo hello world
hello world
$ python example.py add 1 2 3 4 5
15
Defining a method as runnable also doesn't modify the __call__
method for the decorated function, the only change with the wrapped function is it adds a callable runCLI
attribute to the function that can be used to tell the program to parse the CLI args and execute the function based on those args. This lets us use @runnable decorated functions without having to modify dependent code
import cake
cli = cake.Cake()
@cli.runnable
def sum(*args):
_sum = 0
for arg in args:
_sum += arg
return _sum
@cli.runnable
def doublesum(*args):
return sum(*args) * 2 # sum function can be called like normal!
cli.run()
$ python example.py sum 1 2 3 4 5
15
$ python example.py doublesum 1 2 3 4 5
30
Testing
From root, you can run the tests with
python -m cake.tests
Build / Deploy
Install build dependencies locally:
pip install --user --upgrade twine setuptools wheel
Build source and distribution files:
python setup.py sdist bdist_wheel
upload to PyPi with Twine:
python -m twine upload dist/*
You'll be prompted to use __token__
as the username, and you'll need to use an API token from PyPi as the password.
Planned Features (todo)
- Add support for specifying explicit types, and code hydration for passing in lambdas
- Add support for input() based text interfaces in addition to just CLI args? (This is already decently well supported by just running the python interactively and importing the desired functions... will evaluate to see if there's actually a need here)
- Add option to wrap errors with 'pretty' printed output
- Add option to specify printed output stream
- Add more extensive testing / unit tests
- improved documentation
Advanced Topics
pyinstaller
I'd recommend using pyinstaller
if you want to generate standalone executable script builds in addition to cli-cake. Read more on there site here.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file cli-cake-0.0.3.tar.gz
.
File metadata
- Download URL: cli-cake-0.0.3.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3bab9120278a84273d5cbb9c2b06a6aecc6548f626367822e0f3e20ec4bc984a |
|
MD5 | 903a4f5592fa1c184b6f910f12a37193 |
|
BLAKE2b-256 | c0187517a36ea838a5e169b12af11166f1dfddb6db312e1c633c4f0d39e73f7e |
File details
Details for the file cli_cake-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: cli_cake-0.0.3-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f85c47a62b11de7fb5e2013b7de3afe269c0d75edee168ca8b300f3308de96d3 |
|
MD5 | b51cebcc8ba3f616f0cd90a6a0f57d1e |
|
BLAKE2b-256 | 508f1ca11438a54ef596c0618c77a14d0e066c33acf13ab3dab4b39d7f0cdba6 |