0

I am trying to pass from the command line a delimited string of numbers including negative numbers using argparse.

ap = argparse.ArgumentParser()
ap.add_argument("-l","--list",help="delimited string input",type=str)
args = vars(ap.parse_args())

When I run the code using:

python3 prog.py -l "-1,0,1,0,1,0,1,0,-1"

I get:

usage: camWithFilterDemo.py [-h] [-l LIST]
camWithFilterDemo.py: error: argument -l/--list: expected one argument

Can anyone help with a solution or other options I should look into or consider.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • The problem is that `-1` is being treated as an option, but I'm not sure the best way to fix it. BTW welcome to SO! Check out the [tour] and [ask] if you want advice. – wjandrea Jul 02 '20 at 17:19
  • An extra space at the start of that string is an easy fix: python3 prog.py -l " -1,0,1,0,1,0,1,0,-1". Obviously it isn't a robust or user friendly choice, but it's the simplest patch. – hpaulj Jul 02 '20 at 18:18
  • The duplicate isn't idea, since it focuses on accepting a negative scientific notation. I'm sure there are newer duplicates that address the same issue. In any case argument strings which start with a '-' are a problem with `argparse`. It's a known issue without a easy fix. – hpaulj Jul 02 '20 at 18:22

0 Answers0