VirtualMailManager/cli/subcommands.py
changeset 596 607aa5c2acc4
parent 593 3dc1764c23d2
child 597 c79fdce49908
equal deleted inserted replaced
595:86b967c0f1a1 596:607aa5c2acc4
    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         old_ii = txt_wrpr.initial_indent
       
    86         old_si = txt_wrpr.subsequent_indent
       
    87 
       
    88         txt_wrpr.subsequent_indent = (len(self.name) + 2) * ' '
       
    89         w_std(txt_wrpr.fill('%s: %s' % (self.name, self.descr)))
       
    90 
       
    91         info = Command.FMT_HLP_USAGE % dict(alias=self.alias, args=self.args,
       
    92                                             name=self.name, prog=prog)
       
    93         w_std(info)
       
    94 
       
    95         txt_wrpr.initial_indent = txt_wrpr.subsequent_indent = ' '
       
    96         try:
       
    97             [w_std(txt_wrpr.fill(_(para)) + '\n') for para
       
    98                     in help_msgs[self.name]]
       
    99         except KeyError:
       
   100             w_err(1, "'help %s' not yet documented." % self.name,
       
   101                   'see also: vmm(1)')
    78 
   102 
    79 
   103 
    80 class RunContext(object):
   104 class RunContext(object):
    81     """Contains all information necessary to run a subcommand."""
   105     """Contains all information necessary to run a subcommand."""
    82     __slots__ = ('argc', 'args', 'cget', 'hdlr', 'scmd')
   106     __slots__ = ('argc', 'args', 'cget', 'hdlr', 'scmd')
   441                     topic = scmd.name
   465                     topic = scmd.name
   442                     break
   466                     break
   443             else:
   467             else:
   444                 usage(INVALID_ARGUMENT, _(u"Unknown help topic: '%s'") %
   468                 usage(INVALID_ARGUMENT, _(u"Unknown help topic: '%s'") %
   445                       ctx.args[2], ctx.scmd)
   469                       ctx.args[2], ctx.scmd)
   446         # FIXME
   470         if topic != u'help':
   447         w_err(1, "'help %s' not yet implemented." % topic, 'see also: vmm(1)')
   471             return cmd_map[topic].help_()
   448 
   472 
   449     old_ii = txt_wrpr.initial_indent
   473     old_ii = txt_wrpr.initial_indent
   450     old_si = txt_wrpr.subsequent_indent
   474     old_si = txt_wrpr.subsequent_indent
   451     txt_wrpr.initial_indent = ' '
   475     txt_wrpr.initial_indent = ' '
   452     # len(max(_overview.iterkeys(), key=len)) #Py25
   476     # len(max(_overview.iterkeys(), key=len)) #Py25