lib/learn_open/argument_parser.rb in learn-open-1.2.22 vs lib/learn_open/argument_parser.rb in learn-open-1.2.23
- old
+ new
@@ -1,36 +1,49 @@
+require 'optparse'
+
module LearnOpen
class ArgumentParser
attr_reader :args
def initialize(args)
@args = args
end
- def execute
- config_path = File.expand_path('~/.learn-config')
- editor_data = YAML.load(File.read(config_path))[:editor]
- if editor_data.match(/ /)
- editor_data = editor_data.split(' ').first
- end
+ 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
- lesson = nil
- next_lesson = false
+ 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
- configured_editor = !(editor_data.empty? || editor_data.nil?) ? editor_data : nil
- editor_specified = ARGV.detect {|arg| arg.start_with?('--editor=')}.match(/\-\-editor=(.+)/) || configured_editor
- open_after = !!editor_specified
+ def learn_config_editor
+ config_path = File.expand_path('~/.learn-config')
+ editor = YAML.load(File.read(config_path))[:editor]
+ editor.split.first
+ end
- if !ARGV[0].start_with?('--editor=') && !ARGV[0].start_with?('--next')
- lesson = ARGV[0].sub(/\/$/, '')
- elsif ARGV[0].start_with?('--next')
- next_lesson = true
- end
+ def execute
+ cli_args = parse
- if open_after
- editor_specified = editor_specified.is_a?(String) ? editor_specified : editor_specified[1]
- end
+ editor = cli_args[:editor].empty? ? learn_config_editor : cli_args[:editor]
+ cli_args.merge!(editor: editor)
- [lesson, editor_specified, next_lesson]
+ [
+ cli_args[:lesson_name],
+ cli_args[:editor],
+ cli_args[:next],
+ cli_args[:clone_only]
+ ]
end
end
end