Class: Inch::CLI::CommandParser

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

Overview

CommandParser parses a command-line arguments to decide which Command to run.

The basic form translates this shell command

$ inch command_name [options]

into a method call on the corresponding Command class.

Some examples:

$ inch
# >>> Command::Suggest.new.run()

$ inch --pedantic
# >>> Command::Suggest.new.run("--pedantic")

As you can see, if no command_name is given, the CommandParser.default_command will be used.

$ inch list --all
# >>> Command::List.new.run("--all")

If a command_name is found to match a Command, that Command will be used.

$ inch --help
# >>> CommandParser#list_commands

The --help switch is an exception and lists all available commands.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TraceHelper

#ui

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.



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

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



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

def default_command
  @default_command
end

Class Method Details

.run(*args) ⇒ Command::Base

Convenience method to create a new CommandParser and call #run

Returns:



50
51
52
# File 'lib/inch/cli/command_parser.rb', line 50

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

Instance Method Details

#run(*args) ⇒ Command::Base

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

Returns:



57
58
59
60
61
62
63
# File 'lib/inch/cli/command_parser.rb', line 57

def run(*args)
  if ['--help', '-h'].include?(args.join)
    list_commands
  else
    run_command(*args)
  end
end