Class: Angumine::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/angumine/cli.rb

Class Method Summary collapse

Class Method Details

.failObject

Raises:

  • (ArgumentError)


12
13
14
# 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) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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