2

Simply put, how can I differentiate these two in test.py:

python test.py 1
python test.py '1'

Workaround is OK.


Edit:

  1. This workaround looks cool but too complex: argparse
  2. Let the invoker specify args later, in python code use arg = input('Please enter either an integer or a string')
  3. And other workarounds as presented in the answers of this question.

Thank you all for the replies. Every body +1.

4
  • 2
    And something like optparse isn't an option, yeah? docs.python.org/library/optparse.html#standard-option-types Commented Aug 13, 2011 at 14:18
  • Would you consider making the first argument a format string? Something like "ssiiss" could let your python script know to expect two strings, followed by two integers, followed by two strings. Commented Aug 13, 2011 at 14:31
  • 1
    you must be joking calling standard lib, made exactly for such sort of tasks, a 'workaround'. Commented Aug 13, 2011 at 14:50
  • @Roman Bodnarchuk, I don't think argparse can solve the original problem as every body said it is impossible. Can it? So I call it a workaround. Commented Aug 13, 2011 at 14:58

6 Answers 6

4

The quotes are consumed by the shell. If you want to get them into python, you'll have to invoke like python test.py 1 "'2'" "'3'" 4

Sign up to request clarification or add additional context in comments.

3 Comments

The invoker of the python file is not me and I can't tell them to do that. They prefer 1 '2' '3' 4 and I have to differentiate those with quotes and those without quotes in python code
It's not possible to differentiate them. The quotes are shell syntax and is in no way part of how arguments are passed. Command line arguments are always strings and don't require/include quoting and escaping when on their own.
@Tyler Long: This is not possible. As ajwood said, " and ' are consumed by the shell. The Python interpreter will always get 1 2 3 4 as strings, no matter if they had quotes or not in the shell.
3

It is common handling of args, performed by shell. " and ' are ignored, since you may use them to pass, for instance, few words as one argument.

This means that you can't differentiate '1' and 1 in Python.

3 Comments

But you didn't answer my question: how to differentiate those with quotes and those without quotes in python code?
There is no way. Quotes are handled by shell, Python doesn't see them.
At least there is a way for windows. See Eugene Homyakov's answer
2

The shell command line doesn't support passing arguments of different types. If you want to have commands with arguments of different types you need to write your own command line or at least your own command parser.

Variant 1:

Usage:python test.py "1 2 '3' '4'"

Implementation:

command = sys.argv[1]
arguments = map(ast.literal_eval, command.split())
print arguments

Variant 2:

Usage:

python test.py
1 2 '3' 4'
5 6 '7' 8'

Implementation:

for line in sys.stdin:
    arguments = map(ast.literal_eval, line.split())
    print arguments

(Of course, you'd probably want to use raw_input to read the command lines, and readline when it is available, that's merely an example.)

A much better solution would be to actually know what kind of arguments you're expected to get and parse them as such, preferably by using a module like argparse.

Comments

2

Windows-specific:

# test.py
import win32api
print(win32api.GetCommandLine())

Example:

D:\>python3 test.py 3 "4"
C:\Python32\python3.EXE  test.py 3 "4"

You can then parse the command line yourself.

1 Comment

If I could accept more than one answers, yours is definitely among them. Thank you.
1

As you can see from your experiment, the quotes are gone by the time Python is invoked. You'll have to change how the Python is invoked.

2 Comments

Sadly it is almost impossible as I can't change the habit of the invokers.
How did they get a habit to do something that is not possible? Where did they gain it, because it can't be the shell? Have you considered writing your own command line?
0

I'm not sure how correct I am, but if you're using only integer command line arguments, you can typecast it to be int.

suppose (in *nix), I run my program as:

./test.py 1

I can in my program say something line

import sys
def main():
  a=int(sys.argv[1])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.