Commit 82affb26 authored by Barry Warsaw's avatar Barry Warsaw

Refactor the subcommands so that the infrastructure does more of the menial

tasks.  This also let's the bin/mailman command sort its subcommands for help
printing.

Add the outline of a 'members' subcommand.
parent 592f3d56
......@@ -60,10 +60,26 @@ def main():
# partially parse the arguments now, then initialize the system, then find
# the plugins. Punt on this for now.
subparser = parser.add_subparsers(title='Commands')
subcommands = []
for command_class in find_components('mailman.commands', ICLISubCommand):
command = command_class()
verifyObject(ICLISubCommand, command)
command.add(parser, subparser)
subcommands.append(command)
# --help should display the subcommands by alphabetical order, except that
# 'mailman help' should be first.
def sort_function(command, other):
if command.name == 'help':
return -1
elif other.name == 'help':
return 1
else:
return cmp(command.name, other.name)
subcommands.sort(cmp=sort_function)
for command in subcommands:
command_parser = subparser.add_parser(
command.name, help=_(command.__doc__))
command.add(parser, command_parser)
command_parser.set_defaults(func=command.process)
args = parser.parse_args()
if len(args.__dict__) == 0:
# No arguments or subcommands were given.
......
......@@ -32,16 +32,16 @@ from mailman.interfaces.command import ICLISubCommand
class Help:
"""The `help` subcommand."""
# Lowercase, to match argparse's default --help text.
"""show this help message and exit"""
implements(ICLISubCommand)
def add(self, parser, subparser):
name = 'help'
def add(self, parser, command_parser):
"""See `ICLISubCommand`."""
self.parser = parser
help_parser = subparser.add_parser(
'help', help=('show this help message and exit'))
help_parser.set_defaults(func=self.process)
def process(self, args):
"""See `ICLISubCommand`."""
......
......@@ -37,16 +37,15 @@ from mailman.version import MAILMAN_VERSION_FULL
class Info:
"""The `info` subcommand."""
"""Information about this Mailman instance."""
implements(ICLISubCommand)
def add(self, parser, subparser):
name = 'info'
def add(self, parser, command_parser):
"""See `ICLISubCommand`."""
info_parser = subparser.add_parser(
'info', help=_('Information about this Mailman instance.'))
info_parser.set_defaults(func=self.process)
info_parser.add_argument(
command_parser.add_argument(
'-o', '--output',
action='store', help=_("""\
File to send the output to. If not given, standard output is
......
......@@ -44,31 +44,30 @@ from mailman.interfaces.listmanager import ListAlreadyExistsError
class Lists:
"""The `lists` subcommand."""
"""List all mailing lists"""
implements(ICLISubCommand)
def add(self, parser, subparser):
name = 'lists'
def add(self, parser, command_parser):
"""See `ICLISubCommand`."""
lists_parser = subparser.add_parser(
'lists', help=_('List all mailing lists'))
lists_parser.set_defaults(func=self.process)
lists_parser.add_argument(
command_parser.add_argument(
'-a', '--advertised',
default=False, action='store_true',
help=_(
'List only those mailing lists that are publicly advertised'))
lists_parser.add_argument(
command_parser.add_argument(
'-b', '--bare',
default=False, action='store_true',
help=_('Show only the list name, with no description'))
lists_parser.add_argument(
command_parser.add_argument(
'-d', '--domain',
action='append', help=_("""\
List only those mailing lists hosted on the given domain, which
must be the email host name. Multiple -d options may be given.
"""))
lists_parser.add_argument(
command_parser.add_argument(
'-f', '--full',
default=False, action='store_true',
help=_(
......@@ -115,22 +114,20 @@ class Lists:
class Create:
"""The `create` subcommand."""
"""Create a mailing list"""
implements(ICLISubCommand)
def add(self, parser, subparser):
name = 'create'
def add(self, parser, command_parser):
"""See `ICLISubCommand`."""
self.parser = parser
create_parser = subparser.add_parser(
'create', help=_('Create a mailing list'))
create_parser.set_defaults(func=self.process)
create_parser.add_argument(
command_parser.add_argument(
'--language',
type='unicode', metavar='CODE', help=_("""\
Set the list's preferred language to CODE, which must be a
registered two letter language code."""))
create_parser.add_argument(
command_parser.add_argument(
'-o', '--owner',
type='unicode', action='append', default=[],
dest='owners', metavar='OWNER', help=_("""\
......@@ -139,23 +136,23 @@ class Create:
linked to a user. Mailman will send a confirmation message to the
address, but it will also send a list creation notice to the
address. More than one owner can be specified."""))
create_parser.add_argument(
command_parser.add_argument(
'-n', '--notify',
default=False, action='store_true',
help=_("""\
Notify the list owner by email that their mailing list has been
created."""))
create_parser.add_argument(
command_parser.add_argument(
'-q', '--quiet',
default=False, action='store_true',
help=_('Print less output.'))
create_parser.add_argument(
command_parser.add_argument(
'-d', '--domain',
default=False, action='store_true',
help=_("""\
Register the mailing list's domain if not yet registered."""))
# Required positional argument.
create_parser.add_argument(
command_parser.add_argument(
'listname', metavar='LISTNAME', nargs=1,
help=_("""\
The 'fully qualified list name', i.e. the posting address of the
......@@ -221,28 +218,26 @@ class Create:
class Remove:
"""The `remove` subcommand."""
"""Remove a mailing list"""
implements(ICLISubCommand)
def add(self, parser, subparser):
name = 'remove'
def add(self, parser, command_parser):
"""See `ICLISubCommand`."""
self.parser = parser
remove_parser = subparser.add_parser(
'remove', help=_('Remove a mailing list'))
remove_parser.set_defaults(func=self.process)
remove_parser.add_argument(
command_parser.add_argument(
'-a', '--archives',
default=False, action='store_true',
help=_("""\
Remove the list's archives too, or if the list has already been deleted,
remove any residual archives."""))
remove_parser.add_argument(
command_parser.add_argument(
'-q', '--quiet',
default=False, action='store_true',
help=_('Suppress status messages'))
# Required positional argument.
remove_parser.add_argument(
command_parser.add_argument(
'listname', metavar='LISTNAME', nargs=1,
help=_("""\
The 'fully qualified list name', i.e. the posting address of the
......
# Copyright (C) 2009 by the Free Software Foundation, Inc.
#
# This file is part of GNU Mailman.
#
# GNU Mailman is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
"""The 'members' subcommand."""
from __future__ import absolute_import, unicode_literals
__metaclass__ = type
__all__ = [
'Members',
]
from zope.interface import implements
from mailman.i18n import _
from mailman.interfaces.command import ICLISubCommand
class Members:
"""Manage list memberships"""
implements(ICLISubCommand)
name = 'members'
def add(self, parser, command_parser):
"""See `ICLISubCommand`."""
def process(self, args):
"""See `ICLISubCommand`."""
......@@ -73,12 +73,16 @@ class IEmailCommand(Interface):
class ICLISubCommand(Interface):
"""A command line interface subcommand."""
def add(parser, subparser):
name = Attribute('The command name; must be unique')
__doc__ = Attribute('The command short help')
def add(parser, command_parser):
"""Add the subcommand to the subparser.
:param parser: The argument parser.
:type parser: `argparse.ArgumentParser`
:param subparser: The argument subparser.
:param subparser: The command subparser.
:type subparser: `argparse.ArgumentParser`
"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment