[Baypiggies] importing variables into python namespace using argparse module

Rami Chowdhury rami.chowdhury at gmail.com
Wed Sep 7 22:55:44 CEST 2011


On Wed, Sep 7, 2011 at 21:11, Abhishek Pratap <abhishek.vit at gmail.com> wrote:
> This might sound naive as I am trying to learn python.

Not at all, we've all been there :-)

> I am using argparse to parse the command line arguments but I am not
> sure how they variables created by it are imported into python's
> current namespace for downstream usage.

The simple answer is that they're not. I'll explain further after the
code sample

> parser = argparse.ArgumentParser(description='Process some integers')
>
> parser.add_argument('--file_1', nargs=1 , type=file, required=True,
> help='Name of file 1')
> parser.add_argument('--file_2', nargs=1 , type=file, required=True,
> help='Name of file 2')
> parser.parse_args()

Here, you need to capture the object that is returned from
parser.parse_args() -- that object is where any data captured by the
parser is stored, and you can access it from that object. So, for
instance:

>>> args = parser.parse_args()
>>> print "File 1 is %s" % args.file_1

> PS: Please let me know if this is not a appropriate forum to push such
> questions and if there are other mailing list that I could use coz in
> the coming days I am sure to send in a lot of email traffic.

You could also try the main Python-language mailing list
(python-list at python.org) -- there are more people on the list and you
might get quicker responses to questions :-)

Hope that helps,
Rami

-- 
Rami Chowdhury
"Never assume malice when stupidity will suffice." -- Hanlon's Razor
+44-7581-430-517 / +1-408-597-7068 / +88-0189-245544


More information about the Baypiggies mailing list