def add_subcommand_to_spa( self, func, cmd_cls, spa): # add subcommand parser to _SubParsersAction instance assert spa.__class__ == _SubParsersAction subcmd_name = self.utoh(func.__name__) if subcmd_name in spa._name_parser_map: raise CommandCollectionError('duplicate subcommand: %s' % func.__name__) add_arg_parsers = self.get_add_parsers(func, cmd_cls) help = extract_help(func) parser = spa.add_parser( subcmd_name, # add parser for subcommand prog=self._cmdparser.prog + ' ' + func.__name__, formatter_class=self._cmdparser.formatter_class, description=help.get('short', None), parents=add_arg_parsers) setattr( parser, const.parser_func_attr, func) # for creating option tree after arg parser has been created self.add_args_to_parser(func, cmd_cls, parser) return parser
def add_subcommand_to_spa(self, func, cmd_cls, spa): # add subcommand parser to _SubParsersAction instance assert spa.__class__ == _SubParsersAction subcmd_name = self.utoh(func.__name__) if subcmd_name in spa._name_parser_map: raise CommandCollectionError('duplicate subcommand: %s'%func.__name__) add_arg_parsers = self.get_add_parsers(func, cmd_cls) help = extract_help(func) parser = spa.add_parser(subcmd_name, # add parser for subcommand prog=self._cmdparser.prog + ' ' + func.__name__, formatter_class=self._cmdparser.formatter_class, description=help.get('short', None), parents=add_arg_parsers) setattr(parser, const.parser_func_attr, func) # for creating option tree after arg parser has been created self.add_args_to_parser(func, cmd_cls, parser) return parser
def add_args_to_parser(self, func, cmd_cls, parser, common=False, usn=['h']): # extract function argument information help = extract_help(func) # and add them to parser, # extract help and add it to parser if common: parser.common_args = Node('') argspec = inspect.getargspec(func) if cmd_cls is not None: del argspec.args[0] # remove arg: self in case of a class method if argspec.defaults is not None: # find the offset at which arguments with default values start defaults_offset = len(argspec.args) - len(argspec.defaults) else: defaults_offset = len(argspec.args) used_short_names = list(usn) # store used short names for arguments so as not to repeat them for arg in argspec.args: arg_index = argspec.args.index(arg) default = names = choices = nargs = action = filter = None hidden = False if arg_index >= defaults_offset: # argument has a default value arg_default = argspec.defaults[arg_index - defaults_offset] def get_opt_arg_names(arg, short=None): short = short or self.shorten_arg_name(arg, used_short_names) used_short_names.append(short) names = ['-' + short, '--' + arg] return names if arg_default.__class__ == Arg or issubclass(arg_default.__class__, Arg): filter = make_filter(arg_default) choices = arg_default.choices default = arg_default.default nargs = arg_default.nargs hidden = arg_default.hidden if arg_default.pos: if default is not None: nargs = '?' names = [arg] else: names = get_opt_arg_names(arg, arg_default.short) #if default is None or arg_default.pos: # names = [arg] # positional argument else: names = get_opt_arg_names(arg) default = arg_default if type(default) == bool: if default: action = 'store_false' else: action = 'store_true' default = None else: names = [arg] # positional argument if action is None: kwargs = { 'default' : default, 'choices' : choices, 'help' : help.get(arg, None) } if nargs is not None: kwargs['nargs'] = nargs else: kwargs = { 'action' : action, 'help' : help.get(arg, None) } if len(names) == 1: kwargs['metavar'] = self.utoh(names[0]) else: names = [self.utoh(n) for n in names] action = parser.add_argument(*names, **kwargs) setattr(action, const.action_filter_attr, filter) setattr(action, const.action_hidden_attr, hidden) if not hidden: if not common: self.add_to_optiontree(names, default, choices, filter) else: parser.common_args.add_child(self.make_ot_node(names, default, choices, filter)) # end: for loop longhelp = help.get('long', None) if longhelp is not None and len(longhelp) > 0: parser.set_extrahelp(longhelp) if not common: add_args = getattr(func, const.add_attr, None) add_arg_funcs = add_args.args if add_args is not None else None parser.set_defaults(cmd_func=CmdFunc(cmd_cls, func, add=add_arg_funcs)) # set class and function to be called for execution
def add_args_to_parser(self, func, cmd_cls, parser, common=False, usn=['h']): # extract function argument information help = extract_help(func) # and add them to parser, # extract help and add it to parser if common: parser.common_args = Node('') argspec = inspect.getargspec(func) if cmd_cls is not None: del argspec.args[0] # remove arg: self in case of a class method if argspec.defaults is not None: # find the offset at which arguments with default values start defaults_offset = len(argspec.args) - len(argspec.defaults) else: defaults_offset = len(argspec.args) used_short_names = list( usn ) # store used short names for arguments so as not to repeat them for arg in argspec.args: arg_index = argspec.args.index(arg) default = names = choices = nargs = action = filter = None hidden = False if arg_index >= defaults_offset: # argument has a default value arg_default = argspec.defaults[arg_index - defaults_offset] def get_opt_arg_names(arg, short=None): short = short or self.shorten_arg_name( arg, used_short_names) used_short_names.append(short) names = ['-' + short, '--' + arg] return names if arg_default.__class__ == Arg or issubclass( arg_default.__class__, Arg): filter = make_filter(arg_default) choices = arg_default.choices default = arg_default.default nargs = arg_default.nargs hidden = arg_default.hidden if arg_default.pos: if default is not None: nargs = '?' names = [arg] else: names = get_opt_arg_names(arg, arg_default.short) #if default is None or arg_default.pos: # names = [arg] # positional argument else: names = get_opt_arg_names(arg) default = arg_default if type(default) == bool: if default: action = 'store_false' else: action = 'store_true' default = None else: names = [arg] # positional argument if action is None: kwargs = { 'default': default, 'choices': choices, 'help': help.get(arg, None) } if nargs is not None: kwargs['nargs'] = nargs else: kwargs = {'action': action, 'help': help.get(arg, None)} if len(names) == 1: kwargs['metavar'] = self.utoh(names[0]) else: names = [self.utoh(n) for n in names] action = parser.add_argument(*names, **kwargs) setattr(action, const.action_filter_attr, filter) setattr(action, const.action_hidden_attr, hidden) if not hidden: if not common: self.add_to_optiontree(names, default, choices, filter) else: parser.common_args.add_child( self.make_ot_node(names, default, choices, filter)) # end: for loop longhelp = help.get('long', None) if longhelp is not None and len(longhelp) > 0: parser.set_extrahelp(longhelp) if not common: add_args = getattr(func, const.add_attr, None) add_arg_funcs = add_args.args if add_args is not None else None parser.set_defaults( cmd_func=CmdFunc(cmd_cls, func, add=add_arg_funcs) ) # set class and function to be called for execution