Skip to content

Commit f937a0c

Browse files
committed
fix(main) argparse 'extend" action on <py38
1 parent 7afe531 commit f937a0c

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/promnesia/__main__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from typing import List, Tuple, Optional, Dict, Sequence, Iterable, Iterator, Union
66
from pathlib import Path
77
from datetime import datetime
8-
from .compat import check_call
8+
from .compat import check_call, register_argparse_extend_action_in_pre_py38
99
from tempfile import TemporaryDirectory
1010

1111

@@ -292,6 +292,7 @@ def add_index_args(parser: argparse.ArgumentParser, default_config_path: PathIsh
292292
:param default_config_path:
293293
if not given, all :func:`demo_sources()` are run
294294
"""
295+
register_argparse_extend_action_in_pre_py38(parser)
295296
parser.add_argument('--config', type=Path, default=default_config_path, help='Config path')
296297
parser.add_argument('--dry', action='store_true', help="Dry run, won't touch the database, only print the results out")
297298
parser.add_argument(

src/promnesia/compat.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,36 @@ def _fix(args: Paths) -> List[str]:
1111
assert not isinstance(args, str), args # just to prevent shell=True calls...
1212
return list(map(str, args))
1313

14+
15+
import argparse
16+
17+
def register_argparse_extend_action_in_pre_py38(parser: argparse.ArgumentParser):
18+
import sys
19+
20+
if sys.version_info < (3, 8):
21+
22+
class ExtendAction(argparse.Action):
23+
24+
def __call__(self, parser, namespace, values, option_string=None):
25+
items = getattr(namespace, self.dest) or []
26+
items.extend(values)
27+
setattr(namespace, self.dest, items)
28+
29+
30+
parser.register('action', 'extend', ExtendAction)
31+
32+
1433
import subprocess
1534

1635
def run(args: Paths, **kwargs) -> subprocess.CompletedProcess:
1736
return subprocess.run(_fix(args), **kwargs)
1837

1938
def check_call(args: Paths, **kwargs) -> None:
2039
subprocess.check_call(_fix(args), **kwargs)
21-
40+
2241
def check_output(args: Paths, **kwargs) -> bytes:
2342
return subprocess.check_output(_fix(args), **kwargs)
24-
43+
2544
def Popen(args: Paths, **kwargs) -> subprocess.Popen:
2645
return subprocess.Popen(_fix(args), **kwargs)
2746

0 commit comments

Comments
 (0)