Tutorial: command in output_for_cli() example now also takes an argument

This commit is contained in:
Jason Gerard DeRose
2008-11-14 14:27:09 -07:00
parent c974451edf
commit 0f1ed3e904

View File

@@ -572,65 +572,81 @@ signature:
For example, say we setup a command like this: For example, say we setup a command like this:
>>> class show_items(Command): >>> class show_items(Command):
...
... takes_args = ['key?']
...
... takes_options = [Param('reverse', type=Bool(), default=False)] ... takes_options = [Param('reverse', type=Bool(), default=False)]
... ...
... def execute(self, **options): ... def execute(self, key, **options):
... items = [ ... items = dict(
... ('apple', 'fruit'), ... fruit='apple',
... ('dog', 'pet'), ... pet='dog',
... city='Berlin',
... )
... if key in items:
... return [(key, items[key])]
... return [
... (k, items[k]) for k in sorted(items, reverse=options['reverse'])
... ] ... ]
... if options['reverse']:
... items.reverse()
... return items
... ...
... def output_for_cli(self, textui, result, **options): ... def output_for_cli(self, textui, result, key, **options):
... textui.print_name(self.name) ... if key is not None:
... textui.print_keyval(result) ... textui.print_keyval(result)
... format = '%d items' ... else:
... if options['reverse']: ... textui.print_name(self.name)
... format += ' (in reverse order)' ... textui.print_keyval(result)
... textui.print_count(result, format) ... format = '%d items'
... if options['reverse']:
... format += ' (in reverse order)'
... textui.print_count(result, format)
... ...
>>> api = create_api() >>> api = create_api()
>>> api.env.in_server = True # We want to execute, not forward. >>> api.env.in_server = True # We want to execute, not forward.
>>> api.register(show_items) >>> api.register(show_items)
>>> api.finalize() >>> api.finalize()
Normally `cli.CLI.load_plugins()` will register the `cli.textui` plugin, but for Normally when you invoke the ``ipa`` script, `cli.CLI.load_plugins()` will
the sake of our example, we'll just create an instance here: register the `cli.textui` backend plugin, but for the sake of our example,
we just create an instance here:
>>> from ipalib import cli >>> from ipalib import cli
>>> textui = cli.textui() # We'll pass this to output_for_cli() >>> textui = cli.textui() # We'll pass this to output_for_cli()
For what we are concerned with in this example, calling your command through Now for what we are concerned with in this example, calling your command
the ``ipa`` script basically will do the following: through the ``ipa`` script basically will do the following:
>>> options = dict(reverse=False) >>> result = api.Command.show_items()
>>> result = api.Command.show_items(**options) >>> api.Command.show_items.output_for_cli(textui, result, None, reverse=False)
>>> api.Command.show_items.output_for_cli(textui, result, **options)
----------- -----------
show-items: show-items:
----------- -----------
apple = 'fruit' city = 'Berlin'
dog = 'pet' fruit = 'apple'
pet = 'dog'
------- -------
2 items 3 items
------- -------
Similarly, calling it with ``reverse=True`` would result in the following: Similarly, calling it with ``reverse=True`` would result in the following:
>>> options = dict(reverse=True) >>> result = api.Command.show_items(reverse=True)
>>> result = api.Command.show_items(**options) >>> api.Command.show_items.output_for_cli(textui, result, None, reverse=True)
>>> api.Command.show_items.output_for_cli(textui, result, **options)
----------- -----------
show-items: show-items:
----------- -----------
dog = 'pet' pet = 'dog'
apple = 'fruit' fruit = 'apple'
city = 'Berlin'
-------------------------- --------------------------
2 items (in reverse order) 3 items (in reverse order)
-------------------------- --------------------------
Lastly, providing a ``key`` would result in the following:
>>> result = api.Command.show_items('city')
>>> api.Command.show_items.output_for_cli(textui, result, 'city', reverse=False)
city = 'Berlin'
See the `ipalib.cli.textui` plugin for a description of its methods. See the `ipalib.cli.textui` plugin for a description of its methods.