rafa_rrayes
rafa_rrayes

Reputation: 165

How can I get the arguments on a command-line with vanila python?

lets suppose I have a file called this.py when I run this on the terminal

this.py

how can I get all the arguments passed? such as:

this.py -foo

I do not want to use argparse, just vanila python

lets say this.py is this:

#user/python_projects/this.py/
arguments = """ now here I need to find a way of a list of the arguments passed"""
def main(arguments):
    if arguments[0] == 'foo':
        print('bar')
    else:
        print('I like foo ):')
main(arguments)

If I use

this.py -foo

then the expected result is: 'bar'

Upvotes: 0

Views: 34

Answers (1)

richyen
richyen

Reputation: 10018

If you use sys, you should be able to make it work:

$ cat this.py 
import sys
arguments = sys.argv
def main(arguments):
    if arguments[1] == 'foo':
        print('bar')
    else:
        print('I like foo ):')
main(arguments)

$ python this.py foo
bar

Note that your example used argument[0] when I think you really wanted argument[1]

Upvotes: 1

Related Questions