Question
****************************** PYTHON *********************************** import argparse import sys import os #import ipdb import traceback program_name=ECC108-Lab06-TestApp program_version=v1.0 program_name_version = %s %s % (program_name, program_version) class Lab06_TestApp( object
****************************** PYTHON ***********************************
import argparse
import sys
import os
#import ipdb
import traceback
program_name="ECC108-Lab06-TestApp"
program_version="v1.0"
program_name_version = "%s %s" % (program_name, program_version)
class Lab06_TestApp( object ):
def __init__( self, config_file, input_files, output_file ):
#
print( "" )
print( "Input parameters: -----------------" )
print( "Config: {}".format( config_file ) )
print( "Inputs: {}".format( input_files ) )
print( "Output: {}".format( output_file ) )
def check_filepaths( input_files, output_file ):
"""
"""
#ipdb.set_trace()
all_filepaths = list( map( lambda p: os.path.abspath( p ), input_files ) )
print( "--- All absolute filepaths: {}".format( all_filepaths ) )
if os.path.exists( output_file ):
#
print( "" )
print( "### Output file is an existing file: {}".format( output_file ) )
print( "" )
print( " This program will NOT overwrite existing files unless forced" )
print( " to do so. To force this, please use argument -f or --force " )
print( " to override this behavior. Make sure that output file path" )
print( " will not cause a needed file to be overwritten." )
print( "" )
return False
else:
return True
def process_commandline_arguments():
"""
Processes command-line arguments and returns them.
"""
parser = argparse.ArgumentParser( description="
parser.add_argument( 'input_files', nargs='*',
help='Input file name/path' )
parser.add_argument( "--version", action="version",
version=program_name_version,
help="Print the version of this program." )
parser.add_argument( "--debug", dest="debug", action="store_true",
help="Turn debugging on (default=off)." )
parser.add_argument( "-v", "--verbose", action="store_true", dest="verbose",
help="Turn verbose on (default=off)" )
parser.add_argument( "-f", "--force", action="store_true", dest="force",
help="Force overwriting of existing files (default=off)" )
parser.add_argument( "--config", dest="config", default=None,
help="Specify configuration file path" )
parser.add_argument( "--outfile", dest="outfile", default="output.dat",
help="Specify the path of the output file" )
args = None
try:
args = parser.parse_args()
print( "--- args: %s" % args )
except SystemExit as se:
sys.exit( 1 )
except Exception as e:
sys.exit( 1 )
#import pdb
#pdb.set_trace()
return args
try:
args = process_commandline_arguments()
if args.input_files:
#
print( "--- %s starting ..." % program_name_version );
if not args.force and not check_filepaths( args.input_files, output_file=args.outfile ):
#
sys.exit( 0 )
##if not args.config:
## #
## module_dir = os.path.dirname( __file__ )
## args.config = os.path.join( module_dir, "lab06-testapp-config.ini" )
testapp = Lab06_TestApp( args.config, args.input_files, args.outfile )
print( " Done." )
else:
print( " *** Please enter required arguments and retry. " )
except Exception as e:
print( "*** Error: {}".format( e ) )
print( traceback.format_exc() )
print( "--- Exiting without generating an output file." )
sys.exit( 1 )
Exercise Now do the following . Add a new parameter called check with a default value of" (empty string) Demonstrate that you can grab the value passed in the check argument
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started