To accept the inputs as specified in the question, you must pre-process the arguments. I tried many things, from where the negative number issue appears in the documentation, from here and from Python Argparse: Issue with optional arguments which are negative numbers, but those methods didn't work in this specific kind of case (my case was https://github.com/poikilos/minetestmapper-python/blob/master/minetestmapper-numpy.py). I solved the issue as follows.
Step 1: Before using argparse, do the following:
cb_ticks_str = None
i = 0
while i < len(sys.argv):
if sys.argv[i] == "--cb_ticks":
del sys.argv[i]
cb_ticks_str = ''
elif cb_ticks_str == '':
cb_ticks_str = sys.argv[i]
del sys.argv[i]
break
else:
i += 1
i = None
Step 2: Use argpase as normal except don't use it for any non-numerical argument that starts with a hyphen:
parser = argparse.ArgumentParser()
# parser.add_argument('--cb_ticks', required=False, default='')
args = vars(parser.parse_args())
Step 3: Split your argument manually then add it to your args dict:
if cb_ticks_str is not None:
args['cb_ticks'] = [int(v) for v in cb_ticks_str.split(",")]
# ^ raises ValueError if a non-int is in the list
if len(args['cb_ticks']) != 2:
raise ValueError("cb_ticks must have 2 values separated by a comma.")
Alternatively:
If you were using the parser directly instead of using vars like in the question, (do #1 as I described) then in #2 change args = vars(parser.parse_args())
to args = parser.parse_args()
, then for #3 instead do:
Step 3: Split your argument manually then add it to an args object:
if cb_ticks_str is not None:
args.cb_ticks = [int(v) for v in cb_ticks_str.split(",")]
# ^ raises ValueError if a non-int is in the list
if len(args.cb_ticks) != 2:
raise ValueError("cb_ticks must have 2 values separated by a comma.")