8000 GitHub - kkinder/argfarce
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Feb 11, 2022. It is now read-only.

kkinder/argfarce

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages

0