Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 202
Vincent Morris Avatar asked Sep 16 '25 19:09

Vincent Morris


1 Answers

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.

like image 160
FamousJameous Avatar answered Sep 18 '25 09:09

FamousJameous