|
1 | 1 | import argparse
|
2 | 2 | import logging
|
3 | 3 | import sys
|
4 |
| -from pathlib import Path |
| 4 | +from copy import deepcopy |
5 | 5 | from functools import partial
|
| 6 | +from pathlib import Path |
6 | 7 | from types import TracebackType
|
7 | 8 | from typing import List
|
8 | 9 |
|
|
18 | 19 | )
|
19 | 20 |
|
20 | 21 | logger = logging.getLogger(__name__)
|
| 22 | + |
| 23 | + |
| 24 | +class ParseKwargs(argparse.Action): |
| 25 | + """ |
| 26 | + Parse arguments in the for `key=value`. |
| 27 | +
|
| 28 | + Quoted strings are automatically unquoted. |
| 29 | + Can be submitted multiple times: |
| 30 | +
|
| 31 | + ex: |
| 32 | + -k key=value -k double-quotes="value" -k single-quotes='value' |
| 33 | +
|
| 34 | + will result in |
| 35 | +
|
| 36 | + namespace["opt"] == { |
| 37 | + "key": "value", |
| 38 | + "double-quotes": "value", |
| 39 | + "single-quotes": "value", |
| 40 | + } |
| 41 | + """ |
| 42 | + |
| 43 | + def __call__(self, parser, namespace, kwarg, option_string=None): |
| 44 | + kwargs = getattr(namespace, self.dest, None) or {} |
| 45 | + key, value = kwarg.split("=", 1) |
| 46 | + kwargs[key] = value.strip("'\"") |
| 47 | + setattr(namespace, self.dest, kwargs) |
| 48 | + |
| 49 | + |
| 50 | +tpl_arguments = ( |
| 51 | + { |
| 52 | + "name": ["--template", "-t"], |
| 53 | + "help": ( |
| 54 | + "changelog template file name " |
| 55 | + "(relative to the current working directory)" |
| 56 | + ), |
| 57 | + }, |
| 58 | + { |
| 59 | + "name": ["--extra", "-e"], |
| 60 | + "action": ParseKwargs, |
| 61 | + "dest": "extras", |
| 62 | + "metavar": "EXTRA", |
| 63 | + "help": "a changelog extra variable (in the form 'key=value')", |
| 64 | + }, |
| 65 | +) |
| 66 | + |
21 | 67 | data = {
|
22 | 68 | "prog": "cz",
|
23 | 69 | "description": (
|
|
197 | 243 | "default": None,
|
198 | 244 | "help": "keep major version at zero, even for breaking changes",
|
199 | 245 | },
|
| 246 | + *deepcopy(tpl_arguments), |
200 | 247 | {
|
201 | 248 | "name": ["--prerelease-offset"],
|
202 | 249 | "type": int,
|
|
274 | 321 | "If not set, it will include prereleases in the changelog"
|
275 | 322 | ),
|
276 | 323 | },
|
| 324 | + *deepcopy(tpl_arguments), |
277 | 325 | ],
|
278 | 326 | },
|
279 | 327 | {
|
|
0 commit comments