1

I am providing command line arguments as

python tenant_apps.py {dbname} {username} {owner} {project}

and inserting specific name for every argument. But the owner may have both first name and last name or just a single name and if they enter that... how can we make sure that it is taken as one argument? Same question applies to project too. For example owner can be John Adams or just John.

Blckknght
  • 100,903
  • 11
  • 120
  • 169
Roxy
  • 127
  • 1
  • 7
  • 2
    Use quotes when running the program. The program itself doesn't need to change. – zondo Jul 09 '18 at 22:56
  • How the shell passes arguments isn't your program's problem, nor is it even anything your program can control. – Ignacio Vazquez-Abrams Jul 09 '18 at 23:00
  • @zondo: Tried doing that...but then do I have to apply some condition for space between names because still its now working – Roxy Jul 09 '18 at 23:02
  • 1
    Can you show me how you're running it (with an actual example that doesn't work.) – zondo Jul 10 '18 at 00:36
  • This **isn't actually a question about Python**; it's a question about the shell. Deciding which words go into which command-line arguments is done by the terminal, **before the program starts**. – Karl Knechtel Jan 18 '23 at 11:32

1 Answers1

2

As zondo was saying, using quoted args on the command line will allow you to pass in a string containing spaces as a single argument.

Some simple code to demonstrate:

import sys
print("Commnd line arg values: %s" % sys.argv[1:])

Entering the following command line with some quoted parameters will give:

> python myscript.py aaa bbb "ccc ddd" eee "fff ggg hhh"
Commnd line arg values: ['aaa', 'bbb', 'ccc ddd', 'eee', 'fff ggg hhh']
Todd
  • 4,669
  • 1
  • 22
  • 30