werkzeug
Version:
compiles ts, coffee, sass/scss, less/ stylus and packs browser ready bundles
127 lines (96 loc) • 3.24 kB
text/coffeescript
require 'colors'
class ArgsParser
constructor: () ->
null
parse: (, ) ->
.splice(0, 2) if
= or []
= {}
= {}
= []
= null
index = 0
cmd for cmd in .commands
if not ['--help']
name: 'help'
help: 'shows the help'
if .length == 0 and .defaultCmd
.push '--' + .defaultCmd
while index < .length
arg = [index]
cmd = [arg]
if cmd
arg = [index + 1]
nextCmd = [arg]
if not arg or nextCmd
result =
name: cmd.name
arg: cmd.arg?.default
else
++index
result =
name: cmd.name
arg: arg
else if arg
if /^(--|-)/.test arg
= "unknown command \"#{arg}\"."
return @
else if .length == 1
result =
else
= "unexpected argument \"#{arg}\"."
return @
if result
.push result
[result.name] = result.arg
++index
@
parseCmd: (cmd) ->
name = cmd.name
long = '--' + name
short = '-' + (cmd.short or name[0])
cmd.long = long
cmd.short = short
[long] = [short] = cmd
null
getDefaultCmd: (arg) ->
cmd = ['--' + .defaultCmd]
if cmd
result =
name: cmd.name
arg: arg or cmd.arg?.default
result
chars: (char, count) ->
new Array(count + 1).join(char)
printHelp: () ->
help = .help
console.log '\n' + help + '\n' if help
null
printCommands: () ->
sm = 0
lg = 0
for cmd in .commands
sm = Math.max sm, cmd.short.length
lg = Math.max lg, cmd.long.length
console.log 'commands:\n'
for cmd in .commands
arg = cmd.arg
short = cmd.short
long = cmd.long
short = short + ', ' + ' ', sm - short.length
long = long + ' ', lg - long.length
prefix = ' ' + short + long + ' '
align = prefix.length
console.log prefix + cmd.help
if arg
help = arg.help or 'one argument'
value = arg.default
if not value
console.log + 'requires ' + help + '\n'
else
console.log + 'expects ' + help + ' as argument with "' + value + '" as default\n'
console.log()
null
module.exports = ArgsParser