Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Expose new options to command line interface

  • Loading branch information...
commit 5bf6413a6e51cdaf1ea40b60910d5b4825261831 1 parent 71d5ada
@brandonbloom authored
Showing with 55 additions and 15 deletions.
  1. +55 −15 lib/cli.coffee
View
70 lib/cli.coffee
@@ -8,28 +8,68 @@ fs = require 'fs'
exports.main = ->
[interpreter, script, args...] = process.argv
- prefix = null
-
- if args.length > 0
- match = args[0].match(/^--prefix=(.+)$/)
- if match
- args.shift()
- prefix = match[1]
-
- #TODO: Expose --selectors and --no-selectors flags
- #TODO: Expose --exports option
-
- if args.length != 1
+ usage = ->
console.log """
Usage:
#{path.basename(script)} [options] <html-file>
- --prefix=<string> Prepends a string to each element function call
+ --prefix=<string> Prepends a string to each element function call
+ --no-prefix Disables prefix (default)
+ --selectors Output css-selectors for id and classes (default)
+ --no-selectors Disables output of css-selectors for id and classes
+ --export[=<name>] Wraps the output in a Node.js style export
+ --no-export Disables wrapping the output in an export (default)
"""
process.exit 1
- sourceFile = args.shift()
+ prefix = null
+ export_ = null
+ selectors = null
+ sourceFile = null
+
+ for arg in args
+ match = arg.match(/^--([a-z-]+)(=(.+))?$/i)
+ if match
+ key = match[1]
+ value = match[3]
+ rejectValue = ->
+ if value
+ console.log "Unexpected value for boolean flag #{key}"
+ process.exit 1
+ requireValue = ->
+ unless value
+ console.log "Expected value for switch #{key}"
+ process.exit 1
+ switch key
+ when 'prefix'
+ requireValue()
+ prefix = value
+ when 'no-prefix'
+ rejectValue()
+ prefix = null
+ when 'selectors'
+ rejectValue()
+ selectors = true
+ when 'no-selectors'
+ rejectValue()
+ selectors = false
+ when 'export'
+ export_ = value ? true
+ when 'no-export'
+ rejectValue()
+ export_ = false
+ else
+ console.log "Unknown switch #{key}"
+ process.exit 1
+ else if sourceFile
+ usage()
+ else
+ sourceFile = arg
+
+ unless sourceFile?
+ usage()
html = fs.readFileSync sourceFile, 'utf8'
- convert html, process.stdout, {prefix}, (err) ->
+ options = {prefix, selectors, export: export_}
+ convert html, process.stdout, options, (err) ->
console.error err if err
Please sign in to comment.
Something went wrong with that request. Please try again.