Command line arguments are always passed as strings. You will need to parse them into your required data type yourself.
>>> input = "[2,3,4,5]"
>>> map(float, input.strip('[]').split(','))
[2.0, 3.0, 4.0, 5.0]
>>> A = map(float, input.strip('[]').split(','))
>>> print(A, type(A))
([2.0, 3.0, 4.0, 5.0], <type 'list'>)
There are libraries like argparse and click that let you define your own argument type conversion but argparse
treats "[2,3,4]"
the same as [
2
,
3
,
4
]
so I doubt it will be useful.
edit Jan 2019 This answer seems to get a bit of action still so I'll add another option taken directly from the argparse docs.
You can use action=append
to allow repeated arguments to be collected into a single list.
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', action='append')
>>> parser.parse_args('--foo 1 --foo 2'.split())
Namespace(foo=['1', '2'])
In this case you would pass --foo ?
once for each list item. Using OPs example: python filename.py --foo 2 --foo 3 --foo 4 --foo 5
would result in foo=[2,3,4,5]
lists = [[int(el) for el in arg[1:-1].split(',')] for arg in sys.argv[1:]]
? Here you can see that the brackets are rather useless in this case.