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



38
39
40
41
42
43
44
# File 'lib/kapusta/cli.rb', line 38

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



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/kapusta/cli.rb', line 26

def self.parse_options(args)
  options = Options.new(compile: false, help: 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 }
  end.order!(args)

  options
end

.run_file(args) ⇒ Object



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

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
# 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.compile
    compile_file(args)
  else
    run_file(args)
  end
end

.usageObject



57
58
59
# File 'lib/kapusta/cli.rb', line 57

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