caipyrinha Module

Abstraction layer over argparse.

Caipyrinha added 2 methods to argparse.ArgumentParser, callback and parse_wc. Also a Caipyrinha instance is callable and is equal to use parse_wc method.

Example of use:


import caipyrinha

parser = caipyrinha.Caipyrinha(prog="Your Program")
parser.add_argument("--version", action='version', version="%(prog)s 0.1")

@parser.callback(exit=0, exclusive="group1")
def first(flags, returns):
    '''Execute this option and exit'''
    print "bye bye"

def second(flags, returns):
    '''set his own return value with his parameter'''
    return flags.second

@parser.callback("--third", exclusive="group1")
def no_used_name(flags, returns):
    '''you cant use this argument with first'''
    print returns.second

import sys


$ python --help
usage: Your Program [-h] [--version] [--first] [--second SECOND] [--third]

optional arguments:
  -h, --help       show this help message and exit
  --version        show program's version number and exit
  --first          Execute this option and exit
  --second SECOND  set his own return value with his parameter
  --third          you cant use this argument with first
$ python --first
bye bye
$ python --first --second "hello from second"
bye bye
$ python --first --second "hello from second" --third
usage: Your Program [-h] [--version] [--first] [--second SECOND] [--third]
Your Program: error: argument --third: not allowed with argument --first
$ python --second "hello from second" --third
hello from second
class caipyrinha.Caipyrinha(*args, **kwargs)

Bases: argparse.ArgumentParser

Easy argument parser in the top of argparse

add_mutually_exclusive_group(name, required=False)

Add a muttually exclusive group as argparse.Argparse but identified by a name. If the name already exists the same group is returned. If name exists and required is diferent this method change the required value.

callback(*args, **kwargs)

Decorator for create a new argument and add the function decorated ass a callback if the command line argument exists.

IMPORTANT: The callbacks are called in the same order as you declared it.

Support the same *args and **kwargs of add_argument method. Also support the exclude parameter if you want to set the callback in a mutually exclusive group, and exit (must be an int and the default is None) if you want to exit after execute this callback using this value as exit code.

The function to be decorated must accept 2 arguments flags (the flag status of the parser) and returns (the return values of the previous callbacks)

If you dont set the long (--name) or short (-n) option, the name of the function is used as long option. The action by default is store if nargs params is setted otherwise store_true.

for more information please see the argparse documentation


Parse the arguments and execute the callbacks.

Returns:The flags and the returns of all the callbacks.

Project Versions

Previous topic

Installation Guide

This Page