[Tutor] ArgumentParser - Command Line arguments

Asad asad.hasan2004 at gmail.com
Wed Nov 20 07:22:57 EST 2019


Hi All ,

          Need advice or code how to proceed . I am executing python script
as .

python script.py file1 file2

parser = argparse.ArgumentParser()
parser.add_argument("first", type = str, help = 'Input for
sqlinvocation.log')
parser.add_argument("second", nargs="?")

args = parser.parse_args()

if len(sys.argv) == 3:
    args.first = sys.argv[1]
    args.second = sys.argv[2]

else:

    print "enter the sqlinvocation log and qopatch or sqlpatch_catcon_0.log"

print("first:", args.first)
print("second:", args.second)




If I execute python script.py file1 file2 1.

It gives the following error :

error: unrecognized arguments: 1

I few questions . How can perform the following :

1) How can the python script process file1 and file2 and run the script
with those command line arguments and drop 1 and other inputs if provided
by user.

2)How can the script to consider always  file1 as args.first even if the
command is executed as :

python script.py file3 file1

Somehow if its find the file1 in the list of arguments it should consider
as args.first and the other file as args.second


Thanks,



-- 
Asad Hasan
+91 9582111698


More information about the Tutor mailing list