<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Recent changes to Quick_Guide</title><link>https://sourceforge.net/p/cmdconfig/wiki/Quick_Guide/</link><description>Recent changes to Quick_Guide</description><atom:link href="https://sourceforge.net/p/cmdconfig/wiki/Quick_Guide/feed" rel="self"/><language>en</language><lastBuildDate>Tue, 23 Sep 2014 08:27:32 -0000</lastBuildDate><atom:link href="https://sourceforge.net/p/cmdconfig/wiki/Quick_Guide/feed" rel="self" type="application/rss+xml"/><item><title>Quick_Guide modified by Ray</title><link>https://sourceforge.net/p/cmdconfig/wiki/Quick_Guide/</link><description>&lt;div class="markdown_content"&gt;&lt;h2 id="quick-guide"&gt;Quick Guide&lt;/h2&gt;
&lt;p&gt;Well, you know you should read the whole documentation, but you need just a quick fix of your application to recognize command line options? Let's say your application processes a list of strings, but you need to add a &lt;em&gt;verbose&lt;/em&gt; option and also allow your application to read the strings &lt;em&gt;from a file&lt;/em&gt;. The option that switches to &lt;em&gt;verbose&lt;/em&gt; could be &lt;strong&gt;-v&lt;/strong&gt; and the &lt;em&gt;file option&lt;/em&gt; could be &lt;strong&gt;-f &lt;/strong&gt;&lt;em&gt;filename&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;First, download the &lt;a class="" href="https://sourceforge.net/projects/cmdconfig/files/latest/download"&gt;CmdConfig.jar&lt;/a&gt; and add it to your CLASSPATH. This way you get the access to the &lt;em&gt;cz.phalanx.config.Config&lt;/em&gt; class.&lt;/p&gt;
&lt;p&gt;Second, import the class, the best placement is in the class file of your main class:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;import&lt;/span&gt; &lt;span class="n"&gt;cz&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;phalanx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Config&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Third, instantiate the &lt;em&gt;Config&lt;/em&gt; class, specifying the options you want to be recognized. The best placement is at the start of your &lt;em&gt;main()&lt;/em&gt; method:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Config&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"vf:"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="cm"&gt;/* your code continues here */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The "vf:" is an &lt;em&gt;option string&lt;/em&gt;, which tells the parser to expect a &lt;strong&gt;-v&lt;/strong&gt; option with no argument (it's a &lt;em&gt;boolean&lt;/em&gt; option) and a &lt;strong&gt;-f&lt;/strong&gt; option that &lt;em&gt;must&lt;/em&gt; have an argument.&lt;/p&gt;
&lt;p&gt;Fourth, let the parser process the arguments your &lt;em&gt;main()&lt;/em&gt; method got:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;    &lt;span class="n"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;correct&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parseArguments&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Check the &lt;em&gt;correct&lt;/em&gt; value - if it is &lt;em&gt;false&lt;/em&gt;, then your &lt;strong&gt;-f&lt;/strong&gt; option was passed &lt;em&gt;without&lt;/em&gt; an argument. In this case you can decide to abort the program, or just to issue a warning and process only the strings your application was given at the command line.&lt;/p&gt;
&lt;p&gt;Fifth, check the presence of your options and their arguments:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'v'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Program is running in verbose mode."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'f'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="n"&gt;processInputFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="n"&gt;FileInputStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getOptionArgs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'f'&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])));&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The getOptionArgs() method returns an array for defined options (passed to the constructor), &lt;em&gt;null&lt;/em&gt; for undefined options. But you can be sure that when the parseArguments() returned &lt;em&gt;true&lt;/em&gt;, your &lt;strong&gt;-f&lt;/strong&gt; option had at least one argument (it could have more of them).&lt;/p&gt;
&lt;p&gt;Sixth, process the strings given at the command line just as you did it before, but instead of processing the &lt;em&gt;args&lt;/em&gt; array argument of &lt;em&gt;main()&lt;/em&gt;, you pick up the remaining (non-option) arguments from config:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;    &lt;span class="n"&gt;processStrings&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getArguments&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;That's it. :-)&lt;/p&gt;&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Ray</dc:creator><pubDate>Tue, 23 Sep 2014 08:27:32 -0000</pubDate><guid>https://sourceforge.net8cf2ea729ee585837b4c69c08965886d7ec04ef8</guid></item></channel></rss>