Class: GLI::GLIOptionParser
- Inherits:
-
Object
- Object
- GLI::GLIOptionParser
- Defined in:
- lib/gli/gli_option_parser.rb
Overview
Parses the command-line options using an actual OptionParser
Instance Method Summary collapse
-
#initialize(commands, flags, switches, accepts, default_command = nil) ⇒ GLIOptionParser
constructor
A new instance of GLIOptionParser.
-
#parse_options(args) ⇒ Object
Given the command-line argument array, returns and array of size 4:.
Constructor Details
#initialize(commands, flags, switches, accepts, default_command = nil) ⇒ GLIOptionParser
Returns a new instance of GLIOptionParser.
4 5 6 7 8 9 10 |
# File 'lib/gli/gli_option_parser.rb', line 4 def initialize(commands,flags,switches,accepts,default_command = nil) @commands = commands @flags = flags @switches = switches @accepts = accepts @default_command = default_command end |
Instance Method Details
#parse_options(args) ⇒ Object
Given the command-line argument array, returns and array of size 4:
- 0
-
global options
- 1
-
command, as a Command
- 2
-
command-specific options
- 3
-
unparsed arguments
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/gli/gli_option_parser.rb', line 18 def (args) # :nodoc: args_clone = args.clone = {} command = nil = {} remaining_args = nil ,command_name,args = (OptionParserFactory.new(@flags,@switches,@accepts), args) @flags.each do |name,flag| [name] = flag.default_value unless [name] end command_name ||= @default_command || :help command = find_command(command_name) if Array(command).empty? raise UnknownCommand.new("Unknown command '#{command_name}'") elsif command.kind_of? Array raise UnknownCommand.new("Ambiguous command '#{command_name}'. It matches #{command.sort.join(',')}") end ,args = (OptionParserFactory.new(command.flags,command.switches,@accepts), command, args) command.flags.each do |name,flag| [name] = flag.default_value unless [name] end command.switches.each do |name,switch| [name] = switch.default_value unless [name] end [,command,,args] end |