Passing a tuple as command line argument

2024/11/18 21:53:39

My requirement is to pass a tuple as command line argument like

--data (1,2,3,4)

I tried to use the argparse module, but if I pass like this it is receiving as the string '(1,2,3,4)'. I tried by giving type=tuple for argparse.add_argument, but is of no use here.

Do I have to add a new type class and pass that to type argument of add_argument?

Update

I tried the ast.literal_eval based on answers. Thanks for that. But it is giving spaces in the result as shown below.

(1,2,3,4)
<type 'str'>
(1, 2, 3, 4)
<type 'tuple'>
Answer

Set nargs of the data argument to nargs="+" (meaning one or more) and type to int, you can then set the arguments like this on the command line:

--data 1 2 3 4

args.data will now be a list of [1, 2, 3, 4].

If you must have a tuple, you can do:

my_tuple = tuple(args.data)

Putting it all together:

parser = argparse.ArgumentParser()
parser.add_argument('--data', nargs='+', type=int)
args = parser.parse_args()
my_tuple = tuple(args.data)
https://en.xdnf.cn/q/26522.html

Related Q&A

Setting exit code in Python when an exception is raised

$ cat e.py raise Exception $ python e.py Traceback (most recent call last):File "e.py", line 1, in <module>raise Exception Exception $ echo $? 1I would like to change this exit code fr…

cmake error the source does not appear to contain CMakeLists.txt

Im installing opencv in ubuntu 16.04. After installing the necessary prerequisites I used the following command:-kvs@Hunter:~/opencv_contrib$ mkdir build kvs@Hunter:~/opencv_contrib$ cd build kvs@Hunte…

Overlay an image segmentation with numpy and matplotlib

I am trying to overlay two images. The first one is a 512x512 NumPy array (from a CT image). The second one is also a 512x512 NumPy array but I am just interested in the pixels where the value is large…

Why isnt my variable set when I call the other function? [duplicate]

This question already has answers here:How do I get ("return") a result (output) from a function? How can I use the result later?(4 answers)How can I fix a TypeError that says an operator (…

Return max of zero or value for a pandas DataFrame column

I want to replace negative values in a pandas DataFrame column with zero.Is there a more concise way to construct this expression?df[value][df[value] < 0] = 0

Getting str object has no attribute get in Django

views.pydef generate_xml(request, number):caller_id = x-x-x-xresp = twilio.twiml.Response()with resp.dial(callerId=caller_id) as r:if number and re.search([\d\(\)\- \+]+$, number):r.number(number)else:…

Cython Speed Boost vs. Usability [closed]

Closed. This question is opinion-based. It is not currently accepting answers.Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.Clo…

cc1plus: warning: command line option -Wstrict-prototypes is valid for Ada/C/ObjC but not for C++

I am building a C++ extension for use in Python. I am seeing this warning being generated during the compilation process - when a type:python setup.py build_ext -iWhat is causing it, and how do I fix i…

Any way to add a new line from a string with the \n character in flask?

I was playing around with flask when I came across an odd problem with the \n character. it dosent seem to have an effect in my browser, I tried putting in there but it didnt work, any ideas?from fla…

Get list of Cache Keys in Django

Im trying to understand how Django is setting keys for my views. Im wondering if theres a way to just get all the saved keys from Memcached. something like a cache.all() or something. Ive been trying t…