• commandLine.doc.coffee

  • ¶

    This is example of command line client usage. Try to run it!

    transit = require('../../transit')
    app = transit()
  • ¶

    Use command line client.

    app.use transit.commandLine()
  • ¶

    Allows to specify aliases with 'alias' command.

    Try to type alias aloha hi "one more alias for hello" and alias "swap {1} {2}" "echo {2} {1}".

    To delete alias type same command but without target command, this way: alias aloha.

    app.use transit.alias (
  • ¶

    First optional function shall load aliases from some storage and return them to callback.

      (name, cb) ->
        cb [
          {pattern: "hi", replacement: "hello", autohelp: "same as 'hello'"},
          {pattern: "jecho {param}", replacement: "echo {param} desu", autohelp: "japaneese version of echo"}
        ]
    ), (
  • ¶

    Second optional function will be called when aliases are changed, so you may store them.

      (name, data) ->
        console.log "Aliases saved, now there is #{data.length} of them"
    )
  • ¶

    Note that this middleware shall be inserted before commandParser().

  • ¶

    Use simple commands parser.

    app.use transit.commandParser()
  • ¶

    Make client does not wait for response from user hander.

    app.use transit.doNotWaitForResponse()
  • ¶

    Define named custom formatter. Formatter is called before sending data back to client. In most cases you would like to set up a chain of formatting functions.

    app.formatOutput "braces", (data, options, cb) -> cb(null, "(#{data})")
  • ¶

    Define user handler for 'hello' command. Use sendBack to send data to client. It could be called any amount of times.

    app.receive 'hello', autohelp: "just greeting", (req, res) ->
      res.sendBack "Hello #{req.user}"
    
    
    echo = (params, cb) ->
      if params.indexOf("error") > -1
        return cb("Error example")
      else
        cb null, params.join(" ")
  • ¶

    Define user handler for 'echo' command. All command arguments (space-separated) will be available in 'params' field. Here you can see usage of custom formatter braces defined above.

    app.receive 'echo {{params}}', autohelp: "echoes provided params back", (req, res) ->
      echo req.attrs.params, res.braces.asCallback
  • ¶

    Define default user handler. It is called in case command is not matched.

    app.receive (req, res) ->
      res.sendBack "I do not know what is <#{req.data}> :("
  • ¶

    Defines the "help" command which lists all existent commands. Note that it shows aliases as well.

    app.use transit.autohelp {showOnUnknown: false}
  • ¶

    Defines event handler. Events are emitted in special cases like user goes offline (exit event). To emulate that type ':exit' in command line.

    app.on 'exit', (req, res) ->
      console.log "User #{req.user} left"
  • ¶

    Starts listening to the client.

    app.start()