aboutsummaryrefslogtreecommitdiff
path: root/pw_cli/py/pw_cli/arguments.py
blob: a5b8e4b279b6552c09bdcab4013f89d2a2b24b1c (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# Copyright 2020 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
"""Defines arguments for the pw command."""

import argparse
from dataclasses import dataclass, field
from enum import Enum
import logging
from pathlib import Path
import sys
from typing import List, NoReturn, Optional

from pw_cli import argument_types, plugins
from pw_cli.branding import banner

_HELP_HEADER = '''The Pigweed command line interface (CLI).

Example uses:
    pw logdemo
    pw --loglevel debug watch -C out
'''


def parse_args() -> argparse.Namespace:
    return arg_parser().parse_args()


class ShellCompletionFormat(Enum):
    """Supported shell tab completion modes."""

    BASH = 'bash'
    ZSH = 'zsh'


@dataclass(frozen=True)
class ShellCompletion:
    option_strings: List[str] = field(default_factory=list)
    help: Optional[str] = None
    choices: Optional[List[str]] = None
    flag: bool = True

    def bash_completion(self, text: str) -> List[str]:
        result: List[str] = []
        for option_str in self.option_strings:
            if option_str.startswith(text):
                result.append(option_str)
        return result

    def zsh_completion(self, text: str) -> List[str]:
        result: List[str] = []
        for option_str in self.option_strings:
            if option_str.startswith(text):
                short_and_long_opts = ' '.join(self.option_strings)
                # '(-h --help)-h[Display help message and exit]'
                # '(-h --help)--help[Display help message and exit]'
                help_text = self.help if self.help else ''
                state_str = ''
                if not self.flag:
                    state_str = ': :->' + option_str

                result.append(
                    f'({short_and_long_opts}){option_str}[{help_text}]'
                    f'{state_str}'
                )
        return result


def get_options_and_help(
    parser: argparse.ArgumentParser,
) -> List[ShellCompletion]:
    return list(
        ShellCompletion(
            option_strings=list(action.option_strings),
            help=action.help,
            choices=list(action.choices) if action.choices else [],
            flag=action.nargs == 0,
        )
        for action in parser._actions  # pylint: disable=protected-access
    )


def print_completions_for_option(
    parser: argparse.ArgumentParser,
    text: str = '',
    tab_completion_format: str = ShellCompletionFormat.BASH.value,
) -> None:
    matched_lines: List[str] = []
    for completion in get_options_and_help(parser):
        if tab_completion_format == ShellCompletionFormat.ZSH.value:
            matched_lines.extend(completion.zsh_completion(text))
        else:
            matched_lines.extend(completion.bash_completion(text))

    for line in matched_lines:
        print(line)


def print_banner() -> None:
    """Prints the PIGWEED (or project specific) banner to stderr."""
    print(banner() + '\n', file=sys.stderr)


def format_help(registry: plugins.Registry) -> str:
    """Returns the pw help information as a string."""
    return f'{arg_parser().format_help()}\n{registry.short_help()}'


class _ArgumentParserWithBanner(argparse.ArgumentParser):
    """Parser that the Pigweed banner when there are parsing errors."""

    def error(self, message: str) -> NoReturn:
        print_banner()
        self.print_usage(sys.stderr)
        self.exit(2, f'{self.prog}: error: {message}\n')


def add_tab_complete_arguments(
    parser: argparse.ArgumentParser,
) -> argparse.ArgumentParser:
    parser.add_argument(
        '--tab-complete-option',
        nargs='?',
        help='Print tab completions for the supplied option text.',
    )
    parser.add_argument(
        '--tab-complete-format',
        choices=list(shell.value for shell in ShellCompletionFormat),
        default='bash',
        help='Output format for tab completion results.',
    )
    return parser


def arg_parser() -> argparse.ArgumentParser:
    """Creates an argument parser for the pw command."""
    argparser = _ArgumentParserWithBanner(
        prog='pw',
        add_help=False,
        description=_HELP_HEADER,
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )

    # Do not use the built-in help argument so that displaying the help info can
    # be deferred until the pw plugins have been registered.
    argparser.add_argument(
        '-h',
        '--help',
        action='store_true',
        help='Display this help message and exit',
    )
    argparser.add_argument(
        '-C',
        '--directory',
        type=argument_types.directory,
        default=Path.cwd(),
        help='Change to this directory before doing anything',
    )
    argparser.add_argument(
        '-l',
        '--loglevel',
        type=argument_types.log_level,
        default=logging.INFO,
        help='Set the log level (debug, info, warning, error, critical)',
    )
    argparser.add_argument(
        '--debug-log',
        help=(
            'Additionally log to this file at debug level; does not affect '
            'terminal output'
        ),
    )
    argparser.add_argument(
        '--no-banner',
        action='store_true',
        help='Do not print the Pigweed banner',
    )
    argparser.add_argument(
        '--tab-complete-command',
        nargs='?',
        help='Print tab completions for the supplied command text.',
    )
    argparser.add_argument(
        'command',
        nargs='?',
        help='Which command to run; see supported commands below',
    )
    argparser.add_argument(
        'plugin_args',
        metavar='...',
        nargs=argparse.REMAINDER,
        help='Remaining arguments are forwarded to the command',
    )
    return add_tab_complete_arguments(argparser)