Skip to content

Commit

Permalink
Restructuring and Help generator partially done
Browse files Browse the repository at this point in the history
  • Loading branch information
frodwith committed Apr 25, 2011
1 parent d6d7a3c commit f22aaf2
Show file tree
Hide file tree
Showing 12 changed files with 446 additions and 190 deletions.
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2011 Paul Driver

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
189 changes: 0 additions & 189 deletions getopt.coffee

This file was deleted.

79 changes: 79 additions & 0 deletions lib/Help.coffee
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,79 @@
specification = require './specification'
type = require './types'

leftCol = (spec, alias, long) ->
prefix = if long then '--' else '-'
str = prefix + alias

if spec.type isnt type.flag
if spec.type is type.object
str += ' KEY=VAL'
else if long
str += '=VAL'
else
str += ' VAL'

str

rank = {}
rank[type[t]] = i for t, i in ['flag', 'scalar', 'array', 'object']

exports.Help = class Help
tw: 78

constructor: (@spec) ->
specification.index(@spec)

toString: () ->
lines = []
for own target, sub of @spec
line = { rank: rank[sub.type] }
left = (leftCol(sub, s, false) for s in sub.short.slice(0).sort())
left.push leftCol(sub, s, true) for s in sub.long.slice(0).sort()

line.left = left
line.fl = left[0]
line.ll = left[left.length-1]
line.right = sub.description.trim()
line.right += '.' unless line.right.match(/\.$/)
line.right += ' Required.' if sub.required

if sub.default
line.right += ' Default: ' + JSON.stringify(sub.default)

lines.push line

# Right-justify the left sides
lengths = (l.ll.length for l in lines)
max = Math.max.apply(Math, lengths)
for line in lines
for l, i in line.left
pad = max - l.length
l = ' ' + l for [1..pad] if pad
line.left[i] = l + ' '

# Left-justify the right sides two spaces away

remaining = @tw - max - 2
sep = "\n"
sep += ' ' for [1..max+2]


for l, i in lines
words = (w.trim() for w in l.right.split /\s/)
words = (w for w in words if w.length > 0)
all = ''
cur = words.shift()
while words.length > 0
w = words.shift()
if w.length + cur.length < remaining
cur += ' ' + w
else
all += cur + sep
cur = w
lines[i].right = all + cur

lines.sort (a, b) -> (a.rank - b.rank) or a.fl.localeCompare(b.fl)
out = (l.left.join("\n") + l.right for l in lines).join "\n\n"
console.log out
out
Loading

0 comments on commit f22aaf2

Please sign in to comment.