yiwangai1983 / argparse

Automatically exported from code.google.com/p/argparse
Other
0 stars 0 forks source link

positional arguments with hyphens not correctly mapped to Namespace object attribute with underscores #64

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
If an argument has a hyphen in it (e.g. "arg-name"), the resulting
Namespace object should have an attribute with the same name as the
positional argument, but with the hyphen substituted for an underscore
(e.g., "arg_name"). This works fine for optional arguments, but not for
positional arguments. For example:

#! /usr/bin/env python

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('pos-arg1', help='positional argument 1')
args = parser.parse_args()

# following fails:
# print(args.pos_arg1)
# AttributeError: 'Namespace' object has no attribute 'pos_arg1'

print(args.__dict__)
# {'pos-arg1': 'd'}

Original issue reported on code.google.com by jeetsukumaran on 12 Mar 2010 at 12:45

GoogleCodeExporter commented 8 years ago
Note that your 'pos-arg1' is actually the dest= argument, so just like for 
optionals, this is interpreted as the exact name to use. If you want something 
to display as 'pos-arg1' and be stored as 'pos_arg1', you should use 
add_argument('pos_arg1', metavar='pos-arg1').

Original comment by steven.b...@gmail.com on 23 Jul 2010 at 1:18