Skip to content

Commit

Permalink
CLAP now supports nested modes
Browse files Browse the repository at this point in the history
  • Loading branch information
Marek Marecki committed Aug 4, 2013
1 parent 21b6e07 commit 43724a6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
16 changes: 16 additions & 0 deletions Changelog.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,24 @@

#### Version 0.7.1 ():

This version is capable of having *nested modes*, e.g. syntax like `foo bar baz --some --fancy options --after --this`.
Such behaviour needed some changes in code to be done and this resulted in `check()` method of `modes.Parser()`
automatically calling define before any actual checking is done.

**Notice**: it's possible that in version 0.7.2 `modes.Parser()` will be renamed to prevent it being mistaken for `parser.Parser()` and
to imporove accuracy of error messages.


* __fix__: fixed bug in `clap.modes.Parser().addOption()` (I forgot to port it to the new version of options)

* __new__: `_append()` method on `clap.modes.Parser()`
* __new__: you can now nest modes,

* __upd__: there is no need to call `define()` before `check()` - the latter automatically calls the former,


----

#### Version 0.7.0 (2013-08-03):

**Warning**: this release is not backwards compatible, you'll need to port your software to it.
Expand Down
13 changes: 9 additions & 4 deletions clap/modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@
class Parser():
"""Object implementing modes functionality.
"""
def __init__(self, argv, default=''):
self.argv = []
def __init__(self, argv=[], default=''):
self.argv = argv
self.modes = {'': parser.Parser()}
self.mode = ''
self.default = default
self.parser = None
self.feed(argv)

def __contains__(self, option):
"""If you check whether Parser() contains an option or not, general one and every mode-parser
Expand All @@ -60,6 +59,11 @@ def feed(self, argv):
self.parser = None
self.mode = ''

def _append(self, option):
"""Appends option to sub-parsers.
"""
for name in self.modes: self.modes[name]._append(option)

def addOption(self, short='', long='', arguments=[], requires=[], needs=[], required=False, not_with=[], conflicts=[]):
"""Adds an option to the list of options recognized by parser.
Available types are: int, float and str.
Expand All @@ -70,7 +74,7 @@ def addOption(self, short='', long='', arguments=[], requires=[], needs=[], requ
requires=requires, needs=needs,
required=required, not_with=not_with,
conflicts=conflicts)
for name in self.modes: self.modes[name]._append(new)
self._append(new)
return new

def addMode(self, name, parser):
Expand Down Expand Up @@ -117,6 +121,7 @@ def define(self):
def check(self):
"""Checks input list for errors.
"""
self.define()
self.parser.check()

def parse(self):
Expand Down
1 change: 1 addition & 0 deletions clap/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def check(self):
"""Checks if input list is valid for this instance of Parser().
Run before `parse()` to check for errors in input list.
"""

checker.Checker(self).check()

def parse(self):
Expand Down

0 comments on commit 43724a6

Please sign in to comment.