Pass the nargs and const arguments to add_argument:
parser.add_argument('--list',
default="all",
const="all",
nargs="?",
choices=['servers', 'storage', 'all'],
help='list servers, storage, or both (default: %(default)s)')
If you want to know if --list was passed without an argument, remove the const argument, and check if args.list is None.
Documention:
nargs with '?'
One argument will be consumed from the command line if possible, and produced as a single item. If no command-line argument is present, the value from
defaultwill be produced. Note that for optional arguments, there is an additional case – the option string is present but not followed by a command-line argument. In this case the value fromconstwill be produced.
const
When
add_argument()is called with option strings (like-for--foo) andnargs="?". This creates an optional argument that can be followed by zero or one command-line arguments. When parsing the command line, if the option string is encountered with no command-line argument following it, the value ofconstwill be assumed instead. See the nargs description for examples.