This question already has answers here:
How can I pass a list as a command-line argument with argparse?
(13 answers)
Closed 3 days ago.
I want the argparse cli parameter to use "=" sign when i'm giving the list of the arguments with a space between them.
For example:
--repos= repo1 repo2 repo3
but when i'm giving the parameters in this way i'm getting the following exception
"error: unrecognized arguments: repo1 repo2 repo3"
Is there a way to pass the '=' to the parameter?
you can pass the '=' sign to the parameter by enclosing the argument that follows the '=' sign in quotes. For example:
--repos="repo1 repo2 repo3"
This will pass "repo1 repo2 repo3" as a single argument to the --repos option.
Related
This question already has answers here:
How to extract parameters from a list and pass them to a function call [duplicate]
(3 answers)
How to split list and pass them as separate parameter?
(4 answers)
Closed 1 year ago.
Assuming i have a method definition like this do_stuff(*arg).
I have a list of values, ['a','b',...]. How best can i pass the values of this list as individual arguments to the do_stuff method as do_stuff('a','b',...) ?
I've unsuccessfully tried using list comprehension - do_stuff([str(value) for value in some_list])
I am hoping i don't have to change do_stuff() to accept a list
Any pointers would be appreciated.
You can pass them using the "*" operator. So an example would be like this:
def multiplyfouritems(a,b,c,d):
return a*b*c*d
multiplyfouritems(*[1,2,3,4])
It's the same syntax, with the *:
do_stuff(*['a','b','c'])
This question already has answers here:
How can I use python's argparse with a predefined argument string?
(4 answers)
Closed 3 years ago.
My original code like the following works well with command line calls. However, is there a way that I could parse a list of arguments as a string in Python?
Example:
parser = argparse.ArgumentParser()
parser.parse_args()
And you call:
python test.py <args like --a 1 --b 2 ...>
Then if I have a string s which is the args list above, is there a way I could get that parsed?
You can split the string into a list and pass it to parse_args:
parser.parse_args(s.split())
Or if the string contains shell-like syntax such as quotes and escape characters, use shlex.split to split the string:
import shlex
parser.parse_args(shlex.split(s))
This question already has answers here:
Can a variable number of arguments be passed to a function?
(6 answers)
Closed 4 years ago.
I want to have an argument in a function that fills an array with multiple strings, so that i have
def Test(colors):
colorarray = [colors]
which i can fill with
Test(red,blue)
but i always get either the takes 2 positional arguments but 3 were given error or the single strings do not get accepted (e.g. TurtleColor(Color[i]) tells me "bad color string: "red","blue")
i know i can pass the strings as seperate arguments, but i kind of want to avoid having that many different arguments.
You need to read input arguments as a list
def Test(*colors):
colorarray = [*colors]
print(colorarray)
Test('red','blue')
This question already has answers here:
Bash: space in variable value later used as parameter
(4 answers)
How can the arguments to bash script be copied for separate processing?
(2 answers)
Closed 5 years ago.
I'm trying to parse arguments in a shell script for a python program. We're using a wrapper who takes the command line arguments and calls the correct python module.
For some reason, it keeps splitting up args I'm passing with double quotes. This is how I get the args:
args="$#"
Then I run:
runner.sh --host="XHOST 1"
or
runner.sh --host "XHOST 1"
the "$#" breaks the "XHOST 1" into 2 separate tokens of XHOST and 1.
First I've tried using the "$*" as always, but it didn't work. Now I'm using $# and it keeps splitting the args. This was tested on a rhel-7.2 machine.
Is there another way to parse shell args to try and keep them as one token when they are wrapped in quotes? Am I missing something here?
This question already has answers here:
Why use **kwargs in python? What are some real world advantages over using named arguments?
(8 answers)
Use of *args and **kwargs [duplicate]
(11 answers)
Closed 8 years ago.
I am reading Programming Python and can't figure out what the **D mean in the following codes:
>>> D = {'say': 5, 'get': 'shrubbery'}
>>> '%(say)s => %(get)s' % D
'5 => shrubbery'
>>> '{say} => {get}'.format(**D)
'5 => shrubbery'
I googled **kwargs in python and most of the results are talking about to let functions take an arbitrary number of keyword arguments.
The string.format(**D) here doesn't look like something to let function take an arbitrary number of keyword arguments because I see the dictionary type variable D is just one argument. But what does it mean here?
Argument unpacking seems to be what you're looking for.
**D is used for unpacking arguments. It expands the dictionary into a sequence of keyword assignments, so...
'{say} => {get}'.format(**D)
becomes...
'{say} => {get}'.format(say = 5, get = shrubbery)
The **kwargs trick works because keyword arguments are dictionaries.
Short answer, I'm sure someone will come up with a dissertation later on.
**D here means that dictionary D will be used to fill in the "named holes" in the string format. As you can see, {say} got replaced by 5 and {get} got replaced by shrubbery.
Actually, it is the same mechanism as the one used for passing an arbitrary number of parameters to a function; format expects as many parameters as the "holes" in the string. If you want to wrap them up in a dictionary, that's how you do it.
For more information, check keyword arguments and unpacking, in Python's documentation, as Prashant suggested.