Question: The Goal You will make a program that prints a block of text, using command line arguments. The purpose is to learn a simple way

The Goal

You will make a program that prints a block of text, using command line arguments. The purpose is to learn a simple way to parse named and positional arguments. Here are several ways to invoke the program:

python argv.py X -width 5 -height 3

python argv.py X -height 3 -width 5

python argv.py -width 5 X

All of these should print the symbol X, repeated into a block 5 wide and 3 high:

XXXXX

XXXXX

XXXXX

There two kinds of arguments:

Named arguments. They come in pairs: a name, followed by a value. Positional arguments. They occur alone.

In the example above, the width and height are named arguments. The symbol being printed is a positional argument.

Names are marked with a dash (-). Thus the pair -width 5 means that "set width to 5". The pair -height 3 means "set height to 3".

Notice the third example: there is no mention of the width. In that case, we will use a default value for that variable. The default width will be 3.

Your Tasks

Goal: handle named and positional command-line arguments. We will work gradual ly towards this goal.

Begin by using a text editor, or IDLE, to edit the file argv.py.

Notice that you will NOT use IDLE to RUN the program. You will always run the program using the command line.

1. Implement the function print_args() . Just make a for loop that travels through the list slice sys.argv[1:], and prints each entry on a separate line. We are just looking at the arguments, so we skip sys.argv[0], which is the program name "argv.py".

Try it: go to the terminal, and type:

python argv.py X -width 5 -height 3

CAVEAT: if you have a Mac, it comes with python 2 installed. This course uses python 3. In order to get python 3, type python3 instead of python.

Shortcut: on the terminal, you often want to repeat the last command. Press the UP arrow (then maybe edit your command), and press ENTER. Try it!

2. Implement the function print_names() . You probably wrote something like

this:

for arg in sys.argv[1:] :

print (arg)

Instead of printing every argument, just print the names. First check if arg starts with a minus sign: if arg[0] == '-':

Then, instead of print (arg), discard the dash: print (arg[1:]) . Go to the terminal and type the same thing again:

python argv.py X -width 5 -height 3

3. Now you are ready to implement parse_arguments(). If you find a name, you have to access the argument after it. A for loop makes this awkward: a while loop is easier. Begin with this code:

index = 1

while index < len(sys.argv):

arg = sys.argv[index]

...

index += 1

If arg is a name, you should

figure out if the name is "width" or "height".

increment index, and retrieve the next argument (which is a value). remember to convert the value into an int!

change either the width or height variable.

If arg is not a name, then it's a positional argument. You should just store it into symbol.

4. If a user omits a parameter, it gets a default value. So, in parse_arguments(), give the three parameters width, height, and symbol the values 5, 3, and "X" initially (before you start the while loop).

5. At the end of parse_arguments(), return the three parameters, as a tuple:

return (width, height, symbol)

6. Last step: implement print_block(). It gets passed the three parameters, and prints a block of symbols, with no spaces, and a " " after each line.

Testing

I am supplying a program test_argv.py that tests your work. You should use it; we will use a similar program to grade your work. If, on your computer, typing python brings up python 2, you should edit this file, so that the calls to os.system() say "python3 argv.py ..." instead.

=====================================

test_argv.py

import os

os.system('python argv.py -width 10 -height 4 @')

os.system('python argv.py -height 4 -width 10 @')

os.system('python argv.py @ -height 4')

==================

argv.py

import sys

def print_block(width, height, symbol):

# Remove the following line

print ('print_block is not implemented')

def print_args():

# Remove the following line, obviously

print ("print_args is not implemented")

def print_names():

print ("Flags:")

# Remove the following line

print ("print_flags is not implemented")

def parse_arguments():

# Remove the following line

print ("parse_arguments is not implemented")

# Eventually, replace the following line with

# return (width, height, symbol)

return (2, 3, 'X')

def main():

print ('-----print_args----------------')

print_args()

print ('-----print_names--------')

print_names()

print ('-----parse_arguments-----------')

(width, height, symbol) = parse_arguments()

print_block(width, height, symbol)

if __name__ == '__main__':

main()

=====================

test_argv_out.text

-----print_args----------------

-width

10

-height

4

@

-----print_names--------

width

height

-----parse_arguments-----------

@@@@@@@@@@

@@@@@@@@@@

@@@@@@@@@@

@@@@@@@@@@

-----print_args----------------

-height

4

-width

10

@

-----print_names--------

height

width

-----parse_arguments-----------

@@@@@@@@@@

@@@@@@@@@@

@@@@@@@@@@

@@@@@@@@@@

-----print_args----------------

@

-height

4

-----print_names--------

height

-----parse_arguments-----------

@@@@@

@@@@@

@@@@@

@@@@@

! upload argv.py !

please help me out!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!