I have an optional argument -s to setup the tool. I hope user could give an input or use the default value. If he decides to use the default one, he should be able to skip giving extra input.
for example, myScript -s and myScript -s "Hello" should both work
Seems action = store_true not working well with nargs='*'
argparse store action allows the programmer to differentiate 3 use cases:
default)const)Assuming that you want -s and -s Hello to perform the same, you could use
parser = argparse.ArgumentParser(description = "Some desc.")
parser.add_argument("-s", nargs='?', const='Hello', default = None)
then you can test it:
>>> parser.parse_args([])
Namespace(s=None)
>>> parser.parse_args(["-s"])
Namespace(s='Hello')
>>> parser.parse_args(["-s", 'Hello'])
Namespace(s='Hello')
>>> parser.parse_args(["-s", 'foo'])
Namespace(s='foo')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With