From 43724a600f47750a1f1421f2516655d0627e957c Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Sun, 4 Aug 2013 03:51:50 +0200 Subject: [PATCH] CLAP now supports nested modes --- Changelog.markdown | 16 ++++++++++++++++ clap/modes.py | 13 +++++++++---- clap/parser.py | 1 + 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/Changelog.markdown b/Changelog.markdown index 5012f88..d84a088 100644 --- a/Changelog.markdown +++ b/Changelog.markdown @@ -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. diff --git a/clap/modes.py b/clap/modes.py index 0d6bf07..ad5c876 100644 --- a/clap/modes.py +++ b/clap/modes.py @@ -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 @@ -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. @@ -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): @@ -117,6 +121,7 @@ def define(self): def check(self): """Checks input list for errors. """ + self.define() self.parser.check() def parse(self): diff --git a/clap/parser.py b/clap/parser.py index f04953a..789842f 100644 --- a/clap/parser.py +++ b/clap/parser.py @@ -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):