4

The documented way to pass a list of arguments using argparse uses the following syntax

parser = argparse.ArgumentParser()
parser.add_argument("--list", nargs="+", default=["a", "b"])
value = parser.parse_args()

In the SHELL

$ python3 example.py --list a b c

$ ['a', 'b', 'c']

The arguments Im working with have leading dashes and require the use of = to not be misinterpreted by argparse as another option (See this question)

$ python3 example.py --list=-a

$ ['-a']

I would like to pass in a list of arguments using the = syntax. However, Im not sure how to accomplish this. I'm looking for something like the following to work:

$ python3 example.py --list=-a,-b

$ ['-a','-b']

Gunner Stone
  • 997
  • 8
  • 26
  • 1
    You can wrap them in quotes and do the string formatting yourself, otherwise you would need to change the `prefix_chars` to something other than `'-'` – Alexander Jun 06 '22 at 23:02
  • 1
    I think per https://stackoverflow.com/questions/9025204/python-argparse-issue-with-optional-arguments-which-are-negative-numbers you could do something like `--list " -a" " -b"` (note the space right after the open paren) – Joran Beasley Jun 06 '22 at 23:03

0 Answers0