Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The limitations of making a "mini language" have become apparent.</p> <p>Proper languages have a tree-like structure and more complex syntax than <code>cmd</code> can handle easily.</p> <p>Sometimes it's actually easier to use Python directly than it is to invent your own DSL.</p> <p>Currently, your DSL probably reads a script-like file of commands.</p> <p>Because of the way <code>cmd</code> works, your little comments get a string argument, which must be parsed. Then the command gets executed. And, further, each command is a method of the <code>cmd.Cmd</code> subclass.</p> <p>Here's what you can do.</p> <p>Each <code>do_foo( self, args )</code> method becomes a stand-alone callable object. It will follow the <strong>Command</strong> design pattern. It will do <strong>exactly</strong> what the method function does now. No less. <strong>Exactly</strong> the same.</p> <pre><code>class Foo( object ): def __init__( self, global_context ): self.context= global_context def __call__( self, args ): ... The rest of do_foo ... </code></pre> <p>Additionally, your existing <code>cmd.Cmd</code> subclass probably maintains some internal state. All of the <code>self.this</code> and <code>self.that</code> instance variables must be changed to reference and explicit context object.</p> <pre><code>class Context( object ): pass </code></pre> <p>Change <code>self.this</code> or <code>self.that</code> to <code>self.context.this</code> or <code>self.context.that</code></p> <p>Now, you can create your context and your various commands.</p> <pre><code>ctx = Context() foo= Foo(ctx) </code></pre> <p>Your script changes syntax slightly. From:</p> <pre><code>foo argstring bar argstring </code></pre> <p>to:</p> <pre><code>from mylanguage import foo, bar foo( "argstring" ) bar( "argstring" ) </code></pre> <p>This does <strong>Exactly</strong> what the CLI does now. No more. No less. <strong>Exactly</strong> the same. Slightly different syntax. </p> <p>Now your script is no longer in a DSL that's hard to expand. It's in Python.</p> <p>Having done that, you can now use Python syntax <code>if</code> statements.</p> <p>You have the <strong>Exact</strong> functionality currently implemented in <code>cmd</code> with better syntax.</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.
    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