diff options
| -rw-r--r-- | src/mailman/commands/cli_members.py | 26 | ||||
| -rw-r--r-- | src/mailman/commands/docs/members.txt | 25 |
2 files changed, 45 insertions, 6 deletions
diff --git a/src/mailman/commands/cli_members.py b/src/mailman/commands/cli_members.py index b47d72127..538b885db 100644 --- a/src/mailman/commands/cli_members.py +++ b/src/mailman/commands/cli_members.py @@ -60,6 +60,11 @@ class Members: indicate standard input. Blank lines and lines That start with a '#' are ignored. Without this option, this command displays mailing list members.""")) + command_parser.add_argument( + '-o', '--output', + dest='output_filename', metavar='FILENAME', + help=_("""Display output to FILENAME instead of stdout. FILENAME + can be '-' to indicate standard output.""")) # Required positional argument. command_parser.add_argument( 'listname', metavar='LISTNAME', nargs=1, @@ -89,12 +94,21 @@ class Members: :param args: The command line arguments. :type args: `argparse.Namespace` """ - addresses = list(mlist.members.addresses) - if len(addresses) == 0: - print mlist.fqdn_listname, 'has no members' - return - for address in sorted(addresses, key=attrgetter('address')): - print formataddr((address.real_name, address.original_address)) + if args.output_filename == '-' or args.output_filename is None: + fp = sys.stdout + else: + fp = codecs.open(args.output_filename, 'w', 'utf-8') + try: + addresses = list(mlist.members.addresses) + if len(addresses) == 0: + print >> fp, mlist.fqdn_listname, 'has no members' + return + for address in sorted(addresses, key=attrgetter('address')): + print >> fp, formataddr( + (address.real_name, address.original_address)) + finally: + if fp is not sys.stdout: + fp.close() def add_members(self, mlist, args): """Add the members in a file to a mailing list. diff --git a/src/mailman/commands/docs/members.txt b/src/mailman/commands/docs/members.txt index 73cd1c252..25642bfd9 100644 --- a/src/mailman/commands/docs/members.txt +++ b/src/mailman/commands/docs/members.txt @@ -9,6 +9,7 @@ and remove members from a mailing list. >>> class FakeArgs: ... input_filename = None + ... output_filename = None ... listname = [] >>> args = FakeArgs() @@ -55,6 +56,30 @@ Members are displayed in alphabetical order based on their address. Anne Person <anne@example.com> Bart Person <bart@example.com> +You can also output this list to a file. + + >>> from tempfile import mkstemp + >>> fd, args.output_filename = mkstemp() + >>> import os + >>> os.close(fd) + >>> command.process(args) + >>> with open(args.output_filename) as fp: + ... print fp.read() + Anne Person <anne@aaaxample.com> + Anne Person <anne@example.com> + Bart Person <bart@example.com> + >>> os.remove(args.output_filename) + >>> args.output_filename = None + +The output file can also be standard out. + + >>> args.output_filename = '-' + >>> command.process(args) + Anne Person <anne@aaaxample.com> + Anne Person <anne@example.com> + Bart Person <bart@example.com> + >>> args.output_filename = None + Adding members ============== |
