VirtualMailManager/cli/subcommands.py
changeset 606 ab97727357a4
parent 602 af88f01e5bfa
child 607 46454ff9d441
equal deleted inserted replaced
605:b994444f7dee 606:ab97727357a4
    14 from textwrap import TextWrapper
    14 from textwrap import TextWrapper
    15 from time import strftime, strptime
    15 from time import strftime, strptime
    16 
    16 
    17 from VirtualMailManager import ENCODING
    17 from VirtualMailManager import ENCODING
    18 from VirtualMailManager.cli import get_winsize, prog, w_err, w_std
    18 from VirtualMailManager.cli import get_winsize, prog, w_err, w_std
       
    19 from VirtualMailManager.cli.clihelp import help_msgs
    19 from VirtualMailManager.common import human_size, size_in_bytes, \
    20 from VirtualMailManager.common import human_size, size_in_bytes, \
    20      version_str, format_domain_default
    21      version_str, format_domain_default
    21 from VirtualMailManager.constants import __copyright__, __date__, \
    22 from VirtualMailManager.constants import __copyright__, __date__, \
    22      __version__, ACCOUNT_EXISTS, ALIAS_EXISTS, ALIASDOMAIN_ISDOMAIN, \
    23      __version__, ACCOUNT_EXISTS, ALIAS_EXISTS, ALIASDOMAIN_ISDOMAIN, \
    23      DOMAIN_ALIAS_EXISTS, INVALID_ARGUMENT, EX_MISSING_ARGS, \
    24      DOMAIN_ALIAS_EXISTS, INVALID_ARGUMENT, EX_MISSING_ARGS, \
    46 
    47 
    47 
    48 
    48 class Command(object):
    49 class Command(object):
    49     """Container class for command information."""
    50     """Container class for command information."""
    50     __slots__ = ('name', 'alias', 'func', 'args', 'descr')
    51     __slots__ = ('name', 'alias', 'func', 'args', 'descr')
       
    52     FMT_HLP_USAGE = """
       
    53 usage: %(prog)s %(name)s %(args)s
       
    54        %(prog)s %(alias)s %(args)s
       
    55 """
    51 
    56 
    52     def __init__(self, name, alias, func, args, descr):
    57     def __init__(self, name, alias, func, args, descr):
    53         """Create a new Command instance.
    58         """Create a new Command instance.
    54 
    59 
    55         Arguments:
    60         Arguments:
    73 
    78 
    74     @property
    79     @property
    75     def usage(self):
    80     def usage(self):
    76         """the command's usage info."""
    81         """the command's usage info."""
    77         return u'%s %s %s' % (prog, self.name, self.args)
    82         return u'%s %s %s' % (prog, self.name, self.args)
       
    83 
       
    84     def help_(self):
       
    85         """Print the Command's help message to stdout."""
       
    86         old_ii = txt_wrpr.initial_indent
       
    87         old_si = txt_wrpr.subsequent_indent
       
    88 
       
    89         txt_wrpr.subsequent_indent = (len(self.name) + 2) * ' '
       
    90         w_std(txt_wrpr.fill('%s: %s' % (self.name, self.descr)))
       
    91 
       
    92         info = Command.FMT_HLP_USAGE % dict(alias=self.alias, args=self.args,
       
    93                                             name=self.name, prog=prog)
       
    94         w_std(info)
       
    95 
       
    96         txt_wrpr.initial_indent = txt_wrpr.subsequent_indent = ' '
       
    97         try:
       
    98             [w_std(txt_wrpr.fill(_(para)) + '\n') for para
       
    99                     in help_msgs[self.name]]
       
   100         except KeyError:
       
   101             w_err(1, _(u"Subcommand '%s' is not yet documented." % self.name),
       
   102                   'see also: vmm(1)')
    78 
   103 
    79 
   104 
    80 class RunContext(object):
   105 class RunContext(object):
    81     """Contains all information necessary to run a subcommand."""
   106     """Contains all information necessary to run a subcommand."""
    82     __slots__ = ('argc', 'args', 'cget', 'hdlr', 'scmd')
   107     __slots__ = ('argc', 'args', 'cget', 'hdlr', 'scmd')
   443                     topic = scmd.name
   468                     topic = scmd.name
   444                     break
   469                     break
   445             else:
   470             else:
   446                 usage(INVALID_ARGUMENT, _(u"Unknown help topic: '%s'") %
   471                 usage(INVALID_ARGUMENT, _(u"Unknown help topic: '%s'") %
   447                       ctx.args[2], ctx.scmd)
   472                       ctx.args[2], ctx.scmd)
   448         # FIXME
   473         if topic != u'help':
   449         w_err(1, "'help %s' not yet implemented." % topic, 'see also: vmm(1)')
   474             return cmd_map[topic].help_()
   450 
   475 
   451     old_ii = txt_wrpr.initial_indent
   476     old_ii = txt_wrpr.initial_indent
   452     old_si = txt_wrpr.subsequent_indent
   477     old_si = txt_wrpr.subsequent_indent
   453     txt_wrpr.initial_indent = ' '
   478     txt_wrpr.initial_indent = ' '
   454     # len(max(_overview.iterkeys(), key=len)) #Py25
   479     # len(max(_overview.iterkeys(), key=len)) #Py25