Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to set argument_default=argparse.SUPPRESS for subparsers?

I don’t want to get the unset options in the argument namespace. But argparse.SUPPRESS seems not pass on to subparsers.
The following code print(args) got the result. Since sub_arg2 and sub_args3 is not set, how to get ride of them in the namespace?

Namespace(sub_arg1=’1′, sub_arg2=[], sub_arg3=None, subparser=’sub1′)

import argparse

parser = argparse.ArgumentParser(argument_default=argparse.SUPPRESS)
# parser.add_argument('main_arg1')
# parser.add_argument('main_arg2', nargs='*')
subparsers = parser.add_subparsers(dest='subparser')
sparser = subparsers.add_parser('sub1')
sparser.add_argument('sub_arg1')
sparser.add_argument('sub_arg2', nargs='*')
sparser.add_argument('sub_arg3', nargs='?')

args = parser.parse_args('sub1 1'.split())
print(args)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

add_parser takes any arguments the ArgumentParser constructor takes:

sparser = subparsers.add_parser('sub1', argument_default=argparse.SUPPRESS)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading