Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
clean up tilt command usage output and handle missing args
  • Loading branch information
rtomayko committed Mar 6, 2010
1 parent 84e0095 commit 0b668e2
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions bin/tilt
Expand Up @@ -3,21 +3,21 @@ require 'ostruct'
require 'optparse' require 'optparse'
require 'tilt' require 'tilt'


usage = <<EOF usage = <<USAGE
Usage: tilt [OPTIONS] [FILE] Usage: tilt <options> <file>
Process template FILE and generate output. With no FILE or when FILE Process template <file> and write output to stdout. With no <file> or
is '-', read template from stdin and use the --type option to determine when <file> is '-', read template from stdin and use the --type option
the template's type. to determine the template's type.
Options Options
-l, --list List supported template engines + file patterns and exit. -l, --list List template engines + file patterns and exit
-t, --type=PATTERN Use this template engine; required when FILE is stdin. -t, --type=<pattern> Use this template engine; required if no <file>
-y, --layout=FILE Use FILE as a layout template. -y, --layout=<file> Use <file> as a layout template
-D NAME=VALUE Define variable NAME as VALUE. -D<name>=<value> Define variable <name> as <value>
-o, --vars=RUBY Evaluate RUBY to Hash and use for variables. -o, --vars=<ruby> Evaluate <ruby> to Hash and use for variables
-h, --help Show this help message. -h, --help Show this help message
Convert markdown to HTML: Convert markdown to HTML:
$ tilt foo.markdown > foo.html $ tilt foo.markdown > foo.html
Expand All @@ -26,12 +26,12 @@ Process ERB template:
$ echo "Answer: <%= 2 + 2 %>" | tilt -t erb $ echo "Answer: <%= 2 + 2 %>" | tilt -t erb
Answer: 4 Answer: 4
Defining variables: Define variables:
$ echo "Answer: <%= 2 + n %>" | tilt --locals="{:n=>40, :x=>0}" $ echo "Answer: <%= 2 + n %>" | tilt --locals="{:n=>40, :x=>0}"
Answer: 42 Answer: 42
$ echo "Answer: <%= 2 + n %>" | tilt -Dn=40 -Dx=0 $ echo "Answer: <%= 2 + n %>" | tilt -Dn=40 -Dx=0
Answer: 42 Answer: 42
EOF USAGE


script_name = File.basename($0) script_name = File.basename($0)
pattern = nil pattern = nil
Expand All @@ -55,13 +55,13 @@ ARGV.options do |o|
end end


# the template type / pattern # the template type / pattern
o.on("-t", "--type=PATTERN") do |val| o.on("-t", "--type=PATTERN", String) do |val|
abort "unknown template type: #{val}" if Tilt[val].nil? abort "unknown template type: #{val}" if Tilt[val].nil?
pattern = val pattern = val
end end


# pass template output into the specified layout template # pass template output into the specified layout template
o.on("-y", "--layout=FILE") do |file| o.on("-y", "--layout=FILE", String) do |file|
paths = [file, "~/.tilt/#{file}", "/etc/tilt/#{file}"] paths = [file, "~/.tilt/#{file}", "/etc/tilt/#{file}"]
layout = paths. layout = paths.
map { |p| File.expand_path(p) }. map { |p| File.expand_path(p) }.
Expand Down Expand Up @@ -89,8 +89,13 @@ end


file = ARGV.first || '-' file = ARGV.first || '-'
pattern = file if pattern.nil? pattern = file if pattern.nil?
abort "template type not given. see: #{$0} --help" if ['-', ''].include?(pattern)

engine = Tilt[pattern]
abort "template engine not found for: #{pattern}" if engine.nil?

template = template =
Tilt[pattern].new(file) { engine.new(file) {
if file == '-' if file == '-'
$stdin.read $stdin.read
else else
Expand Down

0 comments on commit 0b668e2

Please sign in to comment.