summaryrefslogtreecommitdiff
path: root/src/mailman/utilities/options.py
blob: 7f1f5a1c5325b446689cb824a1546d2e7e590c25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Copyright (C) 2008-2017 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/>.

"""Argument parsing utilities."""

import click

from mailman.core.i18n import _
from public import public


@public
def validate_runner_spec(ctx, param, value):
    # This validator handles two cases.  First, for the runner script where
    # only a single --runner option is allowed, and second the master script
    # where multiple --runner options are allowed.  When no --runner options
    # are given, we'll either get None or the empty tuple.  That's why we use
    # false-iness here.
    if not value:
        return value
    specs = []
    for spec in ([value] if isinstance(value, str) else value):
        parts = spec.split(':')
        if len(parts) == 1:
            specs.append((parts[0], 1, 1))
        elif len(parts) == 3:
            runner = parts[0]
            try:
                rslice = int(parts[1])
                rrange = int(parts[2])
            except ValueError:
                raise click.BadParameter(
                    _('slice and range must be integers: $value'))
            specs.append((runner, rslice, rrange))
        else:
            raise click.UsageError(_('Bad runner spec: $value'))
    return specs[0] if isinstance(value, str) else specs


@public
class I18nCommand(click.Command):                   # pragma: nocover
    # https://github.com/pallets/click/issues/834
    #
    # Note that this handles the case for the `mailman <subcommand> --help`
    # output.  To handle `mailman --help` we override the same method in the
    # `Subcommands` subclass over in src/mailman/bin/mailman.py.  The test
    # suite doesn't cover *this* copy of the method but who cares, since it
    # will hopefully go away some day.
    def format_options(self, ctx, formatter):
        """Writes all the options into the formatter if they exist."""
        opts = []
        for param in self.get_params(ctx):
            rv = param.get_help_record(ctx)
            if rv is not None:
                part_a, part_b = rv
                opts.append((part_a, part_b.replace('\n', ' ')))
        if opts:
            with formatter.section('Options'):
                formatter.write_dl(opts)