There are a few modules specialized in parsing command line arguments: getopt, optparse and argparse. optparse is deprecated, and getopt is less powerful than argparse, so I advise you to use the latter, it'll be more helpful in the long run.
Here's a short example:
import argparse
# Define the parser
parser = argparse.ArgumentParser(description='Short sample app')
# Declare an argument (`--algo`), saying that the
# corresponding value should be stored in the `algo`
# field, and using a default value if the argument
# isn't given
parser.add_argument('--algo', action="store", dest='algo', default=0)
# Now, parse the command line arguments and store the
# values in the `args` variable
args = parser.parse_args()
# Individual arguments can be accessed as attributes...
print args.algo
That should get you started. At worst, there's plenty of documentation available on line (say, this one for example)...
Answer from Pierre GM on Stack OverflowThere are a few modules specialized in parsing command line arguments: getopt, optparse and argparse. optparse is deprecated, and getopt is less powerful than argparse, so I advise you to use the latter, it'll be more helpful in the long run.
Here's a short example:
import argparse
# Define the parser
parser = argparse.ArgumentParser(description='Short sample app')
# Declare an argument (`--algo`), saying that the
# corresponding value should be stored in the `algo`
# field, and using a default value if the argument
# isn't given
parser.add_argument('--algo', action="store", dest='algo', default=0)
# Now, parse the command line arguments and store the
# values in the `args` variable
args = parser.parse_args()
# Individual arguments can be accessed as attributes...
print args.algo
That should get you started. At worst, there's plenty of documentation available on line (say, this one for example)...
It might not answer your question, but some people might find it usefull (I was looking for this here):
How to send 2 args (arg1 + arg2) from cmd to python 3:
----- Send the args in test.cmd:
python "C:\Users\test.pyw" "arg1" "arg2"
----- Retrieve the args in test.py:
import sys, getopt
print ("This is the name of the script= ", sys.argv[0])
print("Number of arguments= ", len(sys.argv))
print("all args= ", str(sys.argv))
print("arg1= ", sys.argv[1])
print("arg2= ", sys.argv[2])
Is it possible to pass arguments into a Python script? - Unix & Linux Stack Exchange
Passing argument to function from command line
Argument passing in python script
Pass an argument to a Python script
Videos
I am trying to modify an already existing code (https://github.com/ohyicong/decrypt-chrome-passwords/blob/main/decrypt_chrome_password.py) to take in command line arguments an example: python script.py "path_to_Local_State_file" "path_to_Login_Data_file" I think that this should be a pretty simple fix but chatGPT is not giving me the correct code when I test it how should I approach this.
This worked for me:
import sys
firstarg=sys.argv[1]
secondarg=sys.argv[2]
thirdarg=sys.argv[3]
You can use the argv from sys
from sys import argv
arg1, arg2, arg3, ... = argv
You can actually put an abitrary number of arguments in the command line. argv will be a list with the arguments. Thus it can also be called as arg1 = sys.argv[0] arg2 = sys.argv[1] . . .
Keep also in mind that sys.argv[0] is simply the name of your python program. Additionally, the "eval" and "exec" functions are nice when you use command line input. Usually, everything in the command line is interpreted as a string. So, if you want to give a formula in the command line you use eval().
>>> x = 1
>>> print eval('x+1')
2
You can also use the sys module. Here is an example :
import sys
first_arg = sys.argv[1]
second_arg = sys.argv[2]
def greetings(word1=first_arg, word2=second_arg):
print("{} {}".format(word1, word2))
if __name__ == "__main__":
greetings()
greetings("Bonjour", "monde")
It has the behavior your are looking for :
$ python parse_args.py Hello world
Hello world
Bonjour monde
Python provides more than one way to parse arguments. The best choice is using the argparse module, which has many features you can use.
So you have to parse arguments in your code and try to catch and fetch the arguments inside your code.
You can't just pass arguments through terminal without parsing them from your code.
How do I pass an argument to a Python script? In this case, a string, which is a filename. How do I call it at the command prompt, and how do I reference the passed argument within the script?