LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 12-30-2015, 11:20 AM   #1
PoleStar
Member
 
Registered: Jul 2010
Posts: 231

Rep: Reputation: 2
python: argument parsing


I need to pass 3 arguments. like

Quote:
-c
-r
-g <some thing>
(-c and -r) is either or.
-g is a must with <some thing>

how that can be done?
 
Old 12-30-2015, 11:45 AM   #2
Ztcoracat
LQ Guru
 
Registered: Dec 2011
Distribution: Slackware, MX 18
Posts: 9,484
Blog Entries: 15

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
These links might help:-

https://docs.python.org/2/library/argparse.html
https://docs.python.org/2/howto/argparse.html

Sorry I don't know more. I haven't finished reading the Python book yet.
 
1 members found this post helpful.
Old 12-30-2015, 12:34 PM   #3
PoleStar
Member
 
Registered: Jul 2010
Posts: 231

Original Poster
Rep: Reputation: 2
Talking

Quote:
from optparse import OptionParser

parser = OptionParser()

parser.add_option("-c" , action="store_true" ,dest="check", help= "Only check" )
parser.add_option("-r" , action="store_true" ,dest="record", help= "Record data" )
parser.add_option("-g" ,dest="groupname", help= "Takes group name here" )

(options, args) = parser.parse_args()
This worked.

Thanks for the idea Ztcoracat.
 
1 members found this post helpful.
Old 12-30-2015, 08:49 PM   #4
Ztcoracat
LQ Guru
 
Registered: Dec 2011
Distribution: Slackware, MX 18
Posts: 9,484
Blog Entries: 15

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by PoleStar View Post
This worked.

Thanks for the idea Ztcoracat.
You're Welcome-
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Parsing Javascript To JSON Using Python 3 cin_ Programming 3 05-18-2015 04:08 PM
[SOLVED] Parsing a table using python simhumcon Programming 5 07-06-2014 04:07 AM
[SOLVED] python parsing nitrohuffer2001 Programming 7 04-01-2014 02:43 PM
[SOLVED] Parsing S-Expressions in Python MTK358 Programming 2 02-02-2011 08:00 AM

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

All times are GMT -5. The time now is 07:21 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