[Tutor] ArgumentParser - Command Line arguments

Mats Wichmann mats at wichmann.us
Wed Nov 20 09:35:26 EST 2019


On 11/20/19 5:22 AM, Asad wrote:
> 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.

You have defined a parser to accept two positional arguments, one of 
which is optional. Positional arguments are just that: they are 
collected in the position in which they are declared.  That means your 
usage looks like this:

usage: [-h] first [second]

giving it one more argument means it's an error, and you indeed get that 
errror.

if you want to have it collect just "known" things and you deal with the 
rest yourself, use parse_known_args instead of parse_args - that will 
give you back the matched args filled in, plus an array of everything 
left over, which you can then ignore. So far so good...

> 
> 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

this you can't do with positional args: the first positional argument 
you declare will always collect the first word on the command line (that 
wasn't first picked up by the options processing). Meaning this:

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

is what it does anyway. not sure what that section is for, since you're 
just repeating what you already had argparse do...

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

since presumably file1 is a variable name, that is it won't be exactly 
"file1" except for your testcase, how would it know this?

if you want to collect things in an order-independent way, you should 
use options; positional arguments are always ordered.  Or, just ignore 
argparse and do it all manually from sys.argv, which it looked like you 
were starting to do anyway.  I don't see that argparse is adding you any 
value here. However, to look at helping this out with an option:

parser.add_argument("file1", type=str, help='Input for sqlinvocation.log')
parser.add_argument("--file2", nargs=1, help='patch file')

now you can run as all of these and it should work out:

python script.py somefilename
python script.py --patch=patchfilename somefilename
python script.py somefilename --patch=patchfilename




More information about the Tutor mailing list