It’s often easy to run your Python scripts from the command line. Sometimes, your scripts may require a form of input to run- here’s a two-minute recipe to use the ArgumentParser module in Python to allow you to enter these inputs like the following:
$ python script.py --input <input>
# can use input in script
And then using these inputs in your program. Easy/quick documentation for these things can be hard to come by, so I’m making some notes here for reference.
- First, install the argparse module- this is built in for most standard versions of Python, but run this to be sure you have it.
$ pip install argparse
- Say we wanted to use argparse to input a string value of someone’s name, for our Python script hello.py that says hello and happy birthday to an input person for an input age:
- First, we import the argparse model and instantiate an object:
- Next, we add the argument, ‘name’, to our argparse object with the add_argument function with the following parameters:
- “-n” and “–name” serve the same function, that is, they will indicate the parameter the user is entering, which becomes important if we have multiple parameters. “-j” is simply the shorthand abbreviation that can also be used to indicate that the next value entered is the argument desired.
- type: indicates the data type desired, will usually be string (str), float, integer (int) as needed
- default: the default value of the input that is assumed if nothing is input by the user. So, if the user simply enters ‘python script.py’ in the command line, then the default values will be used to run the program.
- help: the user can simply enter ‘–help’ to see this helper string, along with the helper strings of all your inputs. Useful to explain requirements of the sought input here, and an example.
If the user types
python script.py --help
They will see:
usage: script.py [-h] [-n NAME] [-a AGE]
Script to greet and wish happy birthday.
optional arguments:
-h, --help show this help message and exit
-n NAME, --name NAME Name of person to greet. ex: Monty
-a AGE, --age AGE Age of person to greet. ex: 19
Sweet! Now how do we use these values into our program? If we use the parse_args() function, the entered inputs can be loaded into a python dictionary.
So, for a test run (here’s the full code):
And here’s the output of running the script in Terminal:
$ python script.py -name Monty -age 100
Happy 100th Birthday Monty!