Skip to content
Browse files

refactored command line interface

Now the command-line interface...actually exists, to be honest. It uses
optparse to accept -t, -s, and -o options, as well as display help.
Hooray for progress!
  • Loading branch information...
1 parent 8090ca1 commit 30a14e7b39f040e176e0dab23f9856793b90d1dd @leafstorm committed Dec 25, 2011
Showing with 37 additions and 8 deletions.
  1. +37 −8 railgen.rb
View
45 railgen.rb
@@ -22,6 +22,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
+require 'optparse'
require 'haml'
require 'yaml'
@@ -255,23 +256,51 @@ def each_line
class RenderContext
- def initialize (network)
+ def initialize (network, stylesheet)
@network = network
- @stylesheet = "rail-style.css"
+ @stylesheet = stylesheet
end
end
-def render_template(template_name, network)
+
+def render_template(template_name, network, stylesheet)
haml = Haml::Engine.new(File.read(template_name))
- haml.render RenderContext.new(network)
+ haml.render RenderContext.new(network, stylesheet)
end
-
+
if __FILE__ == $0
+ options = {:template => "templates/listing.haml", :output => nil,
+ :style => "rail-style.css"}
+ option_parser = OptionParser.new do |opts|
+ opts.banner = "Usage: ruby railgen.rb [OPTIONS] DATAFILE"
+
+ opts.on("-t", "--template FILE", "Use this Haml template") do |t|
+ options[:template] = t
+ end
+ opts.on("-o", "--output FILE", "Write output to this HTML file") do |f|
+ options[:output] = f
+ end
+ opts.on("-s", "--stylesheet LINK", "Use this stylesheet reference") do |s|
+ options[:style] = s
+ end
+ opts.on_tail("-h", "--help", "Display help") do
+ puts opts
+ exit
+ end
+ end
+ option_parser.parse!
+
data = ARGV[0]
- html = ARGV[1]
+ unless data
+ puts option_parser
+ exit
+ end
+ template = options[:template]
+ output = (options[:output] or File.basename(template).gsub("haml", "html"))
+
network = RailNetwork.from_file(data)
- File.open(html, 'w') do |io|
- io.write render_template("templates/listing.haml", network)
+ File.open(output, 'w') do |io|
+ io.write render_template(template, network, options[:style])
end
end

0 comments on commit 30a14e7

Please sign in to comment.
Something went wrong with that request. Please try again.