1

Consider

import argparse

parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--foo", type=str, default="bar", help="description")
args = parser.parse_args()

Now I would like to know if args.foo got its value due to the default, or because the user specified myprogram --foo bar. How can I distinguish that?

Edit: A big plus would be if the docstring still reflected the default.

Tom de Geus
  • 5,625
  • 2
  • 33
  • 77

1 Answers1

3

Since the definition of default is use that value, if nothing is provided it is not be possible. You could change the default to None, and handle it by setting it to the real default, however.

 if args.foo is None:
    # set to intended default
 else:
    # was provided by user
guidot
  • 5,095
  • 2
  • 25
  • 37