<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>yken.org &#187; Objective-C</title>
	<atom:link href="http://yken.org/tag/objective-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://yken.org</link>
	<description>...it depends</description>
	<lastBuildDate>Sun, 07 Mar 2010 17:47:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Parsing the command line in Objective-C</title>
		<link>http://yken.org/2009/01/14/parsing-the-command-line-in-objective-c/</link>
		<comments>http://yken.org/2009/01/14/parsing-the-command-line-in-objective-c/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 00:40:42 +0000</pubDate>
		<dc:creator>ikendra</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://yken.org/?p=77</guid>
		<description><![CDATA[Decision into which platform to rewrite the shell script to retrieve the stock quotes using Google API was a simple one &#8211; I would like to use Objective-C to be able to run the program on a Mac (and later hopefully on my iPod touch too). I began exploring the Objective-C only recently so I [...]]]></description>
			<content:encoded><![CDATA[<p>Decision into which platform to rewrite the <a href="http://yken.org/2009/01/05/how-to-get-a-real-time-stock-quote-using-google-api/">shell script to retrieve the stock quotes using Google API</a> was a simple one &#8211; I would like to use Objective-C to be able to run the program on a Mac (and later hopefully on my iPod touch too). I began exploring the Objective-C only recently so I considered to be a good exercise to learn about basic Foundation classes by writing a class to parse command line arguments (instead of using the obvious <a href="http://www.gnu.org/software/libc/manual/html_node/Getopt.html">getopt</a> choice).</p>
<p><span id="more-77"></span><br />
&nbsp;<br />
<!--more--><a href="http://yken.org/hub/wp-content/CommandLineSample.tar.gz">Download the XCode project (CommandLineSample.tar.gz, 48kB) &#8211; including a sample</a><br />
<!--more--><br />
&nbsp;<br />
<!--more--></p>
<p>The CommandLine class is very simple and offers the following functionality:</p>
<ul>
<li>Validates and parses the command line arguments according to user defined struture of options (NSDictionary)</li>
<li>Options can be declared required or optional, followed by option value or not</li>
<li>Minimum number of expected parameters (beside options) can be set</li>
<li>When command line parsing fails, a message describing the error can be retrieved</li>
</ul>
<p><!--more--><br />
A sample use of the CommandLine class:</p>
<pre>
#import &lt;Foundation/Foundation.h&gt;
#import "CommandLine.h"

int main (int argc, char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    BOOL parseSuccess;

    CommandLine *commandLine =
        [[CommandLine alloc] initWithArgc: argc andArgv: argv];

    // This sample expects at least one parameter (other that options)
    int sampleMinNumberOfParams = 1;

    // Define the matrix describing expected options
    NSDictionary *sampleOptionsMatrix =
        [NSDictionary dictionaryWithObjectsAndKeys:
            CL_OPTION_REQUIRED, @"a",
            CL_OPTION_OPTIONAL, @"b",
            CL_OPTION_REQUIRED_WITH_VALUE, @"f",
            CL_OPTION_OPTIONAL_WITH_VALUE, @"x",
            nil];

    // Pass on required parameters to the parser and execute the parser
    [commandLine cL_setOptionsMatrix: sampleOptionsMatrix
    andMinNumberOfParams: sampleMinNumberOfParams];

    parseSuccess = [commandLine cL_parse];

    if (parseSuccess) {
        // Display the parsed options.
        for (NSString *key in commandLine.parsedCommandLine) {
        NSLog(
           [NSString stringWithFormat: @"Option: %@ Value: %@", key,
           [commandLine.parsedCommandLine valueForKey:key]]);
        }
        //And some more processing of a successfully parsed command line:
        if ([commandLine cL_optionIsSet: @"b"]) {
            NSLog(@"Option b was set");
        }
        NSLog(
            [NSString stringWithFormat: @"Value of the 1st parameter is %@",
            [commandLine cL_parameterGetValue: 1]]);
    } else {
        //Display the error message
        NSLog(commandLine.resultText);
    }

    [pool drain];
    [commandLine release];

    return 0;
}
</pre>
<p>A few notes:</p>
<ul>
<li>Alternative long option names are not supported</li>
<li>The options matrix appears to be somewhat verbose, hope it is not too much overhead</li>
<li>The CommandLine class source code enables some documentation to be generated using <a href="http://www.informatik.uos.de/elmar/projects/objcdoc/">objcdoc</a>
</ul>
<p><!--more--><br />
Feel free to use and modify the class in any way. Now off to the next step: learning the <a href="http://code.google.com/p/gdata-objectivec-client/">Google Data APIs Objective-C Client Library</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://yken.org/2009/01/14/parsing-the-command-line-in-objective-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
