def generate(self, more_content=None, real_modname=None, check_module=False, all_members=False): if not self.parse_name(): # need a module to import self.directive.warn( 'don\'t know which module to import for autodocumenting ' '%r (try placing a "module" or "currentmodule" directive ' 'in the document, or giving an explicit module name)' % self.name) return # now, import the module and get object to document if not self.import_object(): return if not (type(self.object).__name__ == 'function' and self.object.__module__ == 'charms.reactive.cli'): return super(CLIDoc, self).generate(more_content, real_modname, check_module, all_members) parser = argparse.ArgumentParser() parser.prog = 'charms.reactive {}'.format(self.object_name) for args, kwargs in describe_arguments(self.object): parser.add_argument(*args, **kwargs) usage = parser.format_usage() usage = re.sub('usage: (\S+) (\S+) (.*)', r'\1 **\2** `\3`', usage) self.add_line(usage, '<clidoc>') self.add_line('', '<clidoc>') self.indent += ' ' self.add_content(more_content)
def test_keyword_splat_missing(self): """Double-splat arguments can't be represented in the current version of commandant.""" args = cli.describe_arguments(lambda x, y=3, *z, **missing: False) for opts, _ in args: # opts should be ('varname',) at this point self.assertTrue(len(opts) == 1) self.assertNotIn('missing', opts)
def test_varargs(self): """Function has a splat-operator parameter to catch an arbitrary number of positional parameters.""" argparams = tuple(cli.describe_arguments(lambda x, y=3, *z: False)) self.assertEqual(3, len(argparams)) for argspec in ((('x', ), {}), (('--y', ), { "default": 3 }), (('z', ), { "nargs": "*" })): self.assertIn(argspec, argparams)
def test_keyword_arguments(self): """Function has optional parameters with default values.""" argparams = tuple( cli.describe_arguments(lambda x, y=3, z="bar": False)) self.assertEqual(3, len(argparams)) for argspec in ((('x', ), {}), (('--y', ), { "default": 3 }), (('--z', ), { "default": "bar" })): self.assertIn(argspec, argparams)
def test_positional_arguments(self): """Finite number of order-dependent required arguments.""" argparams = tuple(cli.describe_arguments(lambda x, y, z: False)) self.assertEqual(3, len(argparams)) for argspec in ((('x', ), {}), (('y', ), {}), (('z', ), {})): self.assertIn(argspec, argparams)