20

I've tried to learn how argparse.ArgumentParser works and I've write a couple lines for that :

global firstProduct
global secondProduct 
myparser=argparse.ArgumentParser(description='parser test')
myparser.add_argument("product1",help="enter product1",dest='product_1')
myparser.add_argument("product2",help="enter product2",dest='product_2')
args=myparser.parse_args()
firstProduct=args.product_1
secondProduct=args.product_2

I just want to that when User run this script with 2 parameters my code assign them to firstProduct and secondProduct respectively. However it doesn’t work. Is there anyone to tell me why? thanks in advance

Mark Davidson
5,4996 gold badges37 silver badges54 bronze badges
asked Aug 20, 2013 at 12:57
1
  • 5
    You should describe the error, not just say 'it doesn't work'. The error message: "ValueError: dest supplied twice for positional argument" is important. unutbu's answer addresses that. Commented Aug 20, 2013 at 15:14

2 Answers 2

24

Omit the dest parameter when using a positional argument. The name supplied for the positional argument will be the name of the argument:

import argparse
myparser = argparse.ArgumentParser(description='parser test')
myparser.add_argument("product_1", help="enter product1")
myparser.add_argument("product_2", help="enter product2")
args = myparser.parse_args()
firstProduct = args.product_1
secondProduct = args.product_2
print(firstProduct, secondProduct)

Running % test.py foo bar prints

('foo', 'bar')
answered Aug 20, 2013 at 13:01
Sign up to request clarification or add additional context in comments.

5 Comments

thanks a lot. Here is 2 more question. 1) writing product_1 with double quates or single differs ? 2) what is the difference between positional argument and the others ?
The dest parameter can be used with the optionals (ones starting with -)
(1) There is no difference in the result if you specify strings with single quotes or double quotes. Having both options simply helps with quoting quotes: "'" or '"' for example. (2) When the first argument to add_argument starts with - or --, it is an optional argument. Without the dash, it is a positional argument. Positional arguments are recognized by their position: test.py foo bar interprets foo as being in the first position, so it is associated with the first positional argument, product_1
a strange behavior to me that looks a bug. if I use a character '-' into a positional parameter name, form example 'my-name' it doesn't look to be changed as a dest with '-' replaced to '_' ; since it cannot be used explicitly 'dest' param, there is no way to access the value! If i use str() to dump 'args' returned, I obtain something lie Namespace( ... , my-name=1, ....) but i wouldn't be possibile to python to accept such a variable! In fact is a syntax error to write args.my-name=xxxx
@bzimage: You could access the value using args.__dict__['my-name'].
16

In addition to unutbu's answer, you may also use the metavar attribute in order to make the destination variable and the variable name that appears in the help menus different, as shown in this link.

For example if you do:

myparser.add_argument("firstProduct", metavar="product_1", help="enter product1")

You will have the argument available for you in args.firstProduct but have it listed as product_1 in the help.

bryant1410
6,5104 gold badges42 silver badges43 bronze badges
answered Nov 30, 2015 at 19:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.