From a33585a2a3c6d7923659ba9bc1e4bd7b051a955a Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 17 Feb 2014 07:33:19 +0100 Subject: python version (too be documented and implemented in other versions): standard_abbreviations: if all expansions are synonyms, accept it; the standard argument is used MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/argparser.py | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/argparser.py b/src/argparser.py index 9baa507..92fe798 100644 --- a/src/argparser.py +++ b/src/argparser.py @@ -56,12 +56,12 @@ class ArgParser(): Constructor. The short description is printed on same line as the program name - @param description:str Short, single-line, description of the program - @param usage:str? Formated, multi-line, usage text, may be `None` - @param longdescription:str? Long, multi-line, description of the program, may be `None` - @param program:str? The name of the program, `None` for automatic - @param usestderr:bool Whether to use stderr instead of stdout - @param abbreviations:(str, itr)?→str? Function that expands abbrevatied options + @param description:str Short, single-line, description of the program + @param usage:str? Formated, multi-line, usage text, may be `None` + @param longdescription:str? Long, multi-line, description of the program, may be `None` + @param program:str? The name of the program, `None` for automatic + @param usestderr:bool Whether to use stderr instead of stdout + @param (opt:str, opts:list, mapping:dict?=None)→str? Function that expands abbrevatied options ''' self.linuxvt = ('TERM' in os.environ) and (os.environ['TERM'] == 'linux') self.program = sys.argv[0] if program is None else program @@ -72,8 +72,13 @@ class ArgParser(): self.opts = {} self.optmap = {} self.__out = sys.stderr.buffer if usestderr else sys.stdout.buffer - self.abbreviations = abbreviations if abbreviations is not None else lambda arg, candidates : None - self.__abbreviations = lambda arg : self.abbreviations(arg, list(self.optmap.keys())) + self.abbreviations = abbreviations if abbreviations is not None else lambda arg, candidates, mapping = None : None + def get_mapping(map_): + rc = {} + for key in map_.keys(): + rc[key] = map_[key][0] + return rc + self.__abbreviations = lambda arg : self.abbreviations(arg, list(self.optmap.keys()), get_mapping(self.optmap)) @staticmethod @@ -81,10 +86,22 @@ class ArgParser(): ''' Gets the standard abbrevation expender - @return (str, list)→str? The standard abbrevation expender + @return :(opt:str, opts:list, mapping:dict?=None)→str? The standard abbrevation expender ''' + def uniq(items): + if len(items) < 2: + return items + items = sorted(items) + rc = items[:1] + for elem in items[1:]: + if not elem == rc[-1]: + rc.append(elem) + return rc one = lambda arg : arg[0] if len(arg) == 1 else None - return lambda arg, candidates : one(list(filter(lambda a : a.startswith(arg), candidates))) + lfilter = lambda f, l : list(filter(f, l)) + map_ = lambda mapping, args : args if mapping is None else [mapping[a] for a in args] + test = lambda arg : lambda a : a.startswith(arg) + return lambda arg, candidates, mapping = None : one(uniq(map_(mapping, lfilter(test(arg), candidates)))) @staticmethod -- cgit v1.2.3-70-g09d2