exe/dirtree in dirtree-0.1.0 vs exe/dirtree in dirtree-0.2.0
- old
+ new
@@ -2,35 +2,62 @@
require 'dirtree'
require 'optparse'
require 'json'
require 'erb'
-options = {}
+templates_dir = File.join(File.dirname(__FILE__), '..', 'templates')
+templates = Dir.open(templates_dir).map do |file|
+ file[0...file.index('.')]
+end.reject(&:empty?)
+
+options = {
+ template: 'tree'
+}
+
OptionParser.new do |opts|
opts.banner = 'Usage: dirtree [options]... [file]...'
- opts.on('-v', '--version', 'Print version') do
+ opts.on(
+ '-v',
+ '--version',
+ 'Print version'
+ ) do
puts "Dirtree version #{Dirtree::VERSION}"
exit
end
- opts.on('-h', '--help', 'Show this help text') do
+ opts.on(
+ '-h',
+ '--help',
+ 'Show this help text'
+ ) do
puts opts
exit
end
- opts.on('-oFile.html', '--output=File.html', 'Specify a path to write output, if not specified output will be printed to STDOUT') do |value|
+ opts.on(
+ '-oFile.html',
+ '--output=File.html',
+ 'Specify a path to write output, by default will be printed to STDOUT'
+ ) do |value|
options[:output] = value
end
+
+ opts.on(
+ '-tTemplateName',
+ '--template=TemplateName',
+ 'Specify the template name, available templates ' + templates.to_s
+ ) do |value|
+ options[:template] = value
+ end
end.parse!
files = ARGV.empty? ? STDIN.read.lines : ARGV
files.map!(&:strip)
root = Dirtree::Node.new('/')
files.each { |file| root.insert(file.split('/')) }
-templates_dir = File.join(File.dirname(__FILE__), '..', 'templates')
-template_file = File.join(templates_dir, 'tree.html.erb')
+template_file = File.join(templates_dir, options[:template] + '.html.erb')
template = File.read(File.expand_path(template_file))
tree = root.as_json
result = ERB.new(template).result binding