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

This module implements some reusable custom actions to use with the argparse module.

Examples

The following example, taken from samples/folder_actions.py demonstrates the use of a custom action to verify the existence of a folder, specified from the command line:

import argparse
import argparse_actions

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Custom Actions')
    parser.add_argument('directory',
                action=argparse_actions.FolderExistsAction)

    try:
        args = parser.parse_args()
        print 'Directory exists: {0}'.format(args.directory)
    except argparse_actions.NonFolderError as e:
        print 'Directory does not exist'
        print e

In the next example from samples/proper_ip.py, we use the ProperIpFormatAction custom action to verify if an IP address from command line is properly formatted:

import argparse
import argparse_actions

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Custom Actions')
    parser.add_argument('ip',
                    action=argparse_actions.ProperIpFormatAction)

    try:
        args = parser.parse_args()
        print 'IP is properly formatted: {0}'.format(args.ip)
    except argparse_actions.InvalidIp as e:
        print 'IP is invalid: {0}'.format(e.ip)
        # This will display similar output:
        # print e

Extending the Custom Actions

If you find a custom action that almost do what you want, you can

  1. Write your own from scratch
  2. Submit an enhancement request
  3. Extend the existing custom action

I am not commenting on option 1–it is your choice. For option 2, I will be gladly to accept any reasonable request, but sometimes life happens and I might not response quickly enough. That leaves you with the third option of extending the custom action yourself. Don’t worry, it is not that hard. In the next example, I will take the ProperIpFormatAction custom action and extend it to include ‘localhost’ as one of the proper IP format:

import argparse
import argparse_actions

class IpAndLocalhostAction(argparse_actions.ProperIpFormatAction):
    def __call__(self, parser, namespace, values, option_string=None):
        # Do our check: allow for 'localhost'
        if values == 'localhost':
            setattr(namespace, self.dest, values)
        else:
            # Super class to perform its check
            parent = super(IpAndLocalhostAction, self)
            parent.__call__(parser, namespace, values, option_string)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Custom Actions')
    parser.add_argument('ip', action=IpAndLocalhostAction)

    try:
        args = parser.parse_args()
        print 'IP is valid: {0}'.format(args.ip)
    except argparse_actions.InvalidIp as e:
        print e

Discussion:

  • The first step is to create a new class (IpAndLocalhostAction), based on an existing custom action (argparse_actions.ProperIpFormatAction, which is really a class itself)
  • Define the function __call__ to override the base custom action with your own logic.

More Ideas

Here are a few ideas I have in mind, which I might implement:

  • Extend ProperIpFormatAction to determine if and IP…
    • Is reachable
    • Provides some services such as HTTP or FTP
    • Belongs to a particular list, such as the banned IP list
  • Extend FolderExistsAction to determine if the folder is…
    • Writable
    • Empty
    • A symbolic link
Release History

Release History

0.4.4

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

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.4.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.4.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.4.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.3.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.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.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.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
argparse_actions-0.4.4.tar.gz (5.5 kB) Copy SHA256 Checksum SHA256 Source Jun 10, 2013

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