Class: Inch::CLI::CommandParser

Inherits:
Object
  • Object
show all
Includes:
TraceHelper
Defined in:
lib/inch/cli/command_parser.rb

Overview

This class parses a command name out of the inch CLI command and calls that command in the form:

$ inch command_name [options]

If no command or arguments are specified, or if the arguments immediately begin with a --opt (not --help), the CommandParser.default_command will be used (which itself defaults to :doc).

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TraceHelper

#debug, #trace, #trace_header

Constructor Details

#initializeCommandParser

Returns a new instance of CommandParser.



43
44
45
# File 'lib/inch/cli/command_parser.rb', line 43

def initialize
  #log.show_backtraces = false
end

Class Attribute Details

.commandsHash{Symbol => Command}

Returns the mapping of command names to command classes to parse the user command.

Returns:

  • (Hash{Symbol => Command})

    the mapping of command names to command classes to parse the user command.



19
20
21
# File 'lib/inch/cli/command_parser.rb', line 19

def commands
  @commands
end

.default_commandSymbol

Returns the default command name to use when no options are specified or.

Returns:

  • (Symbol)

    the default command name to use when no options are specified or



23
24
25
# File 'lib/inch/cli/command_parser.rb', line 23

def default_command
  @default_command
end

Class Method Details

.run(*args) ⇒ void

This method returns an undefined value.

Convenience method to create a new CommandParser and call #run



39
40
41
# File 'lib/inch/cli/command_parser.rb', line 39

def self.run(*args)
  new.run(*args)
end

Instance Method Details

#run(*args) ⇒ void

This method returns an undefined value.

Runs the Inch::CLI::Command object matching the command name of the first argument.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/inch/cli/command_parser.rb', line 50

def run(*args)
  unless ['--help', '-h'].include?(args.join)
    if args.size == 0 || args.first =~ /^-/
      command_name = self.class.default_command
    else
      command_name = args.first.to_sym
      args.shift
    end
    if commands.has_key?(command_name)
      return commands[command_name].run(*args)
    end
  end
  list_commands
end