|
3 | 3 | import argparse
|
4 | 4 | import logging
|
5 | 5 | import sys
|
| 6 | +from copy import deepcopy |
6 | 7 | from functools import partial
|
7 | 8 | from pathlib import Path
|
8 | 9 | from types import TracebackType
|
|
19 | 20 | )
|
20 | 21 |
|
21 | 22 | logger = logging.getLogger(__name__)
|
| 23 | + |
| 24 | + |
| 25 | +class ParseKwargs(argparse.Action): |
| 26 | + """ |
| 27 | + Parse arguments in the for `key=value`. |
| 28 | +
|
| 29 | + Quoted strings are automatically unquoted. |
| 30 | + Can be submitted multiple times: |
| 31 | +
|
| 32 | + ex: |
| 33 | + -k key=value -k double-quotes="value" -k single-quotes='value' |
| 34 | +
|
| 35 | + will result in |
| 36 | +
|
| 37 | + namespace["opt"] == { |
| 38 | + "key": "value", |
| 39 | + "double-quotes": "value", |
| 40 | + "single-quotes": "value", |
| 41 | + } |
| 42 | + """ |
| 43 | + |
| 44 | + def __call__(self, parser, namespace, kwarg, option_string=None): |
| 45 | + kwargs = getattr(namespace, self.dest, None) or {} |
| 46 | + key, value = kwarg.split("=", 1) |
| 47 | + kwargs[key] = value.strip("'\"") |
| 48 | + setattr(namespace, self.dest, kwargs) |
| 49 | + |
| 50 | + |
| 51 | +tpl_arguments = ( |
| 52 | + { |
| 53 | + "name": ["--template", "-t"], |
| 54 | + "help": ( |
| 55 | + "changelog template file name " |
| 56 | + "(relative to the current working directory)" |
| 57 | + ), |
| 58 | + }, |
| 59 | + { |
| 60 | + "name": ["--extra", "-e"], |
| 61 | + "action": ParseKwargs, |
| 62 | + "dest": "extras", |
| 63 | + "metavar": "EXTRA", |
| 64 | + "help": "a changelog extra variable (in the form 'key=value')", |
| 65 | + }, |
| 66 | +) |
| 67 | + |
22 | 68 | data = {
|
23 | 69 | "prog": "cz",
|
24 | 70 | "description": (
|
|
204 | 250 | "default": None,
|
205 | 251 | "help": "keep major version at zero, even for breaking changes",
|
206 | 252 | },
|
| 253 | + *deepcopy(tpl_arguments), |
207 | 254 | {
|
208 | 255 | "name": ["--prerelease-offset"],
|
209 | 256 | "type": int,
|
|
293 | 340 | "default": None,
|
294 | 341 | "choices": version_schemes.KNOWN_SCHEMES,
|
295 | 342 | },
|
| 343 | + *deepcopy(tpl_arguments), |
296 | 344 | ],
|
297 | 345 | },
|
298 | 346 | {
|
|
0 commit comments