I am trying to add an option starting with number, e.g., --3d here:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--3d", action="store_true")
args = parser.parse_args(['--3d'])
print(args)
parser works well, but args looks like this and I don't know how to get the value:
Namespace(**{'3d': True})
I've tried args.3d, args("3d"), args["3d"], args{"3d"}, etc, and none of them works.
I know I can add dest in add_argument() to work around this problem, but I still want to learn how to get the data in **{}.
getattr(args, '3d')?foo(**{'3d': True})is equivalent tofoo(3d=True), just unpacking a dict into named arguments. The difference is that3d=…is an illegal literal, and illegal for the same reason to use as plainargs.3d. However, using a string for it and alternative methods to unpack/access it works.