LinuxQuestions.org
Help answer threads with 0 replies.
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 03-18-2010, 10:06 PM   #16
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244

Quote:
Originally Posted by theNbomr View Post
Is optparse Python-specific, or are there generic bindings as well?

--- rod.
what do you mean? optparse supports the most common command-line syntax and semantics conventionally used under Unix.
 
Old 03-19-2010, 01:57 AM   #17
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ghostdog74 View Post
what do you mean? optparse supports the most common command-line syntax and semantics conventionally used under Unix.
I think theNbomr was interested in C/C++ code and I think he meant the Python module was based on that generic C/C++ code.
 
Old 03-19-2010, 04:37 AM   #18
dsmyth
LQ Newbie
 
Registered: Mar 2010
Location: Glasgow, Scotland
Distribution: Fedora 12
Posts: 26

Original Poster
Blog Entries: 6

Rep: Reputation: 17
Quote:
Originally Posted by MrCode View Post
I dunno about Python, but the way I've always done it in C is:

Code:
int main(int argc,char* argv[])
{
    if(strcmp(argv[1],"<some argument>"))
        do_something(with_parameters);
    else if(strcmp(argv[1],"<some other argument>"))
        do_something_else(with_different_parameters);
    else
        printf("Usage: <bla bla bla>\n");

    return 0;
}
Is that a bad way of going about it? I've always thought of it as being that argc is the argument count, and argv is the argument variable.
Hello MrCode,

Parsing command line arguments is one of the subjects covered in 'how to program 101' so questions relating to 'better parsing of command line arguments' never really come to mind, it's an IF statement right?

For the most part yeah an IF statement will do but once more functionality is added and more and more switches are added the IF statement slowly becomes this huge flying logical spaghetti monster (HFLSM).

That was the main reason I used optparse. I didn't want to code a HFLSM. Here is an example of my code. The application performs black box system tests on a fortran project (SUT), it takes the test files and runs them before checking the result. The command arguments for the testing functionality include the SUT, previous SUTs, and the test files (possiblity a subset based an category and filter)

Parser in the code is the main optparse object (OptionsParser)

Please ignore any grammer errors in the help... work in progress

Code:
	def test_command_options(self):
		"""
		Specifies the parsers command line options when script called with testing command.
		"""	
		self.parser.set_defaults(testing=True)

		self.parser.add_option("-e", "--engine", dest="test_engine", help="Specifies the engine that is being tested.")

		self.parser.add_option("-a", "--against", "--and-against", action="append", dest="target_engine", help="Specifies the previous engines versions that the test engine will be tested against.")

		self.parser.add_option("-c", "--using-category", "--and-category", action="append", dest="target_category", help="Specifies the categories of tests that will be run. All test files contained with in the category will be tested. You can specify more than one category. No errors are raised if any category is not found.")
From the above the script accepts arguments like this...

Code:
app.py test -e 1.23 -a 1.10 -a 1.15 -c "FastTests"
this will test SUT 1.23 against 1.10 and 1.15 using only the test files that are classified as 'FastTests'

or it also supports this

Code:
app.py test --engine 1.23 --against 1.10 --and-against 1.15 --using-category "FastTests"
the command line becomes a mini DSL!! Excellent!

all the above is automatically parsed to data structures so --against and --and-against becomes a list ["1.10", "1.15"]... automagically!! and --help is supported as well.

app.py test --help

will display all the command line arguments and their help; and I haven't written any code to do that. No HFLSMs in sight!!

Last edited by dsmyth; 03-19-2010 at 04:39 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
Command line arguments bioinformatics_guy Linux - Newbie 2 02-12-2009 07:26 AM
need some help regarding command line arguments kristam269 Linux - General 1 01-23-2007 09:40 AM
command line arguments nickraj Programming 6 09-11-2006 01:01 PM
command line arguments containing ( Lotharster Linux - Newbie 3 01-05-2006 08:43 AM
Help with command line arguments ? synapse Linux - Newbie 2 02-23-2004 02:25 AM

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

All times are GMT -5. The time now is 02:19 AM.

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