Topic
Today’s topic is Command-Line Arguments.
All the purple boxes below indicate things to write on your sprint notes.
Purpose
| Learning: | use command-line arguments with error handling |
|---|---|
| DNA Project: | accept two file names as command-line arguments |
Sandbox
Paste this code into a new file called two_words.py.
import sys
print("You gave me", len(sys.argv), "command-line arguments.")
if len(sys.argv) != 3:
print("Usage: python two_words.py [first word] [second word]")
sys.exit()
print("The program name is:", sys.argv[0])
print("The first word is:", sys.argv[1])
print("The second word is:", sys.argv[2])
print()
print("Here are your words again:")
for word in sys.argv[1:3]:
print(word + " ")
Now run that program from the command line 4 times, like below.
# python two_words.py
# python two_words.py Computer
# python two_words.py Computer Science
# python two_words.py Computer Science Rocks
Hint
After each run, save some typing by pressing the
up-arrowkey and then just adding one word.
Write in your sprint notes…
three things you notice or wonder about
two_words.pyand its output.
Walkthrough
- Grab the walkthrough paper from the classroom.
- Annotate the walkthrough paper while you watch this video (2:00:35 - 2:03:46).
Before continuing:
Check the box in your sprint notes to indicate that you annotated the paper.
You might find this paper helpful in the exercises below.
After that, hang on to this paper because you’ll use it again on Assessment Day.
Exercises
dna.py
2-Approaching
- Open the
dna.pyfile provided in the distribution code. (Don’t have the distribution code yet? See here.) - Under
# Check for command-line usage, write a Guard Clause that requires 3 arguments to pass. Meaning: If there are not 3 arguments, print a usage message and exit - After passing the Guard Clause, show a user-friendly log message indicating the names of the two input files that will be used.
Hint
Try this on your own first, then have a look at the STEP 1 of the sample solution code to check and fix your code.
Write in your sprint notes…
Why is it good for a Guard Clause to print a usage message?
hello.py
3-Proficient
- Copy/paste the code below into a new file called
hello.py. - Update the code according to the comments.
# DNA Exercise: hello.py
# - Takes a name as command line argument
# - Uses a Guard Clause to print a usage message and exit (if needed)
# - Prints "Hello, [name]!"
import sys
if len(sys._____) ___ ___:
print("Usage: python _____ _____")
sys.______
print("Hello", _______)
Write in your sprint notes…
What happens if the user just runs
python hello.py?
repeat.py
4-Distinguished
- Copy/paste the code below into a new file called
repeat.py. - Write code according to the comments.
# DNA Exercise: repeat.py
# - Accepts a word and number (e.g. "python repeat.py hello 3")
# - Prints the word that many times
# - Uses guard clauses for validation
# - Converts string number to integer safely
import sys
Write in your sprint notes…
What’s a code snippet that is important to the functionality of your program?
calc.py
5,000 Fake Bonus Points
- Copy/paste the code below into a new file called
calc.py. - Write code according to the comments.
# DNA Exercise: calc.py
# - Accepts 3 args: two numbers and an operator (+,-,x,/)
# - Uses guard clauses to check that the user provided
# a number, a valid operator, and another number
# - Prints "Usage: python calculator.py num1 op num2" if invalid
# - Prints the expression and its result
# - Handles divide-by-zero gracefully
Note
For this exercise, accept
xfor multipy, not*. The reason? The shell, where you type the command-line arguments, treats*as a special character. If you type*as a command-line argument, the shell will replace your*with a list of all the filenames in the current directory and send that to python. That’s definitely not what we want! So ask the user to usexif they want to multiply.
Write in your sprint notes…
How many times does your program contain the keyword
if?