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.



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

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



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

def default_command
  @default_command
end

Class Method Details

.run(*args) ⇒ Command::Base

Convenience method to create a new CommandParser and call #run

Returns:



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

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:



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

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