Class: AsciiChem::Cli

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

Overview

Thor-based command line interface. Invoked via the asciichem executable (exe/asciichem).

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

Override banner to use lowercase program name consistently.



77
78
79
# File 'lib/asciichem/cli.rb', line 77

def self.banner(command, _namespace = nil, _subcommand = false)
  "asciichem #{command.usage}"
end

Instance Method Details

#convertObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/asciichem/cli.rb', line 20

def convert
  source = read_source
  formula = AsciiChem.parse(source)
  puts render(formula, options[:format])
rescue AsciiChem::ParseError => e
  warn "Parse error: #{e.message}"
  exit 1
rescue AsciiChem::FormatError => e
  warn "Format error: #{e.message}"
  exit 2
end

#lintObject



60
61
62
63
64
65
66
67
68
# File 'lib/asciichem/cli.rb', line 60

def lint
  formula = AsciiChem.parse(options[:input])
  diagnostics = AsciiChem::Linter.run(formula)
  diagnostics.each { |d| puts d }
  exit diagnostics.any? { |d| d.severity == :error } ? 1 : 0
rescue AsciiChem::ParseError => e
  warn "Parse error: #{e.message}"
  exit 1
end

#parse_cmlObject



35
36
37
38
39
40
41
# File 'lib/asciichem/cli.rb', line 35

def parse_cml
  formula = AsciiChem::Cml.parse(options[:input])
  puts formula.to_text
rescue AsciiChem::Error => e
  warn "CML parse error: #{e.message}"
  exit 1
end

#roundtripObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/asciichem/cli.rb', line 45

def roundtrip
  original = options[:input]
  rendered = AsciiChem.parse(original).to_text
  if rendered == original
    puts rendered
    exit 0
  else
    warn "round-trip mismatch:\n  input:    #{original.inspect}\n  rendered: #{rendered.inspect}"
    exit 1
  end
end

#versionObject



72
73
74
# File 'lib/asciichem/cli.rb', line 72

def version
  puts "asciichem #{AsciiChem::VERSION}"
end