<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Recent changes to Registering your Commands in Java</title><link>https://sourceforge.net/p/excon/wiki/Registering%2520your%2520Commands%2520in%2520Java/</link><description>Recent changes to Registering your Commands in Java</description><atom:link href="https://sourceforge.net/p/excon/wiki/Registering%20your%20Commands%20in%20Java/feed" rel="self"/><language>en</language><lastBuildDate>Thu, 05 Dec 2013 16:56:33 -0000</lastBuildDate><atom:link href="https://sourceforge.net/p/excon/wiki/Registering%20your%20Commands%20in%20Java/feed" rel="self" type="application/rss+xml"/><item><title>Registering your Commands in Java modified by Chris DeGreef</title><link>https://sourceforge.net/p/excon/wiki/Registering%2520your%2520Commands%2520in%2520Java/</link><description>&lt;div class="markdown_content"&gt;&lt;p&gt;This is an example of the registerStandard() method to use as a discussion vehicle.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;    &lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Receiver&lt;/span&gt; &lt;span class="n"&gt;registerStandard&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="n"&gt;throws&lt;/span&gt; &lt;span class="n"&gt;ParseException&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;IOException&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;ClientCommand&lt;/span&gt; &lt;span class="n"&gt;cc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;createCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Garbage Collection&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
            &lt;span class="s"&gt;&amp;quot;gc&amp;quot;&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;GC&lt;/span&gt;&lt;span class="p"&gt;()));&lt;/span&gt;
        &lt;span class="n"&gt;cc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;setTimeoutMS&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;60000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;createCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Echo to log&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
            &lt;span class="s"&gt;&amp;quot;echo&amp;quot;&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;Echo&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;  
            &lt;span class="s"&gt;&amp;quot;-tStr -p -km --var message --req&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="n"&gt;cc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;setTimeoutMS&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;createCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Application Shutdown&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
            &lt;span class="s"&gt;&amp;quot;kill&amp;quot;&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;Kill&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
            &lt;span class="s"&gt;&amp;quot;-tint -p -kexitCode --var exitCode --list 0 9 --def 0&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
            &lt;span class="s"&gt;&amp;quot;-tbool -kc confirm --req&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="n"&gt;cc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;setTimeoutMS&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;60000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The essential difference in the registration of these three commands is the complexity of the parse.  It goes from no parse, to a one argument parse, and finally a parser with two arguments.&lt;/p&gt;
