Class: Kapusta::CLI

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

Defined Under Namespace

Classes: Options

Class Method Summary collapse

Class Method Details

.compile_file(args) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/kapusta/cli.rb', line 47

def self.compile_file(args)
  path = args.shift
  abort usage unless path
  abort usage unless args.empty?

  $stdout.write(Kapusta.compile(File.read(path), path:))
end

.parse_options(args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kapusta/cli.rb', line 34

def self.parse_options(args)
  options = Options.new(compile: false, help: false, version: false)

  OptionParser.new do |parser|
    parser.banner = usage
    parser.on('-c', '--compile', 'Compile .kap to Ruby') { options.compile = true }
    parser.on('-h', '--help', 'Show this help') { options.help = true }
    parser.on('-v', '--version', 'Show version') { options.version = true }
  end.order!(args)

  options
end

.run_file(args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/kapusta/cli.rb', line 55

def self.run_file(args)
  path = args.shift
  abort usage unless path

  previous_argv = ARGV.dup
  ARGV.replace(args)
  Kapusta.dofile(path)
ensure
  ARGV.replace(previous_argv) if previous_argv
end

.start(argv = ARGV) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/kapusta/cli.rb', line 10

def self.start(argv = ARGV)
  args = argv.dup
  options = parse_options(args)

  if options.help
    $stdout.puts usage
    return
  end

  if options.version
    $stdout.puts "kapusta #{Kapusta::VERSION}"
    return
  end

  if options.compile
    compile_file(args)
  else
    run_file(args)
  end
rescue Kapusta::Error => e
  warn e.formatted
  exit 1
end

.usageObject



66
67
68
# File 'lib/kapusta/cli.rb', line 66

def self.usage
  'usage: kapusta [--compile|-c] <file.kap> | kapusta <file.kap> [args...]'
end