Contents

  1. command_line_args.py

command_line_args.py

# Demonstrate use of command-line arguments
# by Sara Sprenkle
# Usage: python3 command_line_args.py filename

import sys

PROG_ARG_LOC=0
FILENAME_ARG_LOC=1

# Make sure there are a sufficient number of arguments
# If not, exit.
if len(sys.argv) < 2:
    print("Error: invalid number of command-line arguments")
    print("Usage: python3", sys.argv[PROG_ARG_LOC],"<filename>")
    sys.exit(1)

# Get the 2nd element from the command line and use that as
# the filename    
filename = sys.argv[FILENAME_ARG_LOC]
print("We will use file", filename, "as input")


Generated by GNU Enscript 1.6.6.