|
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
|
|
20 | 21 | )
|
21 | 22 |
|
22 | 23 | logger = logging.getLogger(__name__)
|
| 24 | + |
| 25 | + |
| 26 | +class ParseKwargs(argparse.Action): |
| 27 | + """ |
| 28 | + Parse arguments in the for `key=value`. |
| 29 | +
|
| 30 | + Quoted strings are automatically unquoted. |
| 31 | + Can be submitted multiple times: |
| 32 | +
|
| 33 | + ex: |
| 34 | + -k key=value -k double-quotes="value" -k single-quotes='value' |
| 35 | +
|
| 36 | + will result in |
| 37 | +
|
| 38 | + namespace["opt"] == { |
| 39 | + "key": "value", |
| 40 | + "double-quotes": "value", |
| 41 | + "single-quotes": "value", |
| 42 | + } |
| 43 | + """ |
| 44 | + |
| 45 | + def __call__(self, parser, namespace, kwarg, option_string=None): |
| 46 | + kwargs = getattr(namespace, self.dest, None) or {} |
| 47 | + key, value = kwarg.split("=", 1) |
| 48 | + kwargs[key] = value.strip("'\"") |
| 49 | + setattr(namespace, self.dest, kwargs) |
| 50 | + |
| 51 | + |
| 52 | +tpl_arguments = ( |
| 53 | + { |
| 54 | + "name": ["--template", "-t"], |
| 55 | + "help": ( |
| 56 | + "changelog template file name " |
| 57 | + "(relative to the current working directory)" |
| 58 | + ), |
| 59 | + }, |
| 60 | + { |
| 61 | + "name": ["--extra", "-e"], |
| 62 | + "action": ParseKwargs, |
| 63 | + "dest": "extras", |
| 64 | + "metavar": "EXTRA", |
| 65 | + "help": "a changelog extra variable (in the form 'key=value')", |
| 66 | + }, |
| 67 | +) |
| 68 | + |
23 | 69 | data = {
|
24 | 70 | "prog": "cz",
|
25 | 71 | "description": (
|
|
210 | 256 | "default": None,
|
211 | 257 | "help": "keep major version at zero, even for breaking changes",
|
212 | 258 | },
|
| 259 | + *deepcopy(tpl_arguments), |
213 | 260 | {
|
214 | 261 | "name": ["--prerelease-offset"],
|
215 | 262 | "type": int,
|
|
299 | 346 | "default": None,
|
300 | 347 | "choices": version_schemes.KNOWN_SCHEMES,
|
301 | 348 | },
|
| 349 | + *deepcopy(tpl_arguments), |
302 | 350 | ],
|
303 | 351 | },
|
304 | 352 | {
|
|
0 commit comments