This question already has an answer here:
- Option accepted with and without value 2 answers
I need to recognise if was given argument alone or with optional string or neither
parser.add_argument(???)
options = parser.parse_args()
so
./prog.py --arg
should store ''
into options.arg,
./prog.py --arg=lol
stores 'lol'
into options.arg and
./prog.py
left options.arg as None
now I have:
parser.add_argument("--arg", nargs="?",type=str,dest="arg")
but when I run myprogram as ./prog.py --arg
options.arg remains None
. Only way to recognise --arg was given is run it as ./prog.py --arg=
and this is problem for me.
Use the
const
keyword:results in
You can do it with a custom action: