class Angumine::CLI

Public Class Methods

fail() click to toggle source
# File lib/angumine/cli.rb, line 12
def self.fail
  raise ArgumentError.new("Usage: angumine [-r] path_or_file1 [path_or_file2 [...]]\n\n  -r - recursively search specified path(s)")
end
parse(*args) click to toggle source
# File lib/angumine/cli.rb, line 16
def self.parse(*args)
  fail if args.length == 0

  recursive = args.include?('-r') ? !!args.delete('-r') : false
  fail if args.any? {|a|a.start_with?('-')}

  pathnames = []
  if recursive
    # recursively list all files under specified paths
    require 'find'
    args.each do |dir_or_file_path|
      if FileTest.directory?(dir_or_file_path)
        Find.find(dir_or_file_path) do |path|
          if FileTest.directory?(path)
            # ignore dot directories
            if File.basename(path)[0] == ?.
              Find.prune
            else
              next
            end
          else
            pathnames << path
          end
        end
      else
        pathnames << path
      end
    end
  else
    args.each do |dir_or_file_path|
      if FileTest.directory?(dir_or_file_path)
        puts "Note: non-recursively listing directory '#{dir_or_file_path}'. For recursive search, use -r.\n\n"
        pathnames += Dir.glob(dir_or_file_path).reject {|path| FileTest.directory?(path) || File.basename(path)[0] == ?.}
      else
        pathnames << dir_or_file_path
      end
    end
  end

  if pathnames.length == 0
    puts "No templates found."
    return
  end

  pathnames.each do |pathname|
    puts
    puts '-' * pathname.length
    puts pathname
    puts '-' * pathname.length
    puts
    tree_path = []
    File.open(pathname, 'r') do |file|
      Angumine.parse(IO.read(file)).each {|s| puts s}
    end
  end
end