 mp = require 'message-ports'
 
 # entry point
-@run = (args) ->
-  type = args.shift()
-  type = typeAliases[type] if typeAliases[type]?
-  printUsageAndExitWithError() unless type?
-  validateType type
-  port = args.shift()
-  validatePort port
-  port = parseInt port
-  if args.length is 0
-    interactiveMode type, port
-  else
-    printUsageAndExitWithError()
+@run = (rawArgs) ->
+  printUsageAndExit(1) if rawArgs.length is 0
+  args = parseArgs rawArgs
+
+  printUsageAndExit(0) if args.help is yes
+
+  type = parseType args.type
+  unless validateType type
+    console.log "#{type} isn't a valid port type"
+    printUsageAndExit(1)
+
+  port = parsePort args.port
+  unless validatePort port
+    console.log "#{port} isn't a valid port id"
+    printUsageAndExit(1)
+
+  interactiveMode type, port
+
+# ---
 
 # ## Interactive Modes
 

Examples:
  mp req 2000
  mp rep 2000
"""


# pull/subscribe and push/publish have the same interfaces at this level

['pull', 'sub'].forEach (name) ->

  im[name] = (portNumber, messagePort) ->
    im.info "started #{im.longName name} socket on port #{portNumber}"
    messagePort (msg) ->
      im.info "message received:"
      im.received msg

['push', 'pub'].forEach (name) ->

  im[name] = (portNumber, messagePort, getLine) ->
    im.info "started #{im.longName name} socket on port #{portNumber}"
    getAndSendMsg = ->
      getLine (line) ->
        messagePort line
