Reputation: 884
I have a python script where I need to pass multiple arguments. The first argument may have multiple emails, second is a string and the third is a string too.
Lets say I have the file
mytest.py:
def tfunc(em, st1, st2):
#### do blah blah blah
def main ():
eml = sys,argv[1].strip()
st1v = sys,argv[2].strip()
st2v = sys,argv[3].strip()
dosom = tfunc(eml, st1v, st2v)
if __name__ == '__main__':
main()
Now the way I am testing to run the function on command line is :
python mytest.py [email protected] value of file 1 value of file 2
So the arguments are actually [email protected] ( also need to know what will happen if I have multiple emails), "value of file 1" and "value of file 2"
In my actual final code the spaces will be there for both the arguments. However when I run the code it takes the email ( if only one) correctly, but takes the 2nd argument as value ( from the first argument) and of (from the first argument). But that's not what I want.
I am looking for the [email protected] as first argument, "value of file 1" as 2nd argument and "value of file 2" as third argument.
Can I get some help. Thanks in advance.
Upvotes: 5
Views: 24094
Reputation: 151
you can use argparse to pass your argument:
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-stv1', type=str)
parser.add_argument('-stv2', type=str)
parser.add_argument('-email', nargs='+')
args = parser.parse_args()
print(args)
nargs='+' indicate that at least you should pass one argument as email or more.
Executing the script give the following :
python3 script.py -email [email protected] [email protected] -stv1 "value of file 1" -stv2 "value of file 2"
Namespace(email=['[email protected]', '[email protected]'], stv1='value of file 1', stv2='value of file 2')
Upvotes: 0
Reputation: 428
First (and probably most preferable) solution is to put the second and third argument in quotes as Susmit Agrawal suggested. Then the shell itself will split the command line into arguments appropriately.
python mytest.py [email protected] "value of file 1" "value of file 2"
In case you really need to pass arguments without quotes though, you will have to accept that the shell will be splitting your second and third argument at spaces, so you will need to reconstruct them from sys.argv
yourself.
Lastly, you may want to explore argparse library to help you with parsing the command line arguments. In this case you may want to use optional arguments with nargs
set to '+'
or some certain number based on your command line API. For example, if you define and parse your arguments the following way,
parser = argparse.ArgumentParser()
parser.add_argument('--value-1', nargs=4)
parser.add_argument('--value-2', nargs=4)
parser.add_argument('email', nargs='+')
args = parser.parse_args()
print(args)
then you can call your Python program as
python mytest.py [email protected] --value-1 value of file 1 --value-2 value of file 2
And get the following result,
Namespace(email=['[email protected]'], value_1=['value', 'of', 'file', '1'], value_2=['value', 'of', 'file', '2'])
which you can then conveniently access as
print(args.value_1)
print(args.value_2)
print(args.email)
Upvotes: 5