"""Implement a command line syntax for a
:class:`hooke.command.Command`.
"""
- def __init__(self, command):
- optparse.OptionParser.__init__(self, prog=command._cl_name)
+ def __init__(self, command, name_fn):
+ optparse.OptionParser.__init__(self, prog=name_fn(command.name))
self.command = command
self.command_opts = []
self.command_args = []
if a.name == 'help':
continue # 'help' is a default OptionParser option
if a.optional == True:
+ name = name_fn(a.name)
self.add_option(
- '--%s' % a._cl_name, dest=a._cl_name, default=a.default)
+ '--%s' % name, dest=name, default=a.default)
self.command_opts.append(a)
else:
self.command_args.append(a)
provide the `do_*`, `help_*`, and `complete_*` methods of
:class:`HookeCmd`.
"""
- def __init__(self, cmd, command):
+ def __init__(self, cmd, command, name_fn):
self.cmd = cmd
self.command = command
+ self.name_fn = name_fn
def __call__(self, *args, **kwargs):
raise NotImplementedError
class DoCommand (CommandMethod):
def __init__(self, *args, **kwargs):
super(DoCommand, self).__init__(*args, **kwargs)
- self.parser = CommandLineParser(self.command)
+ self.parser = CommandLineParser(self.command, self.name_fn)
def __call__(self, args):
try:
- args = self._parse_args(self.command, args)
+ args = self._parse_args(args)
except optparse.OptParseError, e:
self.cmd.stdout.write(str(e).lstrip()+'\n')
self.cmd.stdout.write('Failure\n')
break
self.cmd.stdout.write(str(msg).rstrip()+'\n')
- def _parse_args(self, command, args):
+ def _parse_args(self, args):
argv = shlex.split(args, comments=True, posix=True)
options,args = self.parser.parse_args(argv)
if len(args) != len(self.parser.command_args):
raise optparse.OptParseError('%d arguments given, but %s takes %d'
- % (len(args), command._cl_name,
+ % (len(args),
+ self.name_fn(self.command.name),
len(self.parser.command_args)))
params = {}
for argument in self.parser.command_opts:
- params[argument.name] = getattr(options, argument._cl_name)
+ params[argument.name] = getattr(options,
+ self.name_fn(argument.name))
for i,argument in enumerate(self.parser.command_args):
params[argument.name] = args[i]
return params
class HelpCommand (CommandMethod):
def __init__(self, *args, **kwargs):
super(HelpCommand, self).__init__(*args, **kwargs)
- self.parser = CommandLineParser(self.command)
+ self.parser = CommandLineParser(self.command, self.name_fn)
def __call__(self):
- blocks = [self.command.help(),
+ blocks = [self.command.help(name_fn=self.name_fn),
'------',
'Usage: ' + self._usage_string(),
'']
self.cmd.stdout.write('\n'.join(blocks))
def _message(self):
- return self.command.help()
+ return self.command.help(name_fn=self.name_fn)
def _usage_string(self):
if len(self.parser.command_opts) == 0:
else:
options_string = '[options]'
arg_string = ' '.join(
- [arg._cl_name for arg in self.parser.command_args])
- return ' '.join([x for x in [self.command.name,
+ [self.name_fn(arg.name) for arg in self.parser.command_args])
+ return ' '.join([x for x in [self.parser.prog,
options_string,
arg_string]
if x != ''])
self.inqueue = inqueue
self.outqueue = outqueue
- def _safe_name(self, name):
+ def _name_fn(self, name):
return name.lower().replace(' ', '_')
def _add_command_methods(self):
for command in self.commands:
- self._setup_command(command)
- for name in [command._cl_name] + command._cl_aliases:
+ for name in [command.name] + command.aliases:
+ name = self._name_fn(name)
setattr(self.__class__, 'do_%s' % name,
- DoCommand(self, command))
+ DoCommand(self, command, self._name_fn))
setattr(self.__class__, 'help_%s' % name,
- HelpCommand(self, command))
+ HelpCommand(self, command, self._name_fn))
setattr(self.__class__, 'complete_%s' % name,
- CompleteCommand(self, command))
+ CompleteCommand(self, command, self._name_fn))
exit_command = Command(
name='exit', aliases=['quit', 'EOF'],
help='Exit Hooke cleanly.')
- self._setup_command(exit_command)
exit_command.arguments = [] # remove help argument
for name in [exit_command.name] + exit_command.aliases:
setattr(self.__class__, 'do_%s' % name,
lambda self, args : True)
# the True return stops .cmdloop execution
setattr(self.__class__, 'help_%s' % name,
- HelpCommand(self, exit_command))
+ HelpCommand(self, exit_command, self._name_fn))
help_command = Command(
name='help',
Argument(name='command', type='string', optional=True,
help='The name of the command you want help with.')
]
- self._setup_command(help_command)
setattr(self.__class__, 'help_help',
- HelpCommand(self, help_command))
-
- def _setup_command(self, command):
- """Attach some UI specific data to `command`. After this
- point, the :attr:`commands` are read only.
- """
- command._cl_name = self._safe_name(command.name)
- command._cl_aliases = [self._safe_name(n) for n in command.aliases]
- for argument in command.arguments:
- argument._cl_name = self._safe_name(argument.name)
- argument._cl_aliases = [self._safe_name(n)
- for n in argument.aliases]
+ HelpCommand(self, help_command, self._name_fn))
class CommandLine (UserInterface):