简体   繁体   中英

Passing arguments for functions while opening python file from terminal in Linux

I'm trying to open a python script i have through the terminal in Linux, in the format as shown below:

$ python file_name.py a,b,c

Over here a,b,c are variables that i'm using in the code i've written for the program. Currently I'm getting input by asking the user to enter input using the command var_name = input("Enter input for a: ")

However i want to pass the value for these three variables while opening the file itself as mentioned in the format above . Will i have to make use of classes to do this?

The code i wrote is :

print"Hello!"
circle = []
n = input("Enter the number of elements in the circle 'N' :")

for i in range(0,n):
    circle.append(i)

print "The circle being formed is : " + str(circle)

m = input("Enter the value of M : ")
k = input("Enter the value of K : ")

index = m-1

while (len(circle)>k) : 
    del circle[index]
    index = index + (m-1)
    n = n - 1
    index = (index)%n 

print str(circle)

If you're on Python 2.7 (which is very likely) you can try the argparser

import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
               help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
               const=sum, default=max,
               help='sum the integers (default: find the max)')

args = parser.parse_args()
print args.accumulate(args.integers)

And to run:

$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM