From 75c50e5e5d66a8afa5f90f0836c41ecc440d52a8 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Mon, 10 May 2010 09:43:55 -0400 Subject: [PATCH] Added command parser to hooke.ui.commandline for argument listing. --- hooke/__init__.py | 2 +- hooke/command.py | 9 ++++++++- hooke/plugin/cut.py | 2 +- hooke/plugin/playlist.py | 16 ++++++++-------- hooke/ui/commandline.py | 36 ++++++++++++++++++++++++++++++++++-- 5 files changed, 52 insertions(+), 13 deletions(-) diff --git a/hooke/__init__.py b/hooke/__init__.py index b90ff3d..283d0dd 100644 --- a/hooke/__init__.py +++ b/hooke/__init__.py @@ -40,7 +40,7 @@ def version(depth=-1, version_tuple=None): 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') diff --git a/hooke/command.py b/hooke/command.py index bfa4c9f..97d8ea5 100644 --- a/hooke/command.py +++ b/hooke/command.py @@ -3,6 +3,7 @@ """ import Queue as queue +import textwrap class CommandExit (Exception): @@ -24,6 +25,7 @@ class Command (object): Command: test Arguments: + help BOOL (bool) Print a help message. An example Command. @@ -101,7 +103,12 @@ class Command (object): 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 diff --git a/hooke/plugin/cut.py b/hooke/plugin/cut.py index c1d8ca9..15d6bbe 100644 --- a/hooke/plugin/cut.py +++ b/hooke/plugin/cut.py @@ -23,7 +23,7 @@ class CutCommand (Command): 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=""" diff --git a/hooke/plugin/playlist.py b/hooke/plugin/playlist.py index 78ad4eb..44f18bd 100644 --- a/hooke/plugin/playlist.py +++ b/hooke/plugin/playlist.py @@ -29,7 +29,7 @@ class NextCommand (Command): 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__) @@ -46,7 +46,7 @@ class PreviousCommand (Command): 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__) @@ -63,7 +63,7 @@ class JumpCommand (Command): 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. @@ -83,7 +83,7 @@ class SaveCommand (Command): 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=""" @@ -129,7 +129,7 @@ class AddCommand (Command): 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=""" @@ -159,7 +159,7 @@ class AddGlobCommand (Command): 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=""" @@ -185,7 +185,7 @@ class RemoveCommand (Command): 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. @@ -210,7 +210,7 @@ class FilterCommand (Command): 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=""" diff --git a/hooke/ui/commandline.py b/hooke/ui/commandline.py index 02174c4..cab4296 100644 --- a/hooke/ui/commandline.py +++ b/hooke/ui/commandline.py @@ -3,11 +3,13 @@ line. """ 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. @@ -20,7 +22,25 @@ class CommandMethod (object): 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)) @@ -35,6 +55,10 @@ class DoCommand (CommandMethod): 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(), '------', @@ -46,7 +70,15 @@ class HelpCommand (CommandMethod): 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): -- 2.26.2