This is a pre-production deployment of Warehouse, however changes made here WILL affect the production instance of PyPI.
Latest Version Dependencies status unknown Test status unknown Test coverage unknown
Project Description

A clean and simplistic alternative for argparse, optparse and getopt

Pycommand is essentially a fancy wrapper around getopt that consists of one simple CommandBase class that you can use to create executable commands for your python programs with very simplistic and readable code. It has support for subcommands and also nesting commands, so you can create (multiple levels of) subcommands, with the ability to pass the values of optional arguments of a command object to its subcommand objects. Supported Python versions are 2.7 and 3.2 and later.

Download and install

If you have pip installed, you can just:

# pip install pycommand

To work with the current development version, do something like this:

$ git clone git://bitbucket.org/babab/pycommand.git
# cd pycommand
# pip install -e .

Quick setup from a template

To quicly start writing a command from a template (much like the examples below), use pycommand’s helper script by running:

$ pycommand init

This will ask you for an executable name, class name and template type and it will save it to an executable file, ready to be used as a Python shell script (for your Python package/module)).

Example

For full documentation and examples, visit http://pythonhosted.org/pycommand/

Here is an undocumented code example of getting automated usage text generation and parsing of optional arguments. If we name the script for which you can see the code below basic-example and execute it, the following will be the output for running basic-example -h or basic-example --help:

usage: basic-example [options]

An example of a basic CLI program

Options:
-h, --help                        show this help information
-f <filename>, --file=<filename>  use specified file
--version                         show version information

And here is the code:

#!/usr/bin/env python

import pycommand
import sys


class BasicExampleCommand(pycommand.CommandBase):
    '''An example of a basic CLI program'''
    usagestr = 'usage: basic-example [options]'
    description = __doc__

    optionList = (
        ('help', ('h', False, 'show this help information')),
        ('file', ('f', '<filename>', 'use specified file')),
        ('version', ('', False, 'show version information')),
    )

    def run(self):
        if self.flags.help:
            print(self.usage)
            return 0
        elif self.flags.version:
            print('Python version ' + sys.version.split()[0])
            return 0
        elif self.flags.file:
            print('filename = ' + self.flags.file)
            return 0

        print('Program completed. Try adding "--help"')

if __name__ == '__main__':
    # Shortcut for reading from sys.argv[1:] and sys.exit(status)
    pycommand.run_and_exit(BasicExampleCommand)

    # The shortcut is equivalent to the following:

    # cmd = BasicExampleCommand(sys.argv[1:])
    # if cmd.error:
    #     print('error: {0}'.format(cmd.error))
    #     sys.exit(1)
    # else:
    #     sys.exit(cmd.run())

Why was it created?

When parsing command line program arguments, I sometimes work with argparse (a replacement for optparse). I don’t really like the API and the output it gives, which is the main reason I’ve always used getopt for parsing arguments whenever possible.

The CommandBase class was originally written for DisPass, which is a password manager/generator, as a means to easily define new subcommands and have auto-generated usage messages. Because I want to have this in other projects I’ve decided to put it in the cheeseshop.

Software license

Copyright (c) 2013-2015 Benjamin Althues <benjamin@babab.nl>

Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Change Log

pycommand adheres to Semantic Versioning.

0.3.0 - 2015-06-04

Added

  • Shortcut run_and_exit() for reading from sys.argv[1:] and exiting the interpreter via sys.exit(status)
  • Package as wheel distribution to speed up installations
  • Add man pycommand ability, i.e. install mandoc in /usr/share/man3/

Changed

  • Add support for getting flags by attribute like self.flags.help. The default approach for normal dicts like self.flags['help'] remains valid.

0.2.0 - 2015-05-21

Added

  • Full example of a command with subcommands
  • Create quick templates via pycommand script (pycommand init)
  • Unit tests and automatic testing via Travis-CI
  • Documentation man (.3) and info (.info) pages

Changed

0.1.0 - 2013-08-08

Added

  • Initial release
Release History

Release History

0.3.0

This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.2.0

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.1.0

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Download Files

Download Files

TODO: Brief introduction on what you do with files - including link to relevant help section.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
pycommand-0.3.0-py2.py3-none-any.whl (24.2 kB) Copy SHA256 Checksum SHA256 3.4 Wheel Jun 4, 2015
pycommand-0.3.0.tar.gz (17.0 kB) Copy SHA256 Checksum SHA256 Source Jun 4, 2015

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS HPE HPE Development 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