My ArgParse for Python 2.2

Pythons argparse module is very helpful when building command-line tools and so when I worked on Python 2.2 and couldn’t find it, I thought of creating one, a very lite version, for the most basic feature/functionality.

It’s not a drop-in replacement, as I did not implement all the features and didn’t intend to — I just want something for collecting command-line parameters, both positional and keyword based arguments like the real one.

I won’t go into details as to what argparse is and/or what it’s used for since you can just Google it, but if you’ve used this before and like me, couldn’t use it on Python 2.2, then this might be useful for you.

Proof of Test

Here’s a very simple demo of how to use it.

Help!

Passing no argument or passing either “-h” or “–help” would produce the help text:

Passing values

Now we’ll pass both positional and keyword based arguments to it.

Note that it also works even if you add keyword first, then positional next. In this demo, the “pinoykape” is a positional argument but is passed after a keyword argument.

Main code

Here are the lines of code for the above demo:

Summary

As mentioned, this isn’t intended to be a drop-in replacement of the real argparse module. I just wanted something like that for my projects written in Python 2.2 so I created something similar and use the same name so it’s easier to migrate the code from 2 to 3 or vise versa.

Source Codes

The source codes for the demo and the argparse itself is on my GitHub page below (feel free to download and use it):

https://github.com/vegitz/codes/tree/master/0033%20ArgParse%20in%20Python2

Leave a Comment