Python: parsing command line parameters

I hava a small utility which accept some input from command line from another system. To keep the utility more open, i don’t want to restrict some parameters, the caller can provide more parameters, i will just need to ignore them. In Python, we use argparse for this job, by default if the user provide more parameters that you expect, it will trigger the error. There is a way to overcome this

Below is the script i use for this job,

 

parser = argparse.ArgumentParser()
parser.add_argument( '--url',default=None,dest='url')
parser.add_argument( '--id',default="id",dest='id')
parser.add_argument( '--script',default=None,dest='script')
parser.add_argument( 'args',nargs="*")  #this is how we can ignore all other parameters
args,unkown = parser.parse_known_args()

Leave a Reply

Your email address will not be published. Required fields are marked *