 parseOpts = (args) ->
   console.log "Using args #{args.join ' '}"
   i = 0
+  opts = {}
   while i < args.length
     if args[i] == "--host"
       i = i + 1
       if i > args.length then printUsageAndExit "Unknown argument: #{args[i]}"
-      hostname = args[i]
+      opts['hostname'] = args[i]
     else if args[i] == "--timeout"
       i = i + 1
       if i > args.length then printUsageAndExit "Unknown argument: #{args[i]}"
-      timeout = args[i]
+      opts['timeout'] = args[i]
     else if args[i] == "--packs"
       i = i + 1
       if i > args.length then printUsageAndExit "Unknown argument: #{args[i]}"
-      packs = args[i]
+      opts['packs'] = args[i]
     else if args[i] == "--perf"
-      perf = true
-      i = i + 1
-      if i > args.length then printUsageAndExit "Unknown argument: #{args[i]}"
-      date = args[i]
-      i = i + 1
-      if i > args.length then printUsageAndExit "Unknown argument: #{args[i]}"
-      buildId = args[i]
-      i = i + 1
-      if i > args.length then printUsageAndExit "Unknown argument: #{args[i]}"
-      gitHash = args[i]
-      i = i + 1
-      if i > args.length then printUsageAndExit "Unknown argument: #{args[i]}"
-      gitBranch = args[i]
-      i = i + 1
-      if i > args.length then printUsageAndExit "Unknown argument: #{args[i]}"
-      ncpu = args[i]
-      i = i + 1
-      if i > args.length then printUsageAndExit "Unknown argument: #{args[i]}"
-      os = args[i]
-      i = i + 1
-      if i > args.length then printUsageAndExit "Unknown argument: #{args[i]}"
-      jobName = args[i]
-      i = i + 1
-      if i > args.length then printUsageAndExit "Unknown argument: #{args[i]}"
-      outputDir = args[i]
+      opts['perf'] = true
+      i = i + 1
+      if i > args.length then printUsageAndExit "Unknown argument: #{args[i]}"
+      opts['date'] = args[i]
+      i = i + 1
+      if i > args.length then printUsageAndExit "Unknown argument: #{args[i]}"
+      opts['buildId'] = args[i]
+      i = i + 1
+      if i > args.length then printUsageAndExit "Unknown argument: #{args[i]}"
+      opts['gitHash'] = args[i]
+      i = i + 1
+      if i > args.length then printUsageAndExit "Unknown argument: #{args[i]}"
+      opts['gitBranch'] = args[i]
+      i = i + 1
+      if i > args.length then printUsageAndExit "Unknown argument: #{args[i]}"
+      opts['ncpu'] = args[i]
+      i = i + 1
+      if i > args.length then printUsageAndExit "Unknown argument: #{args[i]}"
+      opts['os'] = args[i]
+      i = i + 1
+      if i > args.length then printUsageAndExit "Unknown argument: #{args[i]}"
+      opts['jobName'] = args[i]
+      i = i + 1
+      if i > args.length then printUsageAndExit "Unknown argument: #{args[i]}"
+      opts['outputDir'] = args[i]
     else if args[i] == "--excludeFlows"
       i = i + 1
       if i > args.length then printUsageAndExit "Unknown argument: #{args[i]}"
-      excludeFlows = args[i]
+      opts['excludeFlows'] = args[i]
     else
       printUsageAndExit "Unknown argument: #{args[i]}"
     i = i + 1
-
-parseOpts system.args[1..]
-
-if hostname == null then hostname = 'localhost:54321'
+  opts
+
+opts = parseOpts system.args[1..]
+
+hostname = opts['hostname'] ? 'localhost:54321'
 console.log "PHANTOM: Using #{hostname}"
 
-timeout = if timeout == null then 3600000 else 1000 * parseInt timeout, 10
+timeout = if timeoutArg = opts['timeout']
+  1000 * parseInt timeoutArg, 10
+else
+  3600000
 console.log "PHANTOM: Using timeout #{timeout}ms"
 
-packNames = if packs == null then ['examples'] else packs.split ':'
-console.log "PHANTOM: Using packs:"
-for packName in packNames
-  console.log "PHANTOM: #{packName}"
-
-excludeFlowNames = if excludeFlows == null then [] else excludeFlows.split ';'
-console.log "PHANTOM: Excluding flows:"
-for excludeFlowName in excludeFlowNames
-  console.log "PHANTOM: #{excludeFlowName}"
+packsArg = opts['packs']
+packNames = if packsArg
+  packsArg.split ':'
+else
+  ['examples']
+
+excludeFlowsArg = opts['excludeFlows']
+excludeFlowsNames = if excludeFlowsArg
+  excludeFlowsArg.split ';'
+else
+  []
+for excludeFlowName in excludeFlowsNames
+  console.log "PHANTOM: Excluding flow: #{excludeFlowName}"
 
 page = webpage.create()
 
-if perf
-  console.log "PHANTOM: Performance of individual tests will be recorded in perf.csv in output directory: #{outputDir}."
-  page._outputDir = outputDir
+if opts['perf']
+  console.log "PHANTOM: Performance of individual tests will be recorded in perf.csv in output directory: \
+               #{opts['outputDir']}."
+  page._outputDir = opts['outputDir']
 
 page.onResourceError = ({ url, errorString }) ->
   console.log "BROWSER: *** RESOURCE ERROR *** #{url}: #{errorString}"

system = require 'system'
webpage =  require 'webpage'
timeout = null
packs = null
hostname = null
excludeFlows = null
perf = false
date = null
buildId = null
gitHash = null
gitBranch = null
ncpu = null
os = null
jobName = null
outputDir = null

phantom.onError = (message, stacktrace) ->
  if stacktrace?.length

          window._os = os
          window._jobName = jobName
          window._perf = perf
          window._excludeFlowsNames = excludeFlowsNames
          context = window.flow.context
          if window._phantom_started_
            if window._phantom_exit_ then yes else no
