Is there a way to add an argument that is only usable as -p
or -p=value
, but not -p value
I want to add an optional argument --parallel/-p
to the script. Here is how I want user to pass it:
./script arg1 arg2 -p # this defaults to 10
./script arg1 arg2 -p=13 # This is fine and will set the value to 13
./script arg1 arg2 -p 13 # This is NOT fine and I want to ban this. Next line will tell why.
./script -p arg1 arg2 # I want -p to be usable regardless of its position.
# But here arg1 will be considered as an argument of -p option, which is unwanted.
Unfortunately you have two problems here :
argparse
only allow --option=value
for long argument, which are generated when the name is longer than one character.
argparse
give no way to enforce the --option=value
syntax, and --option value
will always be possible.
You may have to write your own parser using what is inside sys.args
(if you only have one option, it can be done quickly by searching the argument starting with "-p"
.
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