module Subcommands

Public Instance Methods

_check_alias(cmd) click to toggle source
# File lib/subcommand.rb, line 205
def _check_alias cmd
  alas = @aliases[cmd]
  #$stderr.puts "195 alas: #{alas} "
  if alas
    case alas
    when Array
      cmd = alas.shift
      #$stderr.puts "Array cmd: #{cmd} "
      ARGV.unshift alas.shift unless alas.empty?
      #$stderr.puts "ARGV  #{ARGV} "
    else
      cmd = alas
    end
  end
  sc = @commands[cmd] if cmd
  return sc, cmd
end
add_help_option() click to toggle source

this is so that on pressing –help he gets same subcommand help as when doing help. This is to be added in your main program, after defining global options if you want detailed help on –help. This is since optionparser’s default –help will not print your actions/commands

# File lib/subcommand.rb, line 126
def add_help_option
  global_options do |opts|
    opts.on("-h", "--help", "Print this help") do |v|
      add_subcommand_help
      puts @global
      exit
    end
  end
end
add_subcommand_help() click to toggle source

add text of subcommands in help and –help option

# File lib/subcommand.rb, line 110
def add_subcommand_help
  # user has defined some, but lets add subcommand information

  cmdtext = print_actions

  global_options do |opts|
    # lets add the description user gave into banner
    opts.banner << "\n#{opts.description}\n" if opts.description
    opts.separator ""
    opts.separator cmdtext
  end
end
alias_command(name, *args) click to toggle source
# File lib/subcommand.rb, line 202
def alias_command name, *args
  @aliases[name.to_s] = args
end
command(*names) { |opts| ... } click to toggle source

specify a single command and all its options If multiple names are given, they are treated as aliases. Do repeatedly for each command Yields the optionparser

# File lib/subcommand.rb, line 64
def command *names
  name = names.shift
  @commands ||= {}
  @aliases ||= {}
  if names.length > 0
    names.each do |n| 
      #puts "aliases #{n} => #{name} "
      @aliases[n.to_s] = name.to_s
    end
  end
  # Thanks to Robert Klemme for the lazy loading idea.
  opt = lambda { OptionParser.new do |opts|
    yield opts
    # append desc to banner in next line
    opts.banner << "\n#{opts.description}\n" if opts.description
  end }
  @commands[name.to_s] = opt
end
global_options() { |opts| ... } click to toggle source

specify global options and banner and description Yields the optionparser

# File lib/subcommand.rb, line 84
def global_options
  if !defined? @global
    @global = OptionParser.new do |opts|
      yield opts
    end
  else
    yield @global
  end
end
opt_parse() click to toggle source

first parse global optinos then parse subcommand options if valid subcommand special case of “help command” so we print help of command - git style (3) in all invalid cases print global help @return command name if relevant

# File lib/subcommand.rb, line 140
def opt_parse
  # if user has not defined global, we need to create it
  @command_name = nil
  if !defined? @global
    global_options do |opts|
      opts.banner = "Usage: #{$0} [options] [subcommand [options]]"
      opts.separator ""
      opts.separator "Global options are:"
      opts.on("-h", "--help", "Print this help") do |v|
        add_subcommand_help
        puts @global
        exit
      end
      opts.separator ""
      #opts.separator subtext # FIXME: no such variable supposed to have subcommand help
    end
  else
  end
  @global.order!
  cmd = ARGV.shift
  if cmd
    #$stderr.puts "Command: #{cmd}, args:#{ARGV}, #{@commands.keys} "
    sc = @commands[cmd] 
    #puts "sc: #{sc}: #{@commands}"
    unless sc
      # see if an alias exists
      sc, cmd = _check_alias cmd
    end
    # if valid command parse the args
    if sc
      @command_name = cmd
      sc.call.order!
    else
      # else if help <command> then print its help GIT style (3)
      if !ARGV.empty? && cmd == "help"
        cmd = ARGV.shift
        #$stderr.puts " 110 help #{cmd}"
        sc = @commands[cmd]
        # if valid command print help, else print global help
        unless sc
          sc, cmd = _check_alias cmd
        end
        if sc
          #puts " 111 help #{cmd}"
          puts sc.call
        else 
          # no help for this command XXX check for alias
          puts "Invalid command: #{cmd}."
          add_subcommand_help
          puts @global
        end
      else
        # invalid command
        puts "Invalid command: #{cmd}" unless cmd == "help"
        add_subcommand_help
        puts @global 
      end
      exit 0
    end
  end
  return @command_name
end
print_actions() click to toggle source