class DocoptCompgen::Parser

Public Class Methods

new(help, complete_short: false) click to toggle source
# File lib/docopt_compgen/parser.rb, line 3
def initialize(help, complete_short: false)
    @help = help
    @complete_short = complete_short
end

Public Instance Methods

to_node() click to toggle source
# File lib/docopt_compgen/parser.rb, line 8
def to_node
    build(parse_docopt, Node.new)
end

Private Instance Methods

build(pattern, node) click to toggle source
# File lib/docopt_compgen/parser.rb, line 36
def build(pattern, node)
    wrappers = [
        Docopt::Either,
        Docopt::Optional,
        Docopt::OneOrMore,
        Docopt::AnyOptions,
    ]

    if wrappers.include?(pattern.class)
        pattern.children.each do |child|
            build(child, node)
        end
    end

    if [Docopt::Required].include?(pattern.class)
        pattern.children.each do |child|
            new_node = build(child, node)
            if child.is_a?(Docopt::Command)
                node = new_node
            end
        end
    end

    if [Docopt::Option].include?(pattern.class)
        if pattern.short && @complete_short
            node.add_option(pattern.short)
        end

        if pattern.long
            node.add_option(pattern.long)
        end
    end

    if [Docopt::Argument].include?(pattern.class)
        node.add_argument(pattern.name)
    end

    if [Docopt::Command].include?(pattern.class)
        node = node.add_subcommand(pattern.name)
    end

    return node
end
parse_docopt() click to toggle source
# File lib/docopt_compgen/parser.rb, line 14
def parse_docopt
    options = Docopt.parse_defaults(@help)

    pattern = Docopt.parse_pattern(
        Docopt.formal_usage(Docopt.printable_usage(@help)),
        options,
    )

    # Options explicitly specified in subcommands cannot be used for a
    # general [options] placeholder. This is necessary so that options
    # can be restricted to specific subcommands while still allowing the
    # remaining to be used in place of [options].
    used_options = pattern.flat(Docopt::Option).uniq
    pattern.flat(Docopt::AnyOptions).each do |any_options|
        any_options.children = options.reject do |option|
            used_options.include?(option)
        end.uniq
    end

    return pattern
end