Class: Gly::CLI

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

Overview

implements the ‘gly’ executable

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.handle_no_command_error(command, has_namespace = $thor_runner) ⇒ Object

override Thor’s default handler



141
142
143
144
145
146
147
# File 'lib/gly/cli.rb', line 141

def handle_no_command_error(command, has_namespace=$thor_runner)
  if has_namespace
    fail Thor::UndefinedCommandError, "Could not find command #{command.inspect} in #{namespace.inspect} namespace."
  else
    fail Thor::UndefinedCommandError, "Could not find command #{command.inspect}. Did you mean 'gly preview #{command}' ?"
  end
end

Instance Method Details

#fy(*files) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/gly/cli.rb', line 103

def fy(*files)
  check_lygre_available!

  files.each_with_index do |f,i|
    input = File.read f

    parser = GabcParser.new
    result = parser.parse(input)

    if result then
      puts if i >= 1
      GlyConvertor.new.convert result.create_score, STDOUT
    else
      STDERR.puts 'glyfy considers the input invalid gabc:'
      STDERR.puts
      STDERR.puts "'#{parser.failure_reason}' on line #{parser.failure_line} column #{parser.failure_column}:"
      STDERR.puts input.split("\n")[parser.failure_line-1]
      STDERR.puts (" " * parser.failure_column) + "^"
      return false
    end
  end
end

#gabc(*files) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/gly/cli.rb', line 14

def gabc(*files)
  files.each do |f|
    DocumentGabcConvertor.new(parser.parse(f)).convert
  end
rescue Gly::Exception => ex
  error_exit! ex
end

#list(*files) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/gly/cli.rb', line 65

def list(*files)
  if files.empty?
    STDERR.puts 'No file specified.'
    exit 1
  end

  if options[:recursive]
    files = files.collect do |f|
      if File.directory?(f)
        Dir[File.join(f, '**/*.gly')]
      else
        f
      end
    end
    files.flatten!
  end

  lister = Lister.new(files, options[:format])
  lister.list(STDOUT, STDERR)

  exit(lister.error? ? 1 : 0)
rescue Gly::Exception => ex
  error_exit! ex
end

#ly(*files) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/gly/cli.rb', line 91

def ly(*files)
  check_lygre_available!

  files.each do |f|
    DocumentLyConvertor.new(parser.parse(f)).convert
  end

rescue Gly::Exception => ex
  error_exit! ex
end

#preview(*files) ⇒ Object



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
# File 'lib/gly/cli.rb', line 27

def preview(*files)
  tpl = nil
  if options[:template]
    begin
      tpl = File.read(options[:template])
    rescue Errno::ENOENT
      raise Gly::Exception.new("File not found: '#{options[:template]}'")
    end
  end

  # convert HashWithIndifferentAccess to Hash
  # with Symbol keys - options.to_h would make Hash
  # with String keys
  opts = options.each_pair.collect {|k,v| [k.to_sym,v]}.to_h

  opts[:suffix_always] = true

  files.each do |f|
    gen = PreviewGenerator.new template: tpl, options: opts
    begin
      gen.process(parser.parse(f))
    rescue Errno::ENOENT
      with_extension = f + '.gly'
      if File.exist? with_extension
        gen.process(parser.parse(with_extension))
      else
        raise Gly::Exception.new("File not found: '#{f}'")
      end
    end
  end

rescue Gly::Exception => ex
  error_exit! ex
end