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



44
45
46
47
48
49
50
# File 'lib/kapusta/cli.rb', line 44

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



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/kapusta/cli.rb', line 31

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



52
53
54
55
56
57
58
59
60
61
# File 'lib/kapusta/cli.rb', line 52

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
# 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
end

.usageObject



63
64
65
# File 'lib/kapusta/cli.rb', line 63

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