+1 vote
in Programming Languages by (13.2k points)

Is there any Python module that can be used to access command-line arguments? I do not want to use the argparse module; just need a quick and dirty solution.

1 Answer

+3 votes
by (84.8k points)
selected by
 
Best answer

You can use the sys module to get the command-line arguments passed to the code. "sys.argv" captures all arguments including the program name. The first argument is always the program name itself. 

Here is the code with the sys module. You can pass as many arguments as you want to this code. From index 1 to the end, values will represent all arguments.

import sys

# Get all the arguments passed to the script (the first one is the script name itself)

args = sys.argv

# print arguments

if len(args) == 1:

    print("No arguments other name program name passed")

else:

    print("arguments passed are: ", args[1:])


...