0

I'm trying to parse the command line arguments in a very simple way:

$ python main.py --path /home/me/123

or

$ python main.py --path=/home/me/123

And then:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--path')
args = parser.parse_args()

And args returns nothings:

(Pdb) args
(Pdb) args.path

How can I access the value of --path?

Eric
  • 2,636
  • 21
  • 25

2 Answers2

0

You can print args.path and it will show your line argument. For more details you can check the below link for more details about argparse

You can also use sys to parse your command line arguments, such as

>>> import sys
>>> path = sys.argv[1]  # sys.argv always start at 1
>>> print path

Check the below link for more details.

Hope it helps.

Eric
  • 2,636
  • 21
  • 25
  • 1
    For trivial input parameters it is ok, but I would not suggest it for more complex things. – wenzul Aug 18 '15 at 06:46
0

It works fine for me...

>>> args
Namespace(path='/home/me/123')

So you can access it via args.path

wenzul
  • 3,948
  • 2
  • 21
  • 33