Posts

Showing posts from June, 2017

Something about Python Argparse

Its been decades I have been using sys.argv for simple tasks related to argument parsing. But the thing is, sometimes we don't realize how good something is unless we use it . Like the argparse module.

The official document got me totally confused. So I wrote my own tutorial. Let's get started.

So the module to be imported is 'argparse'. First thing to do is , create a parser object.


parser = argparse.ArgumentParser()
Then we can add what arguments we want . These are the requirements.
I want a username. I also want to set a default.  If nothing is provided, then the default user will be used. If User name is provided , then that should be used. We should tell the user about this requirement through some kind of help instruction.
parser.add_argument("-user",default='Admin')
So to pass a username, one needs to use the '-user' option. 

args=parser.parse_args()
Then we need to call the above. This will create a namespace.


>>>argsNamespace(u…