# Class: YARD::CLI::CommandParser

Inherits:
  Object
  - Object
  - YARD::CLI::CommandParser
  show all

Defined in:
  lib/yard/cli/command_parser.rb

## Overview

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

    $ yard 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`).

## Adding a Command

To add a custom command via plugin, create a mapping in CommandParser.commands from the Symbolic command name to the Command class that implements the command. To implement a command, see the documentation for the Command class.

See Also:

- Command
- CommandParser.commands
- CommandParser.default_command

## Class Attribute Summary collapse

- .commands ⇒ Hash{Symbol => Command}

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

- .default_command ⇒ Symbol

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

## Class Method Summary collapse

- .run(*args) ⇒ void

  Convenience method to create a new CommandParser and call #run.

## Instance Method Summary collapse

- #initialize ⇒ CommandParser constructor

  A new instance of CommandParser.

- #run(*args) ⇒ void

  Runs the Command object matching the command name of the first argument.

## Constructor Details

### #initialize ⇒ `CommandParser`

Returns a new instance of CommandParser.

    56
    57
    58

    # File 'lib/yard/cli/command_parser.rb', line 56

    def initialize
      log.show_backtraces = false
    end

## Class Attribute Details

### .commands ⇒ `Hash{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.

    27
    28
    29

    # File 'lib/yard/cli/command_parser.rb', line 27

    def commands
      @commands
    end

### .default_command ⇒ `Symbol`

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

    31
    32
    33

    # File 'lib/yard/cli/command_parser.rb', line 31

    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

    54

    # File 'lib/yard/cli/command_parser.rb', line 54

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

## Instance Method Details

### #run(*args) ⇒ `void`

This method returns an undefined value.

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

    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76

    # File 'lib/yard/cli/command_parser.rb', line 63

    def run(*args)
      unless args == ['--help']
        if args.empty? || args.first =~ /^-/
          command_name = self.class.default_command
        else
          command_name = args.first.to_sym
          args.shift
        end
        if commands.key?(command_name)
          return commands[command_name].run(*args)
        end
      end
      list_commands
    end