Class: LearnOpen::ArgumentParser

Inherits:
Object
  • Object
show all
Defined in:
lib/learn_open/argument_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ ArgumentParser

Returns a new instance of ArgumentParser.



7
8
9
# File 'lib/learn_open/argument_parser.rb', line 7

def initialize(args)
  @args = args
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



5
6
7
# File 'lib/learn_open/argument_parser.rb', line 5

def args
  @args
end

Instance Method Details

#executeObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/learn_open/argument_parser.rb', line 35

def execute
  cli_args = parse

  editor = cli_args[:editor].empty? ? learn_config_editor : cli_args[:editor]
  cli_args.merge!(editor: editor)

  [
    cli_args[:lesson_name],
    cli_args[:editor],
    cli_args[:next],
    cli_args[:clone_only]
  ]
end

#learn_config_editorObject



29
30
31
32
33
# File 'lib/learn_open/argument_parser.rb', line 29

def learn_config_editor
  config_path = File.expand_path('~/.learn-config')
  editor = YAML.load(File.read(config_path))[:editor]
  editor.split.first
end

#parseObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/learn_open/argument_parser.rb', line 11

def parse
  options = {}
  rest = OptionParser.new do |opts|
    opts.on("--next", "open next lab") do |n|
      options[:next] = n
    end
    opts.on("--editor=EDITOR", "specify editor") do |e|
      options[:editor] = e
    end

    opts.on("--clone-only", "only download files. No shell") do |co|
      options[:clone_only] = co
    end
  end.parse(args)
  options[:lesson_name] = rest.first
  options
end