https://github.com/oz123/mach
create awesome command line applications in a blaze
https://github.com/oz123/mach
command-line decorators library python3
Last synced: 8 months ago
JSON representation
create awesome command line applications in a blaze
- Host: GitHub
- URL: https://github.com/oz123/mach
- Owner: oz123
- License: lgpl-3.0
- Created: 2018-05-21T07:37:21.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2022-12-26T20:37:02.000Z (over 3 years ago)
- Last Synced: 2024-10-11T11:20:10.654Z (over 1 year ago)
- Topics: command-line, decorators, library, python3
- Language: Python
- Size: 86.9 KB
- Stars: 13
- Watchers: 2
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.rst
- Contributing: CONTRIBUTING.rst
- License: LICENSE
Awesome Lists containing this project
README
========
M.A.C.H
========
.. image:: https://readthedocs.org/projects/mach/badge/?version=latest
:target: http://mach.readthedocs.io/en/latest/?badge=latest
.. image:: https://travis-ci.org/oz123/mach.svg?branch=master
:target: https://travis-ci.org/oz123/mach
.. image:: https://coveralls.io/repos/github/oz123/mach/badge.svg?branch=master
:target: https://coveralls.io/github/oz123/mach?branch=master
Magical Argparse Command Helper
.. image:: https://raw.githubusercontent.com/oz123/mach/master/imgs/mach-logo.jpg
Features
--------
* Get your CLI interfaces quickly
* Turn a simple class to a CLI application or an interactive interpreter.
Given:
.. code:: python
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``:
.. code:: python
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``:
.. code:: shell
$ 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:
.. code:: shell
$ 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:
.. code:: shell
$ python calc.py add 4 9
13
To see the help of the subcommand use ``-h``:
.. code:: shell
$ 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:
.. code:: shell
$ ./examples/calc2.py
Welcome to the calc shell. Type help or ? to list commands.
calc2 > ?
Documented commands (type help ):
========================================
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