1

I am trying to pass a argument to command line for running a python program which takes list of Field Objects and send an email.

The program is running fine when the list of objects is already set in my program.

a=[{"name": "productname_0", "type": "html", "content": "A Cool Shirt"}, {"name": "productname_1", "type": "html", "content": "Some Nice Shoes"}, {"name": "productname_2", "type": "html", "content": "A Trendy Hat"}, {"name": "productprice_0", "type": "html", "content": "20.99"}, {"name": "productprice_1", "type": "html", "content": "50.99"}, {"name": "productprice_2", "type": "html", "content": "FREE"}]

sendmail(a)

But when I pass this list in command line, it did not work for me.

a =sys.argv[1]
sendmail(a)

Running Program from Command Line

mailscript.py [{"name": "productname_0", "type": "html", "content": "A Cool Shirt"}, {"name": "productname_1", "type": "html", "content": "Some Nice Shoes"}, {"name": "productname_2", "type": "html", "content": "A Trendy Hat"}, {"name": "productprice_0", "type": "html", "content": "20.99"}, {"name": "productprice_1", "type": "html", "content": "50.99"}, {"name": "productprice_2", "type": "html", "content": "FREE"}]
adnan
  • 504
  • 1
  • 4
  • 21
  • Did you try `python mailscript.py [{"name": "productname_0", "type": "html", "content": "A Cool Shirt"}, {"name": "productname_1", "type": "html", "content": "Some Nice Shoes"}, {"name": "productname_2", "type": "html", "content": "A Trendy Hat"}, {"name": "productprice_0", "type": "html", "content": "20.99"}, {"name": "productprice_1", "type": "html", "content": "50.99"}, {"name": "productprice_2", "type": "html", "content": "FREE"}]` ? – Shivam Jha Sep 26 '20 at 18:39
  • 2
    Passed arguments are always strings. You don't get a list in `argv` because the input looks like a list. You'd need to [parse](https://stackoverflow.com/questions/1894269/how-to-convert-string-representation-of-list-to-a-list) the string as a list first. – Carcigenicate Sep 26 '20 at 18:40
  • You may have to do some quoting.escaoping of for example “ and > in the commandline - start by printing what the arguments passed are and sort out the quoting so the printout is the string you need, then sort out the parsing of it into an actual lPython list. – DisappointedByUnaccountableMod Sep 26 '20 at 18:43
  • The type using argv to acquire value are `str` (you can use `type` to check) and need to parse the value back. In this case the str can be parse back to list of dict using `json.loads` – Darkborderman Sep 26 '20 at 18:46
  • Because your data only uses double quotation marks, you can use single quotation marks to quote the whole thing like this: `mailscript.py '[{....}]'`. – Wups Sep 26 '20 at 18:55
  • Hi @Divik, Itried json.loads but it says "json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)" – adnan Sep 26 '20 at 19:28
  • Hello @adnan can you provide the value of `argv` variable using `print(sys.argv)`? – Darkborderman Sep 26 '20 at 19:39
  • '[{name:productname_0, This is the value i am getting on print that variable – adnan Sep 26 '20 at 19:42
  • @adnan It looks like your argument are being cut so `json.loads()` fails. Try to send the argument completely(maybe it's the teminal character limit, etc). – Darkborderman Sep 26 '20 at 19:51
  • Any idea, what's causing this ? I am simply copying these commands on Command Line – adnan Sep 26 '20 at 19:57

1 Answers1

0

Inside your program use:

a = json.loads(sys.argv[1])
sendmail(a)

Execute your program as :

python mailscript.py '[{"name": "productname_0", "type": "html", "content": "A Cool Shirt"}, {"name": "productname_1", "type": "html", "content": "Some Nice Shoes"}, {"name": "productname_2", "type": "html", "content": "A Trendy Hat"}, {"name": "productprice_0", "type": "html", "content": "20.99"}, {"name": "productprice_1", "type": "html", "content": "50.99"}, {"name": "productprice_2", "type": "html", "content": "FREE"}]'
soumya-kole
  • 1,111
  • 7
  • 18