ICAB4220A Create scripts for networking- Task 1 notes
Task 1 - Notes on parsing command-line arguments
Overview
In this task you will write the code to deal with the command line options for the event logging software.
The users of the event logging program can specify two things on the command line:
- The type of events to look for, either 'error', 'warning' or 'information', or 'all'. This will be specified in the '-t' or '-type' option.
- The period of time in hours to look back in the log. This will be specified in the '-g' or -'goback' option.
For example, if our program is called like this:
showEvents -t info -g 100
then the program will print the last 100 hours of events of type 'information' from the Windows logs.
Guidelines for this task
To complete this task, you will need to submit your command-line parser program, complete with a mock-up of the html output.
Here is a summary of the steps you will need to take:
Step 1
Using the 'commandline3.py' program as an example, write a simple command line parser for the options of our event log query program. For example, if the user types:
showEvents -t warning -g 100
then your program should print:
type = warning
goBack = 100 hours
You can assume that the options will be given in the exact order shown, but you should check for the presence of the -t and -g to avoid errors. You program will assign the event type to a variable called 'eventType', and the number of hours to go back to a variable called 'goback'. The program will simply print out the values of these two variables as shown.
Step 2
Next, use the Python 'optparse' module to process the command line. You can use the 'orderpie.py' program as an example (see the downloads available under 'Your tools for this task').
This module will allow you to enter the options in any order, will allow both long and short options (for example -g or -goback) and will assign the option values to variables automatically.
Step 3
Lastly, modify your program to open the mock-up HTML output file (see 'logfile.htm' for an example - under 'Your tools for this task').
If you examine this file, you will see that it simulates the output for eventType = 'all' and goBack = 24. You should launch this file only if the actual entered command line options match these values.
icab4220a_task1_notes.doc
© State of New South Wales, Department of Education and Training 20061