Class: Yap::Cli::Options

Inherits:
Object
  • Object
show all
Includes:
OptionsLoader
Defined in:
lib/yap/cli/options.rb

Defined Under Namespace

Classes: Addon, Generate

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OptionsLoader

#load_command, #load_constant_from_path, #load_relative_constant

Constructor Details

#initialize(options: {}) ⇒ Options

Returns a new instance of Options.



51
52
53
54
# File 'lib/yap/cli/options.rb', line 51

def initialize(options: {})
  @options = options
  @commands = []
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



49
50
51
# File 'lib/yap/cli/options.rb', line 49

def options
  @options
end

Instance Method Details

#[](key) ⇒ Object



56
57
58
# File 'lib/yap/cli/options.rb', line 56

def [](key)
  @options[key]
end

#commandsObject



60
61
62
# File 'lib/yap/cli/options.rb', line 60

def commands
  @commands.dup.freeze
end

#help_messageObject



102
103
104
# File 'lib/yap/cli/options.rb', line 102

def help_message
  option_parse.to_s
end

#parse(args) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/yap/cli/options.rb', line 64

def parse(args)
  option_parser.order!(args)
  options_instance = self

  Yap.configuration.run_shell = false if args.any?

  scope = []
  require_path = Pathname.new("yap/cli/options")
  args_processed = []

  while args.any?
    if args_processed == args
      puts "Unknown option(s*): #{scope.concat([args.first]).join(' ')}"
      exit 1
    end
    args_processed = args.dup

    option_str = args.shift
    current_scope = scope + [option_str]


    begin
      options_class = load_relative_constant current_scope.join('/')
      options_instance = options_class.new(options: options)
      options[:option] = options_instance
      options_instance.parse(args)
      @commands << options_instance.command

      scope << option_str
    rescue LoadError => ex
      puts "Unknown option: #{option_str}"
      puts
      puts options_instance.help_message
      exit 1
    end
  end
end