Skip to main content

Create CLI interfaces quickly

Project description

https://readthedocs.org/projects/mach/badge/?version=latest https://travis-ci.org/oz123/mach.svg?branch=master https://coveralls.io/repos/github/oz123/mach/badge.svg?branch=master

Magical Argparse Command Helper

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:

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mach.py-0.4.2.tar.gz (76.3 kB view details)

Uploaded Source

File details

Details for the file mach.py-0.4.2.tar.gz.

File metadata

  • Download URL: mach.py-0.4.2.tar.gz
  • Upload date:
  • Size: 76.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/36.7.2 requests-toolbelt/0.8.0 tqdm/4.23.3 CPython/3.6.5

File hashes

Hashes for mach.py-0.4.2.tar.gz
Algorithm Hash digest
SHA256 2ce7389dd8bd88a6579f0fb74b895c1a079e485fc3d8199ad8c787a89bad91f0
MD5 ef223de2fc9cb5dc3c998f91f8d3c705
BLAKE2b-256 b90eed0f04a6e06531b24514fb1644a5b2d646ac65b4518c55da21fa0e2deaf7

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page