module BaseChip::Cli

Public Class Methods

included(mod) click to toggle source
# File lib/base_chip/cli.rb, line 44
def self.included mod
  mod.extend Cli::ClassMethods
  mod.class_eval do
    include BaseChip::Reporting
    include Cli::InstanceMethods

    @cli    = Cli::Data.new

    desc    "Show usage information"
    example ""
    example " #{cli.tasks[1]}" if cli.tasks[1]
    def help(command = nil,*subcommands)
      puts ''
      if command 
        command = command.to_sym
        if (sc = cli.sub_commands[command])
          sc.new.help(*subcommands)
          return
        elsif t_cli = cli.tasks[command]
          puts "#{cli.full_command(command)} #{t_cli.usage}:"
        else
          fault "could not find help topic \"#{command}\""
        end
      else
        t_cli = cli

        puts "#{cli.full_command(command,false)}: "
      end

      if t_cli.description
        puts ''
        puts "    #{t_cli.description}"
      end
      if t_cli.long_description
        puts ''
        puts "    #{t_cli.long_description}"
      end
      
      unless command
        array = []
        cli.tasks.each_value do |t|
          if t.full_usage 
            array << [t.full_usage                                 ,t.description]
          else
            array << ["#{cli.full_command(t.name.to_s)} #{t.usage}",t.description]
          end
        end
        cli.sub_commands.each do |name,sc|
          array << ["#{sc.cli.full_command(nil,'false')} #{name} ...",sc.cli.description]
        end
        puts ''
        puts "    Commands:"
        cli.table(array,nil,"        ")

      end

      t_cli.options_table
      puts ''
    end
  end
end

Public Instance Methods

help(command = nil,*subcommands) click to toggle source
# File lib/base_chip/cli.rb, line 55
def help(command = nil,*subcommands)
  puts ''
  if command 
    command = command.to_sym
    if (sc = cli.sub_commands[command])
      sc.new.help(*subcommands)
      return
    elsif t_cli = cli.tasks[command]
      puts "#{cli.full_command(command)} #{t_cli.usage}:"
    else
      fault "could not find help topic \"#{command}\""
    end
  else
    t_cli = cli

    puts "#{cli.full_command(command,false)}: "
  end

  if t_cli.description
    puts ''
    puts "    #{t_cli.description}"
  end
  if t_cli.long_description
    puts ''
    puts "    #{t_cli.long_description}"
  end
  
  unless command
    array = []
    cli.tasks.each_value do |t|
      if t.full_usage 
        array << [t.full_usage                                 ,t.description]
      else
        array << ["#{cli.full_command(t.name.to_s)} #{t.usage}",t.description]
      end
    end
    cli.sub_commands.each do |name,sc|
      array << ["#{sc.cli.full_command(nil,'false')} #{name} ...",sc.cli.description]
    end
    puts ''
    puts "    Commands:"
    cli.table(array,nil,"        ")

  end

  t_cli.options_table
  puts ''
end