Since I seem to be unable to override __version__ in a Doctest,
we'll pass the version tuple in as an argument. You can ignore
- ``version_tuple``.
+ `version_tuple`.
>>> v = (1, 2, 3, 'devel', '20100501', 'Kenzo')
"""
import Queue as queue
+import textwrap
class CommandExit (Exception):
Command: test
<BLANKLINE>
Arguments:
+ <BLANKLINE>
help BOOL (bool) Print a help message.
<BLANKLINE>
An example Command.
name_part += ' (%s)' % ', '.join(self.aliases)
parts = [name_part]
if len(self.arguments) > 0:
- argument_part = ['Arguments:'] + [a.help() for a in self.arguments]
+ argument_part = ['Arguments:', '']
+ for a in self.arguments:
+ argument_part.append(textwrap.fill(
+ a.help(),
+ initial_indent="",
+ subsequent_indent=" "))
argument_part = '\n'.join(argument_part)
parts.append(argument_part)
parts.append(self._help) # help part
name='cut',
arguments=[
Argument(name='curve', type='curve', optional=False, help="""
-:class:``hooke.curve.Curve`` to cut from.
+:class:`hooke.curve.Curve` to cut from.
""".strip()),
Argument(name='block', aliases=['set'], type='int', default=0,
help="""
arguments=[
Argument(name='playlist', type='playlist', optional=False,
help="""
-:class:``hooke.plugin.playlist.Playlist`` to act on.
+:class:`hooke.plugin.playlist.Playlist` to act on.
""".strip()),
],
help=self.__doc__)
arguments=[
Argument(name='playlist', type='playlist', optional=False,
help="""
-:class:``hooke.plugin.playlist.Playlist`` to act on.
+:class:`hooke.plugin.playlist.Playlist` to act on.
""".strip()),
],
help=self.__doc__)
arguments=[
Argument(name='playlist', type='playlist', optional=False,
help="""
-:class:``hooke.plugin.playlist.Playlist`` to act on.
+:class:`hooke.plugin.playlist.Playlist` to act on.
""".strip()),
Argument(name='index', type='int', optional=False, help="""
Index of target curve.
arguments=[
Argument(name='playlist', type='playlist', optional=False,
help="""
-:class:``hooke.plugin.playlist.Playlist`` to act on.
+:class:`hooke.plugin.playlist.Playlist` to act on.
""".strip()),
Argument(name='output', type='file',
help="""
arguments=[
Argument(name='playlist', type='playlist', optional=False,
help="""
-:class:``hooke.plugin.playlist.Playlist`` to act on.
+:class:`hooke.plugin.playlist.Playlist` to act on.
""".strip()),
Argument(name='input', type='file', optional=False,
help="""
arguments=[
Argument(name='playlist', type='playlist', optional=False,
help="""
-:class:``hooke.plugin.playlist.Playlist`` to act on.
+:class:`hooke.plugin.playlist.Playlist` to act on.
""".strip()),
Argument(name='input', type='glob', optional=False,
help="""
arguments=[
Argument(name='playlist', type='playlist', optional=False,
help="""
-:class:``hooke.plugin.playlist.Playlist`` to act on.
+:class:`hooke.plugin.playlist.Playlist` to act on.
""".strip()),
Argument(name='index', type='int', optional=False, help="""
Index of target curve.
arguments=[
Argument(name='playlist', type='playlist', optional=False,
help="""
-:class:``hooke.plugin.playlist.Playlist`` to act on.
+:class:`hooke.plugin.playlist.Playlist` to act on.
""".strip()),
Argument(name='filter', type='function', optional=False,
help="""
"""
import cmd
-import readline
+import optparse
+import readline # including readline makes cmd.Cmd.cmdloop() smarter
from ..command import CommandExit, Command, Argument
from ..ui import UserInterface, CommandMessage
+
# Define a few helper classes. The .__call__ methods of these
# functions will provide the do_*, help_*, and complete_* methods of
# HookeCmd.
def __call__(self, *args, **kwargs):
raise NotImplementedError
+def command_parser(command):
+ p = optparse.OptionParser()
+ args = []
+ for a in command.arguments:
+ if a.name == 'help':
+ continue # 'help' is a default OptionParser option
+ if a.optional == True:
+ p.add_option(
+ '--%s' % a.name.replace('_', '-'),
+ dest=a.name, default=a.default)
+ else:
+ args.append((a.name.replace('_', '-'), a))
+ return (p, args)
+
class DoCommand (CommandMethod):
+ def __init__(self, *args, **kwargs):
+ super(DoCommand, self).__init__(*args, **kwargs)
+ self.parser,self.args = command_parser(self.command)
+
def __call__(self, args):
args = self._parse_args(self.command, args)
self.cmd.inqueue.put(CommandMessage(self.command, args))
return {}
class HelpCommand (CommandMethod):
+ def __init__(self, *args, **kwargs):
+ super(HelpCommand, self).__init__(*args, **kwargs)
+ self.parser,self.args = command_parser(self.command)
+
def __call__(self):
blocks = [self.command.help(),
'------',
return self.command.help()
def _usage_string(self):
- return ' '.join([self.command.name, '[args]'])
+ if len(self.args) == len(self.command.arguments):
+ options_string = ''
+ else:
+ options_string = '[options]'
+ arg_string = ' '.join([name for name,arg in self.args])
+ return ' '.join([x for x in [self.command.name,
+ options_string,
+ arg_string]
+ if x != ''])
class CompleteCommand (CommandMethod):
def __call__(self, text, line, begidx, endidx):