Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>yup, there is a simpler/better way, especially in Java or other OO languages. </p> <p>The basic insight, first, is that your command parser is a finite state machine: the START state is an empty line (or index at the start of a line).</p> <p>Let's think about <code>echo</code>:</p> <pre><code>$ echo foo bat "bletch quux" </code></pre> <ol> <li><p>tokenize the line into pieces:</p> <p>"echo" "foo" "bar" "bletch quux"</p></li> <li><p>in a shell, the grammar is usually <em>verb noun noun noun...</em> so interpret it that way. You CAN do it with a sequence of if-else if things, but a hash is better. You load the hash with strings as indices, and index something else. It could be just a number, which would go into a switch:</p></li> </ol> <p>(this is pseudocode):</p> <pre><code> Hashtable cmds = new Hashtable(); enum cmdindx { ECHO=1, LS=2, ...} cmds.add("echo", ECHO); // ... // get the token into tok switch (cmds.get(tok)) { case ECHO: // process it // get each successor token and copy it to stdout break; ... default: // didn't recognize the token // process errors } </code></pre> <p>EVEN better, you can apply the Command and Object Factory patterns. Now you have a class Command</p> <pre><code> public interface Command { public void doThis(String[] nouns) ; public Command factory(); } public class Echo implements Command { public void doThis(String[] nouns){ // the code is foreach noun in nouns, echo it } public Command factory(){ // this clones the object and returns it } } </code></pre> <p>Now, your code becomes</p> <pre><code> // Load the hash Hashtable cmds = new Hashtable(); cmds.add("echo", new Echo()); // one for each command // token is in tok // the "nouns" or "arguments are in a String[] nouns ((cmds.get(tok)).factory()).doThis(nouns); </code></pre> <p>See how this works? You look up the object in the hash. You call the <code>factory</code> method to get a new copy. You then invoke the processing for that command using the <code>doThis</code> method.</p> <h3>Update</h3> <p>This may be a bit <em>too</em> general, in that it uses the Factory pattern. Why have a factory method? Mainly, you'd use that so that each time you execute a command, the "verb" object (like the instance of <code>Echo</code>) can have its own internal state. If you don't need state to persist for a long time, you can simplify this to </p> <pre><code> (cmds.get(tok)).doThis(nouns); </code></pre> <p>Now it simply gets and uses the <code>Echo</code> object you created when you instanciated it with <code>cmds.add("echo", new Echo());</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload