LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-10-2014, 04:43 AM   #1
johncsl82
Member
 
Registered: Nov 2003
Location: Planet Earth
Distribution: Redhat, Gentoo, Mandrake, FreeBSD
Posts: 73

Rep: Reputation: 18
Question Python 2.7: using argparse for getting raw_input() data from command-line?


Hello People,

I am trying to use argparse to get raw input data which is similar to raw_input() function

My program:

Code:
#!/usr/bin/env python

#test.py

import sys
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-f', '--my-foo', help='foobar')
args = parser.parse_args()

print(args.my-foo)
when I execute test.py -f testing 123, it give me unwanted result which is error: unrecognized arguments: 123

then, I change my code to:

Code:
#!/usr/bin/env python

#test.py

import sys
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-f', '--my-foo', nargs='*', action='store', help='foobar')
args = parser.parse_args()

print(args.my-foo)
when I execute it this time, again, it give unwanted result which is ['testing', '123']

what I wanted was when I execute test.py -f testing 123, it will produce an output of testing 123 as string... which is similar to the raw_input() function, such as the program mention at below:

Code:
#!/usr/bin/env python

#raw_input.py

import sys

args = raw_input()

print(args)
Thanks


Last edited by johncsl82; 10-10-2014 at 07:55 AM.
 
Old 10-10-2014, 09:33 AM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,222

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Just call your existing script with:

Code:
test.py -f "testing 123"
As for your question about just taking the rest of the command-line as is as a string: it's actually impossible because the command-line arguments are already broken apart (into separate entries in sys.argv) by the time your program starts.

Last edited by dugan; 10-10-2014 at 10:03 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Python argparse question MTK358 Programming 1 12-18-2010 09:50 PM
Python: find-and-replace while typing in raw_input General Programming 4 05-03-2010 09:06 PM
is it possible to override the raw_input() command in python deathalele Programming 1 10-05-2008 04:26 PM
If no raw_input: (Python question) xadrith Programming 4 07-26-2008 07:21 AM
Suspending operations with raw_input (Python) xadrith Programming 1 07-24-2008 05:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration