From 15eb20ae11d800f19ac6e11ae462100dfd46f0df Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Mon, 28 Jan 2019 17:05:50 +0100 Subject: [PATCH] positional arguments works well --- README.md | 5 +++-- tiramisu_cmdline_parser.py | 21 +++++++++++++-------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 369e649..302b8b7 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,8 @@ from tiramisu import IntOption, StrOption, BoolOption, ChoiceOption, \ choiceoption = ChoiceOption('cmd', 'choice the sub argument', ('str', 'list', 'int'), - properties=('mandatory',)) -# 'positional')) + properties=('mandatory', + 'positional')) # * a boolean to pass script in verbosity mode with argument -v --verbosity booloption = BoolOption('verbosity', 'increase output verbosity', @@ -60,6 +60,7 @@ config = Config(OptionDescription('root', int_ ])) # initialise the parser +config.property.read_write() parser = TiramisuCmdlineParser(config) # parse arguments of current script parser.parse_args() diff --git a/tiramisu_cmdline_parser.py b/tiramisu_cmdline_parser.py index 31cc8e5..9401ed0 100644 --- a/tiramisu_cmdline_parser.py +++ b/tiramisu_cmdline_parser.py @@ -166,16 +166,17 @@ class TiramisuCmdlineParser(ArgumentParser): if name.startswith(self.prefix_chars): raise ValueError('name cannot startswith "{}"'.format(self.prefix_chars)) properties = obj.property.get() - kwargs = {'help': option.doc().replace('%', '%%'), - 'default': SUPPRESS, - 'dest': option.path()} + kwargs = {'help': option.doc().replace('%', '%%')} if 'positional' in properties: #if not 'mandatory' in properties: # raise ValueError('"positional" argument must be "mandatory" too') - args = [name] + args = [option.path()] #if option.requires(): + kwargs['default'] = obj.value.get() kwargs['nargs'] = '?' else: + kwargs['dest'] = option.path() + kwargs['default'] = SUPPRESS if self.fullpath and prefix: name = prefix + '.' + name args = [self._gen_argument(name, properties)] @@ -240,11 +241,15 @@ class TiramisuCmdlineParser(ArgumentParser): self.error('unrecognized arguments: {}'.format(name)) if valid_mandatory: for key in self.config.value.mandatory(): - if self.fullpath or '.' not in key: - name = key + properties = self.config.option(key).property.get() + if 'positional' not in properties: + if self.fullpath or '.' not in key: + name = key + else: + name = key.rsplit('.', 1)[1] + args = self._gen_argument(name, self.config.option(key).property.get()) else: - name = key.rsplit('.', 1)[1] - args = self._gen_argument(name, self.config.option(key).property.get()) + args = key self.error('the following arguments are required: {}'.format(args)) return namespaces