ArgumentParser.set_defaults(**kwargs)
Most of the time, the attributes of the object returned by parse_args()
will be fully determined by inspecting the command-line arguments and the argument actions. set_defaults()
allows some additional attributes that are determined without any inspection of the command line to be added:
>>> parser = argparse.ArgumentParser() >>> parser.add_argument('foo', type=int) >>> parser.set_defaults(bar=42, baz='badger') >>> parser.parse_args(['736']) Namespace(bar=42, baz='badger', foo=736)
Note that parser-level defaults always override argument-level defaults:
>>> parser = argparse.ArgumentParser() >>> parser.add_argument('--foo', default='bar') >>> parser.set_defaults(foo='spam') >>> parser.parse_args([]) Namespace(foo='spam')
Parser-level defaults can be particularly useful when working with multiple parsers. See the add_subparsers()
method for an example of this type.
Please login to continue.