Class: CommandMapper::Gen::Parsers::Man

Inherits:
Help
  • Object
show all
Defined in:
lib/command_mapper/gen/parsers/man.rb

Constant Summary collapse

SECTION_REGEXP =
/^[A-Z ]+$/
INDENT =
'       '
OPTION_LINE =
/^#{INDENT}-(?:[A-Za-z0-9]|-[A-Za-z0-9])/

Constants inherited from Help

Help::IGNORED_ARGUMENT_NAMES, Help::SUBCOMMAND, Help::SUBCOMMAND_LINE, Help::USAGE_LINE, Help::USAGE_PREFIX, Help::USAGE_SECTION

Instance Attribute Summary

Attributes inherited from Help

#command, #parser_error_callback

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Help

#ignore_argument?, #initialize, parse, #parse_argument, #parse_argument_node, #parse_arguments, #parse_option_line, #parse_subcommand_line, #parse_usage

Constructor Details

This class inherits a constructor from CommandMapper::Gen::Parsers::Help

Class Method Details

.run(command) ⇒ Command?

Parses the command's man page.

Parameters:

  • command (Command)

    The command object to parse data into.

Returns:

  • (Command, nil)

    Returns nil if the command could not be found.

Raises:



21
22
23
24
25
26
27
28
29
# File 'lib/command_mapper/gen/parsers/man.rb', line 21

def self.run(command)
  output = begin
             `man #{command.man_page} 2>/dev/null`
           rescue Errno::ENOENT
             raise(CommandNotInstalled,"the 'man' command is not installed")
           end

  parse(output,command) unless (output.nil? || output.empty?)
end

Instance Method Details

#parse(output) ⇒ Object

Parses the man page output into Help#command.

Parameters:

  • output (String)

    The plain-text man page output to parse.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/command_mapper/gen/parsers/man.rb', line 53

def parse(output)
  section = nil

  output.each_line do |line|
    line.chomp!

    if line =~ SECTION_REGEXP
      section = line
    else
      case section
      when 'SYNOPSIS'
        # SYNPSIS lines are indented
        if line.start_with?(INDENT)
          parse_synopsis(line.chomp)
        end
      when 'DESCRIPTION', 'OPTIONS'
        if line =~ OPTION_LINE
          parse_option_line(line.chomp)
        end
      end
    end
  end
end

#parse_synopsis(line) ⇒ Object

Parses a command synopsis line.

Parameters:

  • line (String)

    The command string.



37
38
39
# File 'lib/command_mapper/gen/parsers/man.rb', line 37

def parse_synopsis(line)
  parse_usage(line.strip)
end