Sha256: c4bee4dc63eafd62ea15873b9fd51510c83cfdc025271a7d32e5b26ecdc0751d

Contents?: true

Size: 1.65 KB

Versions: 9

Compression:

Stored size: 1.65 KB

Contents

module UltraCommandLine
  module Manager

    module Factory

      include UltraCommandLine::Utils::YamlFactory

      def from_hash(definition_hash, factory_options = {})
        UltraCommandLine.logger.debug 'Starting commands analysis from definition hash.'
        commands = []
        # Create main command
        UltraCommandLine.logger.debug 'Defining main command.'
        main_command = create_command '', definition_hash
        main_command.banner = definition_hash.fetch :banner, ''
        commands << main_command
        # Create sub-commands
        definition_hash.fetch(:subcommands, {}).each do |subcommand_name, subcommand_definition|
          UltraCommandLine.logger.debug "Defining sub-command '#{subcommand_name}'."
          # subcommand_definition ||= {}
          subcommand = create_command subcommand_name, subcommand_definition
          subcommand.banner = subcommand_definition.fetch :banner, ''
          # Let's add global options
          main_command.options.select(&:global?).each do |global_option|
            subcommand.options << global_option
          end
          commands << subcommand
        end
        if block_given?
          yield commands
        else
          # Return a new object supporting a list of commands as parameter
          new_manager = new commands
          new_manager.initialize_definition definition_hash
          commands.each {|c| c.send :manager=, new_manager}
          new_manager
        end
      end

      private

      def create_command(name, options_definition)
        UltraCommandLine::Commands::SubCommand.from_hash options_definition, name: name, manager: self
      end



    end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ultra_command_line-0.4.16 lib/ultra_command_line/manager/factory.rb
ultra_command_line-0.4.15 lib/ultra_command_line/manager/factory.rb
ultra_command_line-0.4.14 lib/ultra_command_line/manager/factory.rb
ultra_command_line-0.4.13 lib/ultra_command_line/manager/factory.rb
ultra_command_line-0.4.12 lib/ultra_command_line/manager/factory.rb
ultra_command_line-0.4.10 lib/ultra_command_line/manager/factory.rb
ultra_command_line-0.4.9 lib/ultra_command_line/manager/factory.rb
ultra_command_line-0.4.8 lib/ultra_command_line/manager/factory.rb
ultra_command_line-0.4.7 lib/ultra_command_line/manager/factory.rb