&lt;p&gt;When a command is registered with the ExCon Receiver it is wrapped in an instance of a ClientCommand.  You can interact a bit with the wrapper before you register it.  But to do that you must create it separately from the registration.  The registration method allows you to provide all of the details for registration.  But the preferred means of registering is to first create the wrapper (ClientCommand) and then register the wrapper.&lt;/p&gt;
&lt;h2&gt;Creating the ClientCommand&lt;/h2&gt;
&lt;p&gt;There are 4 parameters that you can provide when creating the command wrapper (ClientCommand).&lt;/p&gt;
&lt;h3&gt;Title&lt;/h3&gt;
&lt;pre class="java:html:nocontrols" name="code"&gt;final String title,&lt;/pre&gt;
&lt;p&gt;The title is displayed when the console-user uses the "help" command.  A list of all commands is presented by cmdName and title.  The title is optional but very helpful to the user.&lt;/p&gt;
&lt;p&gt;e.g.: "Stack trace" is a title for the "Dump" command.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="cp"&gt;# xc help&lt;/span&gt;
&lt;span class="o"&gt;=========================================================&lt;/span&gt;
&lt;span class="n"&gt;Help&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;Contents&lt;/span&gt;  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;does&lt;/span&gt; &lt;span class="n"&gt;not&lt;/span&gt; &lt;span class="n"&gt;matter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;=========================================================&lt;/span&gt;
&lt;span class="n"&gt;Dump&lt;/span&gt;                &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;Stack&lt;/span&gt; &lt;span class="n"&gt;trace&lt;/span&gt;
&lt;span class="n"&gt;Echo&lt;/span&gt;                &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;Echo&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt;
&lt;span class="n"&gt;GC&lt;/span&gt;                  &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;Garbage&lt;/span&gt; &lt;span class="n"&gt;Collection&lt;/span&gt;
&lt;span class="n"&gt;Kill&lt;/span&gt;                &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;Application&lt;/span&gt; &lt;span class="n"&gt;Shutdown&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Command Name&lt;/h3&gt;
&lt;pre class="java:html:nocontrols" name="code"&gt;final String cmdName,&lt;/pre&gt;
&lt;p&gt;The cmdName is how the command is requested.  When it is requested the case does not matter so you can specify the command's name with upper and lower case letters for readability.  This is required.&lt;/p&gt;
&lt;p&gt;The command entitled "Garbage Collection" can be executed with the lowercase "gc".&lt;br /&gt;
&lt;pre class="html:nocontrols:nogutter" name="code"&gt;# xc gc&lt;br /&gt;
free memory before(842,515,184) after(879,780,512) reclaimed(37,265,328)&lt;br /&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;h3&gt;Instance of the Command Class&lt;/h3&gt;
&lt;pre class="java:html:nocontrols" name="code"&gt;final IExternalRequest cmd,&lt;/pre&gt;
&lt;p&gt;Create a new instance of your command class for this parameter.  This is required.  This specific instance of the command will be used each time the command is requested.  So it must be re-entrant; reset all of the variables when exiting the command so that the next command can have a clean slate to work with.&lt;/p&gt;
&lt;h3&gt;Arguments to the Command&lt;/h3&gt;
&lt;pre class="java:html:nocontrols" name="code"&gt;final String... parserDef&lt;/pre&gt;
&lt;p&gt;An Argument parser to move values from the command-line into the variables in your command.  This is optional.  But it is necessary if your command has parameters that must be resolved.&lt;/p&gt;
&lt;h2&gt;Parameters to The Registration Method&lt;/h2&gt;
&lt;p&gt;The same method parameters are available for the registration method as those that are available for the createCommand method.  This is a convenience when you don't need direct access to the wrapped command (ClientCommand).&lt;/p&gt;
&lt;p&gt;Although the code, similar to the example at the top of this page, is simpler, it does not allow access to the command wrapper.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Receiver&lt;/span&gt; &lt;span class="nf"&gt;registerStandard&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="n"&gt;throws&lt;/span&gt; &lt;span class="n"&gt;ParseException&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;IOException&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  

    &lt;span class="k"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Garbage Collection&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
        &lt;span class="s"&gt;&amp;quot;gc&amp;quot;&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;GC&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;  

    &lt;span class="k"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Echo to log&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
        &lt;span class="s"&gt;&amp;quot;echo&amp;quot;&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;Echo&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;  
        &lt;span class="s"&gt;&amp;quot;-tStr -p -km --var message --req&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  

    &lt;span class="k"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Application Shutdown&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
        &lt;span class="s"&gt;&amp;quot;kill&amp;quot;&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;Kill&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;  
        &lt;span class="s"&gt;&amp;quot;-tint -p -kexitCode --var exitCode --list 0 9 --def 0&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
        &lt;span class="s"&gt;&amp;quot;-tbool -kc confirm --req&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Interacting with the ClientCommand&lt;/h2&gt;
&lt;h3&gt;Limiting the milliseconds in which the command can run.&lt;/h3&gt;
&lt;pre class="java:html:nocontrols" name="code"&gt;cc.setTimeoutMS(long timeoutMS);&lt;/pre&gt;
&lt;p&gt;This limits the wall-clock time that a command can run before it is interrupted.&lt;/p&gt;&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Chris DeGreef</dc:creator><pubDate>Thu, 05 Dec 2013 16:56:33 -0000</pubDate><guid>https://sourceforge.net71f0726dc94026384d1d37ba4e2819b43fe016a9</guid></item></channel></rss>