M.A.C.H
Magical Argparse Command Helper
Features
Get your CLI interfaces quickly
Turn a simple class to a CLI application or an interactive interpreter.
Given:
class Calculator:
def add(self, a, b):
"""adds two numbers and prints the result"""
return a + b
def div(self, a, b):
"""divide one number by the other"""
return a / b
You can make command line application using the decorator mach1:
from mach import mach1
@mach1()
class Calculator:
def add(self, int: a, int: b):
"""adds two numbers and prints the result"""
print(a + b)
def div(self, int: a, int: b):
"""divide one number by the other"""
print(a / b)
calc = Calculator()
calc.run()
Now if you run the module, you will get a program that you can invoke with the flag -h or --help:
$ python calc.py -h
usage: calc.py [-h] {add,div} ...
positional arguments:
{add,div} commands
add adds two numbers and prints the result
div divide one number by the other
optional arguments:
-h, --help show this help message and exit
each method is a subcommand, with type checking and it’s own very help. Hench, this won’t work:
$ python calc.py add foo bar
usage: calc.py add [-h] b a
calc.py add: error: argument b: invalid int value: 'foo'
And this will:
$ python calc.py add 4 9
13
To see the help of the subcommand use -h:
$ python calc.py add -h
usage: calc.py add [-h] b a
positional arguments:
b
a
optional arguments:
-h, --help show this help message and exit
With the help of the decorator mach2 you can turn your class to CLI application and have also an iteractive shell which invoke when no parameters are given:
$ ./examples/calc2.py
Welcome to the calc shell. Type help or ? to list commands.
calc2 > ?
Documented commands (type help <topic>):
========================================
add div exit help
calc2 > help add
adds two numbers and prints the result
calc2 > add 2 4
6
calc2 > div 6 2
3.0
calc2 > exit
Come back soon ...
$
Installation
You can get mach from PyPI using pip:
$ pip install mach.py
Metadata
Release files for mach.py 0.4.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mach.py-0.4.4.tar.gz | 78.7 kB | Without provenance | Details for mach.py-0.4.4.tar.gz |
Release files / mach.py-0.4.4.tar.gz
| Download URL | mach.py-0.4.4.tar.gz |
|---|---|
| Size | 78.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a06b596331d5a9d23a9430dc52449b5699ed55559407e9467696bad492bf19f6
|
|
BLAKE2b-256 checksum How to use checksums |
31dda7dbbf458c3610793b72ecabd3ad137a1b350b0fe4611f617cc0f9b1878e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0rc2
|
Security for mach.py 0.4.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.