Homework 2, Arguing with the Arguments.
Short Description:
Write a program that parses command line arguments and behaves differently depending on the value given.
Goals
When you finish this homework, you should have:
- Parsed command line arguments.
- Used argv[0] to determine program operation.
Formal Description
This is a purely academic exercise. Please read the spec carefully to make sure your program performs correctly.
For reasons to be specified later write a c++ program called hw2.cpp (executable hw2). Make this program behave differently based on
- The command line arguments.
- The name used to invoke the program.
Your program should recognize and respond to the following command line arguments.
- --arg n:
- If n is a valid argument number, add this argument to the list to print.
- If n is invalid print (but an integer)"Invalid argument count " followed by the value of n.
- If there is no argument for n print " "Usage: --arg n"
- You may assume that if n is given it is an integer and can be converted using
stoi.
- This argument may be given multiple times.
-
hw2 --arg 1 --arg 2 --arg 4 --arg 2 should print --arg, 1, --arg, 1. (But see the spec)
- Only print this after all arguments have been parsed.
- Print "The selected args are:"
- Print the selected arguments in the order given in the command line. (I used a vector of ints)
- Print an endline.
- --args or -a:
- By default this option is off.
- Print the header "The args are:"
- Print all of the command line arguments as specified below
- Print an endline.
- --var varname
- If varname is an environment variable name, add the corresponding var=value pair to the list.
- If varname is not supplied, print the error "Usage: --env varname."
- This can be ignored if there is no corresponding varname in the environment.
- This argument may be given multiple times.
- Only print this after all arguments have been parsed.
- --env or -e:
- By default this option is off.
- Print the header "The the environment is:"
- Print all of the command line arguments as specified below
- Print an endline.
The output format should be each line starts with a tab, then the next value to print.
[bennett@mirkwood closed]$ hw2 --arg 8 --arg 7 --arg 8 1 2 3 4 5
The selected args are:
2
1
2
The output order should be
- Special arguments requested
- All arguments, if requested.
- Special environment variables if requested
- All environment variables if requested.
- A blank line.
[bennett@mirkwood closed]$ hw2 --arg 0 --var HOME -a
The selected args are:
hw2
The args are:
--arg
0
--var
HOME
-a
The selected environment is:
HOME=/home/bennett
Your program should examine argv[0] and determine how it should execute.
-
hw5 should display nothing by default.
-
myarg should display the arguments by default (-a, --args)
-
myenv should display the environment by default (-e --env)
Remember, you can form a hard link by typing ln oldfile newfile. So you might want to ln hw5 myargs. However this is quite cumbersome, so this Makefile might help. When using this makefile, all indented lines start with a tab.
Notes
- You may parse the command line arguments any way you wish.
- You may use all of the standard library. I used vectors and find.
- You may write this in c if you wish.
Extra Credit
Any of the following will lead to extra credit being awarded.
- Search the environment for the variable MY_ARGS_ARGS which should contain a string of comma separated numbers. Use these for
-arg
- Search the environment for the variable MY_ARGS_ENV which should contain a string of comma separated variable names. Use these for
-env
-
[bennett@mirkwood closed]$ export MY_ARGS_ARGS=3,2,4,1,5
[bennett@mirkwood closed]$ hw2 --var MY_ARGS_ARGS
The selected environment is:
MY_ARGS_ARGS=3,2,4,1,5
[bennett@mirkwood closed]$ echo $MY_ARGS_ARGS
3,2,4,1,5
- Write this in python. Note, you must provide a c/c++ version.
- Write this in bash. Note, you must provide a c/c++ version.
Required Files
A single tar file containing the source code and makefile for this program.
Submission
Submit the assignment to the D2L folder Homework 2 by the due date.