Sha256: 6a7514bf5da85cd8171d50240eb699b0da353617994f311de19de6817e5ff105

Contents?: true

Size: 1.46 KB

Versions: 3

Compression:

Stored size: 1.46 KB

Contents

# encoding: utf-8

module CLAide
  class Command
    # Loads a command instances from arguments.
    #
    module Parser
      # @param  [Array, ARGV] argv
      #         A list of (remaining) parameters.
      #
      # @return [Command] An instance of the command class that was matched by
      #         going through the arguments in the parameters and drilling down
      #         command classes.
      #
      def self.parse(command, argv)
        argv = ARGV.coherce(argv)
        cmd = argv.arguments.first
        if cmd && subcommand = command.find_subcommand(cmd)
          argv.shift_argument
          parse(subcommand, argv)
        elsif command.abstract_command? && command.default_subcommand
          load_default_subcommand(command, argv)
        else
          command.new(argv)
        end
      end

      # @param  [Array, ARGV] argv
      #         A list of (remaining) parameters.#
      #
      # @return [Command] Returns the default subcommand initialized with the
      #         given arguments.
      #
      def self.load_default_subcommand(command, argv)
        default_subcommand = command.default_subcommand
        subcommand = command.find_subcommand(default_subcommand)
        unless subcommand
          raise 'Unable to find the default subcommand ' \
            "`#{default_subcommand}` for command `#{self}`."
        end
        result = parse(subcommand, argv)
        result.invoked_as_default = true
        result
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
claide-0.7.0 lib/claide/command/parser.rb
claide-0.6.1 lib/claide/command/parser.rb
claide-0.6.0 lib/claide/command/parser.rb