Lab 04 - Command Line Arguments
Due by 11:59pm on 2023-07-06.
Starter Files
Download lab04.zip. Inside the archive, you will find starter files for the questions in this lab.
Topics
Intro to Command Line Arguments
Let's say that we are coding a program that calculates pay given rate and hours:
def calculate_pay(rate, hours):
return rate * hours
rate = 5.5
hours = 45
print(calculate_pay(rate, hours)) # 247.5
Notice that if we ever want to calculate pay for multiple people, we have to manually go into our code and change the rate and hours variables we call calculate_pay with. There is a better way
Instead of manually going into our code and executing our code in the command line, we can merge the two steps by allowing our code to take additional command line arguments within the terminal where 5.5 and 45 are arguments:
python3 program.py 5.5 45
247.5
To do this in our code, we have to import the sys library. To have access to the argument values, we use sys.argv. For example, if we execute python3 example.py hi im a command line argument 1 in the command line with the following code:
import sys
print(sys.argv) # ['example.py', 'hi', 'im', 'a', 'command', 'line', 'argument', '1']
It would print ['example.py', 'hi', 'im', 'a', 'command', 'line', 'argument', '1']
Notice that the first command line argument is the python file's name 'example.py' and that the '1' was represented as a string.
We can edit our code to look something more like this to make it more versatile:
def calculate_pay(rate, hours):
return rate * hours
rate = float(sys.argv[1])
hours = float(sys.argv[2])
print(calculate_pay(rate, hours)) # 247.5
Recall that the
float()function converts an integer or string to a float -- a decimal number.
Discuss
Why would it be helpful to have your program accept command line arguments?
Consider the situation with the calculate_pay program.
What if I wanted to apply any filter to any image without manually changing the code?
Setting Command Line Arguments in Your IDE
Sometimes you might not want to run your code in the terminal, but instead want to run it from the IDE (with the Run button) but using command line arguments. Your IDE likely support this feature. The following will show you how to set it up in VS Code and Pycharm.
VS Code
Navigate to the python file you want to work with. Under Run select Add Configurations.... Select Python File
A launch.json file should have been created. Within the configurations key-value pairs, add "args": ["<arg1>", "<arg2>", ...]. Remember to put a comma after the previous key-value pair.
Now navigate back to your python file, and under the Run tab select Run Without Debugging
PyCharm
Near the top-right corner, click on the box with the name of your python file. Then click Edit Configurations...:
A new window should have opened. You might see Add New Configurations text; if so, click it and select Python File. Now input your command line arguments into the parameters box:
Now click Apply and OK near the bottom-right corner of the new window. You should be able to run your program with command line arguments when you click the Run button
Required Questions
Note: Like the previous lab, some of the functionality you write here will be used in Project 1. Make every effort to complete these problems in Lab where you have help from TAs and your fellow students to save you work on the project.
Q1: Printing Arguments
Create a file lab04.py that reads in the command line arguments and passes them into a function called print_args that prints one argument per line.
For example, if we run python3 lab04.py 1 2 3 4, it should print:
lab04.py
1
2
3
4
Q2: Flags
Add a function that checks to see, given a list of arguments, if the second argument is one of -p, -i, or -h and returns True if it is; otherwise, it returns False.
Add a second function called flags that takes in a list of arguments. If there is a valid flag in the proper spot, flags should do the following based on the flag:
-p- print out all the command line arguments after the -p (on separate lines)-i- print "Hello World"-h- prints out a help command with the following informationValid flags: -p : prints out all the command line arguments after the -p -i : prints "Hello World" -h : prints out a help command
Your main program should then do the following
- If there is a valid flag, call
flags - Else, call
print_args
For example:
> python3 lab04.py -i
Hello World
> python3 lab04.py foo bar baz
lab04.py
foo
bar
baz
> python3 lab04.py -p my 3 arguments
my
3
arguments
> python3 lab04.py -h
Valid flags:
-p : prints out all the command line arguments after the -p
-i : prints "Hello World"
-h : prints out a help command
Q3: File IO
Implement two additional flags -w and -r. If the flag is -w, write to a file specified in the next argument with the contents given in the arguments following the file name. Each of the contents should be on a new line. For example, running
python3 lab04.py -w output.txt Hello World
should give you a file output.txt with
Hello
World
If the user does not provide any content when trying to write, print No Content Provided and do not write to the file.
Note: Do not worry about the extra empty line created when writing to the file
If the flag is -r read from the file specified in the following arguments and print out each line in the file. For example, using output.txt from the previous example:
python3 lab04.py -r output.txt
Hello
World
Submit
If you attend the lab, you don't have to submit anything.
If you don't attend the lab, you will have to submit working code. Submit the lab04.py file on Canvas to Gradescope in the window on the assignment page.