Vincent Morris
Vincent Morris

Reputation: 670

How to pass a string as an argument in python without "namespace"

How do you pass a string as an argument in python without the output containing "Namespace".

Here is my code:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("person", help="person to test",type=str)
person = str(parser.parse_args())
print "The person's name is " + person 

Here is the command I am running:

python test.py bob

This is my output:

The person's name is Namespace(person='bob')

I could probably do some fancy splitting, but I'm sure I'm just leveraging the argparse incorrectly(I'm new to python) if anyone can please tell me how to properly pass a string like this I would greatly apreciate it.

Upvotes: 3

Views: 10265

Answers (1)

FamousJameous
FamousJameous

Reputation: 1585

The namespace is a container for the results of argument parsing. You can access the individual argument values by looking them up in the namespace:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("person", help="person to test",type=str)
args = parser.parse_args()
print "The person's name is " + args.person

The docs have some great examples that show how to use argparse.

Upvotes: 11

Related Questions