Skip to content

Commit f46aafe

Browse files
Felix Kloftfkloft
authored andcommitted
assign bag_info from arguments to namespace instead of parser
Python docs recommend to add properties to namespace, this allows for parsers to be reused.
1 parent db0e3dc commit f46aafe

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

bagit.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,15 +1442,18 @@ def force_unicode_py2(s):
14421442

14431443
class BagArgumentParser(argparse.ArgumentParser):
14441444
def __init__(self, *args, **kwargs):
1445-
self.bag_info = {}
14461445
argparse.ArgumentParser.__init__(self, *args, **kwargs)
14471446

14481447

14491448
class BagHeaderAction(argparse.Action):
1450-
def __call__(self, parser, _, values, option_string=None):
1449+
def __call__(self, parser, namespace, values, option_string=None):
14511450
opt = option_string.lstrip("--")
14521451
opt_caps = "-".join([o.capitalize() for o in opt.split("-")])
14531452
parser.bag_info[opt_caps] = values
1453+
try:
1454+
namespace.bag_info[opt_caps] = values
1455+
except AttributeError:
1456+
namespace.bag_info = {opt_caps: values}
14541457

14551458

14561459
def _make_parser():
@@ -1593,7 +1596,7 @@ def main():
15931596
try:
15941597
make_bag(
15951598
bag_dir,
1596-
bag_info=parser.bag_info,
1599+
bag_info=args.bag_info,
15971600
processes=args.processes,
15981601
checksums=args.checksums,
15991602
)

0 commit comments

Comments
 (0)