Giter Site home page Giter Site logo

argfarce's Introduction

argfarce

Argfarce makes it easy to declare argparse structures. Consider this example:

    >>> class PersonalInfoParser(ArgumentParser):
    ...     class Meta:
    ...         prog = 'person.py'
    ...     
    ...     profession = Argument('-p', '--profession', choices=('developer', 'programmer', 'software engineer'), help="These are all pretty much the same")
    ...     action = Argument('-a', '--action')
    ...     name = Argument('full_name')
    ...     comments = Argument(nargs='*')
    ... 
    >>> parser = PersonalInfoParser()
    >>> parser.parse_args('-p programmer -a salute Ken foo bar spam'.split())
    >>> print parser.action
    salute
    >>> print parser.name
    Ken
    >>> print parser.profession
    programmer
    >>> print parser.comments
    ['foo', 'bar', 'spam']

Argfarce will also work with subparsers and subparser level calls:

    >>> class SandwichParser(ArgumentParser):
    ...     class Meta:
    ...         prog = 'sandwich.py'
    ...         subparser_help = 'Use one of the following commands:'
    ...     
    ...     class MakeSandwichParser(ArgumentParser):
    ...         class Meta:
    ...             subparser_argument = 'make'
    ...             call = 'func'
    ...             
    ...         cheese = Argument('-c', '--use-cheese', choices=('cheddar', 'provolone', 'swiss'), help="Cheese to use on your sandwich")
    ...         protein = Argument('-p', '--use-protein', choices=('tempeh', 'chicken', 'beef'), help="Protein for your meal")
    ...         extras = Argument('-x', '--extras', nargs="+", choices=('lettuce', 'tomato', 'olives', 'peppers', 'pickles', 'oil'), help="Fixings you want")
    ...     
    ...         def func(self, args):
    ...             return args.cheese.upper()
    ...
    ...
    ...     class EatSandwichParser(ArgumentParser):
    ...         class Meta:
    ...             subparser_argument = 'eat'
    ...             call = 'func'
    ...             
    ...         speed = Argument('-s', choices=('fast', 'slow'), help="How fast to eat the sandwich")
    ...
    ...         def func(self, args):
    ...             if args.speed == 'fast':
    ...                 return 'speed of light!'
    ...             else:
    ...                 return 'slow motion'
    ... 
    >>> p = SandwichParser()
    >>> p.parse_args('make -c swiss -p beef -x lettuce tomato olives'.split())
    >>> print p.cheese
    swiss
    >>> print p.protein
    beef
    >>> print p.extras
    ['lettuce', 'tomato', 'olives']
    >>> print p.call(p)
    SWISS
    >>> p.parse_args('eat -s fast'.split())
    >>> print p.speed
    fast
    >>> print p.call(p)
    speed of light!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.