# class Chef::Knife

### Constants

OFFICIAL_PLUGINS

### Attributes

name_args[RW]
ui[RW]

### Public Class Methods

category(new_category) click to toggle source

Explicitly set the category for the current command to `new_category` The category is normally determined from the first word of the command name, but some commands make more sense using two or more words

### Arguments¶ ↑

new_category:

  A String to set the category to (see examples)

### Examples:¶ ↑

Data bag commands would be in the ‘data’ category by default. To put them in the ‘data bag’ category:

    category('data bag')

    # File lib/chef/knife.rb, line 94
    def self.category(new_category)
      @category = new_category
    end

common_name() click to toggle source

    # File lib/chef/knife.rb, line 106
    def self.common_name
      snake_case_name.split('_').join(' ')
    end

dependency_loaders() click to toggle source

    # File lib/chef/knife.rb, line 205
    def self.dependency_loaders
      @dependency_loaders ||= []
    end

deps(&block) click to toggle source

    # File lib/chef/knife.rb, line 209
    def self.deps(&block)
      dependency_loaders << block
    end

guess_category(args) click to toggle source

    # File lib/chef/knife.rb, line 177
    def self.guess_category(args)
      category_words = args.select {|arg| arg =~ /^(([[:alnum:]])[[:alnum:]\_\-]+)$/ }
      category_words.map! {|w| w.split('-')}.flatten!
      matching_category = nil
      while (!matching_category) && (!category_words.empty?)
        candidate_category = category_words.join(' ')
        matching_category = candidate_category if subcommands_by_category.key?(candidate_category)
        matching_category || category_words.pop
      end
      matching_category
    end

inherited(subclass) click to toggle source

    # File lib/chef/knife.rb, line 79
    def self.inherited(subclass)
      unless subclass.unnamed?
        subcommands[subclass.snake_case_name] = subclass
      end
    end

list_commands(preferred_category=nil) click to toggle source

Print the list of subcommands knife knows about. If `preferred_category` is given, only subcommands in that category are shown

    # File lib/chef/knife.rb, line 139
    def self.list_commands(preferred_category=nil)
      load_commands

      category_desc = preferred_category ? preferred_category + " " : ''
      msg "Available #{category_desc}subcommands: (for details, knife SUB-COMMAND --help)\n\n"

      if preferred_category && subcommands_by_category.key?(preferred_category)
        commands_to_show = {preferred_category => subcommands_by_category[preferred_category]}
      else
        commands_to_show = subcommands_by_category
      end

      commands_to_show.sort.each do |category, commands|
        next if category =~ /deprecated/i
        msg "** #{category.upcase} COMMANDS **"
        commands.sort.each do |command|
          msg subcommands[command].banner if subcommands[command]
        end
        msg
      end
    end

load_commands() click to toggle source

    # File lib/chef/knife.rb, line 119
    def self.load_commands
      @commands_loaded ||= subcommand_loader.load_commands
    end

load_deps() click to toggle source

    # File lib/chef/knife.rb, line 213
    def self.load_deps
      dependency_loaders.each do |dep_loader|
        dep_loader.call
      end
    end

msg(msg="") click to toggle source

    # File lib/chef/knife.rb, line 70
    def self.msg(msg="")
      ui.msg(msg)
    end

reset_subcommands!() click to toggle source

    # File lib/chef/knife.rb, line 74
    def self.reset_subcommands!
      @@subcommands = {}
      @subcommands_by_category = nil
    end

run(args, options={}) click to toggle source

Run knife for the given `args` (ARGV), adding `options` to the list of CLI options that the subcommand knows how to handle.

### Arguments¶ ↑

args:

  usually ARGV

options:

  A Mixlib::CLI option parser hash. These `options` are how

subcommands know about global knife CLI options

    # File lib/chef/knife.rb, line 167
    def self.run(args, options={})
      load_commands
      subcommand_class = subcommand_class_from(args)
      subcommand_class.options = options.merge!(subcommand_class.options)
      subcommand_class.load_deps
      instance = subcommand_class.new(args)
      instance.configure_chef
      instance.run_with_pretty_exceptions
    end

snake_case_name() click to toggle source

    # File lib/chef/knife.rb, line 102
    def self.snake_case_name
      convert_to_snake_case(name.split('::').last) unless unnamed?
    end

subcommand_category() click to toggle source

    # File lib/chef/knife.rb, line 98
    def self.subcommand_category
      @category || snake_case_name.split('_').first unless unnamed?
    end

subcommand_class_from(args) click to toggle source

    # File lib/chef/knife.rb, line 189
    def self.subcommand_class_from(args)
      command_words = args.select {|arg| arg =~ /^(([[:alnum:]])[[:alnum:]\_\-]+)$/ }

      subcommand_class = nil

      while ( !subcommand_class ) && ( !command_words.empty? )
        snake_case_class_name = command_words.join("_")
        unless subcommand_class = subcommands[snake_case_class_name]
          command_words.pop
        end
      end
      # see if we got the command as e.g., knife node-list
      subcommand_class ||= subcommands[args.first.gsub('-', '_')]
      subcommand_class || subcommand_not_found!(args)
    end

subcommand_loader() click to toggle source

    # File lib/chef/knife.rb, line 115
    def self.subcommand_loader
      @subcommand_loader ||= Knife::SubcommandLoader.new(chef_config_dir)
    end

subcommands() click to toggle source

    # File lib/chef/knife.rb, line 123
    def self.subcommands
      @@subcommands ||= {}
    end

subcommands_by_category() click to toggle source

    # File lib/chef/knife.rb, line 127
    def self.subcommands_by_category
      unless @subcommands_by_category
        @subcommands_by_category = Hash.new { |hash, key| hash[key] = [] }
        subcommands.each do |snake_cased, klass|
          @subcommands_by_category[klass.subcommand_category] << snake_cased
        end
      end
      @subcommands_by_category
    end

ui() click to toggle source

    # File lib/chef/knife.rb, line 66
    def self.ui
      @ui ||= Chef::Knife::UI.new(STDOUT, STDERR, STDIN, {})
    end

unnamed?() click to toggle source

Does this class have a name? (Classes created via Class.new don’t)

    # File lib/chef/knife.rb, line 111
    def self.unnamed?
      name.nil? || name.empty?
    end

use_separate_defaults?() click to toggle source

Configure mixlib-cli to always separate defaults from user-supplied CLI options

    # File lib/chef/knife.rb, line 62
    def self.use_separate_defaults?
      true
    end