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

easyargs

A project designed to make command line argument parsing easy.

There are many ways to create a command line parser in python: argparse, docopt, click. These are all great options, but require quite a lot of configuration and sometimes you just need a function to be called. Enter easyargs. Define the function that you want to be called, decorate it and let easyargs work out the command line. This is probably best shown with an example that takes one required argument and two optional ones:

import easyargs

@easyargs
def main(name, count=1, greeting='Hello'):
    """A simple greeting program"""
    for i in range(count):
        print '{greeting} {name}!'.format(greeting=greeting, name=name)


if __name__ == '__main__':
    main()

In this example, main is inspected, the arg keywords are turned into positional arguments and the kwarg keywords will be turned into optional arguments. This can be seen if we run the above script with the help flag:

$ python simple.py -h
usage: simple_test.py [-h] [--count COUNT] [--greeting GREETING] name

A simple greeting program

positional arguments:
  name

optional arguments:
  -h, --help           show this help message and exit
  --count COUNT
  --greeting GREETING

A few things worth noting. Firstly, the description is taken from the docstring of the function. Secondly, there is no need to convert count to an integer. Because the default argument is of type int, the value is coerced to an integer:

$ python simple.py World
Hello World

$ python simple.py everybody --count 2 --greeting Hola
Hola everybody!
Hola everybody!

How to define the function

The goal of easyargs is to avoid having complicated configuration parameters, and let the function specify things, however, the following list of rules might be useful:

  • main(arg): arg is a required positional argument
  • main(_arg): arg is an optional positional argument
  • main(arg=int, _arg=int): Setting a default value as a basic type will keep the argument positional, but coerce it to that type only tested with int / float
  • main(arg=list): Setting a default argument as a list will consume multiple arguments from the command line. It doesn’t make sense to supply this more than once.
  • main(arg=value): Creates an optional argument with a default value of value
  • main(arg=3): If the default value is of type int / float. Then if a value is set it will be coerced to the type.
  • main(arg=True): If the default value is of type bool, then arg becomes a flag option.
  • main(a=values): If the argument has a length of 1, then it will create a short argument.

Sub commands

Whilst having a simple function parser is great, sometimes you need to have a sub parser. This can be created by wrapping a number of functions in a class. Let’s demonstrate this with another example by duplicating part of the git command. At the same time we’ll introduce the concept of using the docstring to include the help text for each function parameter.

import easyargs

@easyargs
class GitClone(object):
    """A git clone"""

    def clone(self, src, _dest):
        """
        Clone a repository
        :param src: The source repository to clone from
        :param _dest: The directory to check the source code to
        """

    def commit(self, a=False, m=None, amend=False):
        """
        Commit a change to the index
        :param a: Add all tracked files to the index
        :param m: Supply the commit message on the command line
        :param amend: Amend the previous commit
        """
        print 'Committing {m}'.format(m=m)

Let’s see what this looks like on the command line:

$ python examples/git_clone.py -h
usage: git_clone.py [-h] {clone,commit} ...

A git clone

positional arguments:
  {clone,commit}  sub-command help
    clone         Clone a repository
    commit        Commit a change to the index

optional arguments:
  -h, --help      show this help message and exit

$ python examples/git_clone.py clone
usage: git_clone.py clone [-h] src [dest]
git_clone.py clone: error: too few arguments

$ python examples/git_clone.py clone -h
usage: git_clone.py clone [-h] src [dest]

positional arguments:
  src         The source repository to clone from
  dest        The directory to check the source code to

optional arguments:
  -h, --help  show this help message and exit

$ python examples/git_clone.py commit -am "Message"
Committing Message
Release History

Release History

0.9.2

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.9.1

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.9.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.8.2

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.8.1

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.8.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
easyargs-0.9.2-py2.py3-none-any.whl (9.8 kB) Copy SHA256 Checksum SHA256 py2.py3 Wheel Nov 13, 2016
easyargs-0.9.2.tar.gz (9.6 kB) Copy SHA256 Checksum SHA256 Source Nov 13, 2016

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