Sample Video Frame
Exercise 4: Dealing with Command Line Arguments
Before you can work on this first part of the book you need to do some quick hacking that teaches you about command line arguments in Python.
Traditionally we call this kind of hacking a "spike". The term comes from doing a small test project that covers all of the elements of a larger process or project. This little test hack "spikes" through everything to make sure you can use it. The purpose of a spike is to figure out how to use some new library or tool in a dress rehearsal before you sit down to really use it in your project.
This is also the first exercise that has the "Challenge Format". The Challenge Format is designed to make you figure out how to do something, and then you can come and see how I did it compared to your work. I don't give you the code first and have you type it in. Nope, that's for beginners and you're no longer a beginner. You are now reading a challenge and then you have to solve it.
WARNING: Read this very carefully! You are not expected to complete a fully working publishable piece of software in 45 minutes. The 45 minute time limit is to make you get going and stop worrying that you'll do it wrong. It's a push to make you work and not intended as a test. This means that if you look at the 45 minute time frame and then freeze because you think you can't get a monumental piece of beauty completed you are doing this wrong. You should look at this as, "Let's see what I can get done in 45 minutes." These exercises are open ended because different people will complete different amounts of work in the given time. You are simply using the time constraint to figure out how you work, not to figure out if you are a terrible programmer or a great one.
Exercise Challenge
You are to write two tiny Python scripts that test out processing command line arguments using two methods:
Plain old
sys.argv
like you would do normally.Python's
argparse
package, for more fancy argument handling.
Your test script should be able to handle the following situations:
Getting help with
--help
or-h
.At least three arguments that are flags, meaning they don't take an extra argument but simply putting them on the command line turns something on.
At least three arguments that are options, meaning they do take an argument and set a variable in your script to that option.
Additional "positional" arguments, which are a list of files at the end of all the
--
style arguments and can handle Terminal wildcards like*/.txt
.
Since this exercise is a spike you should have the attitude that if something is a pain during your test you can abandon it and try the other thing. Start with trying to solve this with sys.argv
, and then if you just can't figure it out try argparse
instead.
Remember that this is a timed 45 minute exercise and you need to stick to that. You must also keep track of everything you do to get started. Your purpose with this exercise is to figure out how you keep getting in your own way to start a project. Are you talking yourself out of it before you even start? Do you not know where your text editor is or how to use it? Write it down, and then figure out how to remove that friction.
However, do not confuse this strict 45 minute exercise with failure. You are attempting to do something, anything, in 45 minutes. If your skill level is such that you get an ex4.py
file written and nothing else then you did something in 45 minutes. You should then take a look at why all you did was start that file, figure out what you need to do next time, and then attempt another 45 minute session.
Solution
To keep you from cheating, the code to all of the solutions is in the book's project site at http://bit.ly/lmpthwsolve, which is hosted on https://github.com/. Rather than include the code here so you are tempted to cheat and just take a little peek at it, you'll have to go check out the project and navigate to the ex4
directory to see how I did this hack. You'll also find my notes on how I did my start and what I could improve.
WARNING: Remember, if you get stuck to refer back to to Part III and use the Process for Early Coders I gave you. You make a list, do the list, check what you did. That's it.
Study Drills
How many other Python argument parsing libraries are there? Is there one you like better?
What's the main advantage of
argparse
oversys.argv
.What are things you can improve in your project starting method? Is there something you can eliminate now?
Register for Learn More Python the Hard Way
Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.