Skip to content

Commit

Permalink
replace nopt with optparse, add indent option
Browse files Browse the repository at this point in the history
  • Loading branch information
timaschew committed Oct 12, 2013
1 parent fdb050c commit 7bcdb0e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 126 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"docpad-plugin-coffeescript": "2.x",
"docpad-plugin-uglify": "2.x",
"joe": "~1.3.0",
"joe-reporter-console": "~1.2.1"
"joe-reporter-console": "~1.2.1",
"nopt": "~2.1.1"
},
"directories": {
"lib": "./out/lib"
Expand Down
55 changes: 40 additions & 15 deletions src/documents/lib/command.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ fsUtil = require 'fs'
pathUtil = require 'path'
tty = require 'tty'
fileUtil = require 'file'
optparse = require './optparse'
{inspect} = require 'util'
nopt = require 'nopt'

# The help banner that is printed when `js2coffee` is called without arguments.
BANNER = """
Expand All @@ -19,17 +20,31 @@ BANNER = """
cat file.js | js2coffee
"""

knownOpts =
version: Boolean # show js2coffee version
verbose: Boolean # be verbose
no_comments: Boolean # do not translate comments
show_src_lineno: Boolean # show src lineno's as comments
help: Boolean # if you need help
indent: String # set indent character(s), default two spaces

shortHands =
v: ["--version"]
V: ["--verbose"]
X: ["--no-comments"]
l: ["--show-src_lineno"]
h: ["--help"]
i4: ["--indent", " "]
it: ["--indent", "\t"]

# The list of all the valid option flags that `js2coffee` knows how to handle.
SWITCHES = [
# ['-b', '--batch', 'batch mode to convert all .js files in directory']
# ['-o', '--output [OUTDIR]', 'set the output directory']
# ['-r', '--recursive', 'recurse on all subdirectories']
['-v', '--version', 'Show js2coffee version']
['-V', '--verbose', 'Be verbose']
['-X', '--no_comments', 'Do not translate comments']
['-l', '--show_src_lineno', 'Show src lineno\'s as comments']
['-h', '--help', 'If you need help']
]
description =
'version': 'Show js2coffee version'
'verbose': 'Be verbose'
'no_comments': 'Do not translate comments'
'show_src_lineno': 'Show src lineno\'s as comments'
'help': 'If you need help'
'indent': 'Specify the indent character(s) - default 2 spaces'

# Top-level objects shared by all the functions.
options = {}
Expand All @@ -40,9 +55,13 @@ UnsupportedError = js2coffee.UnsupportedError
cmd = pathUtil.basename(process.argv[1])

parseOptions = ->
optionParser = new optparse.OptionParser SWITCHES, BANNER
options = optionParser.parse process.argv.slice 2
sources = options.arguments
options = nopt knownOpts, shortHands, process.argv, 2
sources = options.argv.remain or= []

# nopt trim string values, copy it manually from argv.cooked
index = options.argv.cooked.indexOf "--indent"
if index isnt -1 and options.argv.cooked.length >= index
options.indent = options.argv.cooked[index+1]

writeFile = (dir, currfile, coffee) ->
outputdir = options.output || '.'
Expand Down Expand Up @@ -138,7 +157,13 @@ compileFromStdin = ->
console.log output

usage = ->
console.warn (new optparse.OptionParser SWITCHES, BANNER).help()
console.warn BANNER + "\n"
console.warn "options:"
for arg, type of knownOpts
console.warn "--#{arg} # #{description[arg]}"
console.warn "\nshorcuts:"
for short, long of shortHands
console.warn "-#{short} = #{inspect long}"
process.exit 0

version = ->
Expand Down
3 changes: 3 additions & 0 deletions src/documents/lib/js2coffee.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ buildCoffee = (str, opts = {}) ->
str = str.replace /\r/g, ''
str += "\n"

if opts.indent?
Code.INDENT = opts.indent

builder = new Builder opts
scriptNode = parser.parse str

Expand Down
110 changes: 0 additions & 110 deletions src/documents/lib/optparse.js.coffee

This file was deleted.

0 comments on commit 7bcdb0e

Please sign in to comment.