diff options
author | Mattias Andrée <maandree@operamail.com> | 2013-08-26 21:29:41 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2013-08-26 21:29:41 +0200 |
commit | d878ecbe47634d210af716edbdffffa0f8b2b34d (patch) | |
tree | fd079e090f048318357a220c62f52589b48d79f2 /src | |
parent | update pkgbuild (diff) | |
download | argparser-d878ecbe47634d210af716edbdffffa0f8b2b34d.tar.gz argparser-d878ecbe47634d210af716edbdffffa0f8b2b34d.tar.bz2 argparser-d878ecbe47634d210af716edbdffffa0f8b2b34d.tar.xz |
issue 7, python version
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/argparser.py | 38 | ||||
-rwxr-xr-x | src/test.py | 3 |
2 files changed, 32 insertions, 9 deletions
diff --git a/src/argparser.py b/src/argparser.py index 39bc584..fcdad91 100644 --- a/src/argparser.py +++ b/src/argparser.py @@ -51,16 +51,17 @@ class ArgParser(): ''' - def __init__(self, description, usage, longdescription = None, program = None, usestderr = False): + def __init__(self, description, usage, longdescription = None, program = None, usestderr = False, abbreviations = None): ''' 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 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>)?→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 @@ -71,6 +72,19 @@ 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, self.optmap.keys()) + + + @staticmethod + def standard_abbreviations(): + ''' + Gets the standard abbrevation expender + + @return (str, itr<str>)→str? The standard abbrevation expender + ''' + one = lambda arg : arg[0] if len(arg) == 1 else None + return lambda arg, candidates : one(list(filter(lambda a : a.startswith(arg), candidates))) @staticmethod @@ -278,7 +292,11 @@ class ArgParser(): else: self.optmap[arg_opt][2](arg_opt, self.optmap[arg_opt][0], argqueue[-1]) else: - unrecognised(arg) + _arg = self.__abbreviations(arg) + if _arg is None: + unrecognised(arg) + else: + queue[0:0] = [_arg] elif (arg in self.optmap) and (self.optmap[arg][1] <= ArgParser.OPTARGUMENTED): optqueue.append(arg) get += 1 @@ -288,7 +306,11 @@ class ArgParser(): argqueue.append(None) dashed = True else: - unrecognised(arg) + _arg = self.__abbreviations(arg) + if _arg is None: + unrecognised(arg) + else: + queue[0:0] = [_arg] else: sign = arg[0] i = 1 diff --git a/src/test.py b/src/test.py index 26e9ff3..0cfd242 100755 --- a/src/test.py +++ b/src/test.py @@ -37,7 +37,8 @@ parser = ArgParser('A test for argparser', 'test [options] [files]', 'GNU Affero General Public License for more details.\n' '\n' 'You should have received a copy of the GNU Affero General Public License\n' - 'along with this library. If not, see <http://www.gnu.org/licenses/>.', None, True) + 'along with this library. If not, see <http://www.gnu.org/licenses/>.', + None, True, ArgParser.standard_abbreviations()) parser.add_argumentless(['-h', '-?', '--help'], 0, 'Prints this help message\n(and exits)') parser.add_argumentless(['--hello'], 0, 'Prints the text: hello world') |