Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

python command-line interfaces made easy

Project Description

easycommandline.py

a solution for python command-line interfaces, inspired by tj/commander.js

Installation

$ pip install easycommandline

Option

Options are defined with the .options() method, also serving as documentation for the options. The example below parses args and options from sys.argv

# file favcolor.py

from easycommandline import program

program.version('1.0.1')
program.options(
        ('-r', '--red'),
        ('-g', '--green'),
        ('-b', '--blue'),
        ('-w', '--white'),
    )
program.parse_argv()

if program.red:
    print('- red')
if program.blue:
    print('- blue')
if program.white:
    print('- white')
$ python favcolor.py -r -w
    - red
    - white

Coercion

# file coercion.py

from easycommandline import program

def collect(value, previous_value):
    previous_value.append(value)
    return previous_value

def make_range(value):
    components = value.split('..')
    return range( int(components[0]), int(components[1]) )

def sum(value, previous_value):
    return previous_value + int(value)

program.version('1.0.1')
program.options(
        ('-r', '--recursive'),
        ('-p', '--preload', 'preload enabled'),
        ('-n', '--name', 'username'),
        ('-i', '--integer <int>', 'an integer argument', int),
        ('-f', '--float <float>', 'a float argument', float),
        ('-a', '--anotherfloat', 'another float argument', float),
        ('-c', '--collection [example]', 'collection', collect, []),
        ('-x', '--range A..B', 'range', make_range),
        ('-s', '--sum', 'the sum of numbers', sum, 0),
    )
program.parse_argv()

print('recursive  : ', program.recursive)
print('preload    : ', program.preload)
print('name       : ', program.name)
print('integer    : ', program.integer)
print('float      : ', program.float)
print('collection : ', program.collection)
print('range      : ', program.range)
print('sum        : ', program.sum)
$ python coercion.py -p --name Tracy -i 1000 --range 5..8 -s 1 2 3 4
    recursive  :  None
    preload    :  True
    name       :  Tracy
    integer    :  1000
    float      :  None
    collection :  []
    range      :  [5, 6, 7]
    sum        :  10

Command

specifies
argument required with < >
argument optional with [ ], cannot specify more than one optional argument
# file cmd.py

from easycommandline import program
import os

program.version('1.0.1')


cmd = program.cmd('cd <path>')
cmd.options(
        ('-c', '--create', 'create directory if not existed'),
        )
cmd.description('change directory')
@cmd.action
def action_for_cmd_cd(cmd, path):
    if cmd.create and not os.path.exists(path):
        os.makedirs(path)



cmd = program.cmd('start server <path> [env]')
@cmd.action
def action_for_cmd_start_server(cmd, path, env):
    print(path)
    print(env)


program.parse_argv()
$ python cmd.py cd ~/Desktop/abc -c
$ python cmd.py start server ~/Desktop/abc production

Automated –help

$ python coercion.py -h

    Usage: python coercion.py [options]

    Options:

    -h, --help                  output usage information
    -V, --version               output the version number
    -r, --recursive
    -p, --preload               preload enabled
    -n, --name                  username
    -i, --integer <int>         an integer argument
    -f, --float <float>         a float argument
    -a, --anotherfloat          another float argument
    -c, --collection [example]  collection
    -x, --range A..B            range
    -s, --sum                   the sum of numbers
Release History

Release History

This version
History Node

1.8.1

Download Files

Download Files

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

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
easycommandline-1.8.1.tar.gz (6.7 kB) Copy SHA256 Checksum SHA256 Source Jul 18, 2015

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting