I would like to invoce my programm like program -s <optional value>
. I would like to assign a default value, but would also like to be able to detect if the -s
switch was given.
What I have:
max_entries_shown = 10
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-s",
nargs = '?',
default = max_entries_shown)
args = parser.parse_args()
This gives me a value of 10 for args.s
if I don't give -s
on the command line, and None
if I specify -s
without a value. What I want is args.s
equal to None
if no switch is given, and args.s
set to the default value with -s
given, and args.s
equal to custom_value
if run as program -s custom_value
. How can I achive this?