Class: Miniparse::Commander

Inherits:
Object
  • Object
show all
Defined in:
lib/miniparse/commander.rb

Overview

TODO this class maybe does too much, separate a command broker or something similar

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommander

Returns a new instance of Commander.



20
21
22
23
# File 'lib/miniparse/commander.rb', line 20

def initialize
  @commands = {}
  @brokers = {}
end

Instance Attribute Details

#current_commandObject (readonly)

Returns the value of attribute current_command.



9
10
11
# File 'lib/miniparse/commander.rb', line 9

def current_command
  @current_command
end

#parsed_argsObject (readonly)

Returns the value of attribute parsed_args.



9
10
11
# File 'lib/miniparse/commander.rb', line 9

def parsed_args
  @parsed_args
end

#parsed_commandObject (readonly)

Returns the value of attribute parsed_command.



9
10
11
# File 'lib/miniparse/commander.rb', line 9

def parsed_command
  @parsed_command
end

Instance Method Details

#add_command(args, &block) ⇒ Object

:no_options indicates the command has no command line options

Parameters:

  • name

    is the command name (ex. either “kill” or :kill)

  • desc

    is a short description of the command

  • opts

    are the options to apply to the command



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/miniparse/commander.rb', line 31

def add_command(args, &block)
  spec = args[:spec]
  unless name = Command.spec_to_name(spec)
    raise SyntaxError, "unknown or invalid command specification '#{spec}'"
  end
  # only run before first command get added
  # and the user isn't trying to add his own help command
  add_help_command    if name != :help && commands.empty?
  cmd = Command.new(args, &block)
  # FEATURE if a command already exists it gets quietly overwritten (and its options lost)
  @commands[name] = cmd
  @brokers[name] = OptionBroker.new do
    puts help_command_text(name)
    exit ERR_HELP_REQ
  end
  @current_command = name    unless args[:no_options]
  cmd
end

#current_brokerObject

Returns current command broker or nil.

Returns:

  • current command broker or nil



16
17
18
# File 'lib/miniparse/commander.rb', line 16

def current_broker
  brokers[current_command]
end

#help_descObject

Returns the command general help for the commands in the commander.

Returns:

  • the command general help for the commands in the commander



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/miniparse/commander.rb', line 72

def help_desc
  text = ""
  if current_command
    names_wo_desc = []
    desc_texts = commands.sort.collect do |name, cmd| 
      if cmd.desc
        cmd.help_desc
      else
        names_wo_desc << name
        nil
      end
    end
    unless desc_texts.compact!.empty?
      text += "\nCommands:\n"
      text += desc_texts.join("\n") 
    end
    unless names_wo_desc.empty?
      text += "\nMore commands: \n"
      text += ' '*Miniparse.control(:width_indent)
      text += names_wo_desc.join(", ")
    end
  end
  text
end

#parse_argv(name, argv) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/miniparse/commander.rb', line 63

def parse_argv(name, argv)
  cmd = commands.fetch(name)
  @parsed_command = cmd.name
  @parsed_args = brokers[cmd.name].parse_argv(argv)
  commands[cmd.name].run(parsed_args)
  parsed_args
end

#parsed_valuesObject



11
12
13
# File 'lib/miniparse/commander.rb', line 11

def parsed_values
  brokers[parsed_command].parsed_values  if parsed_command
end

#split_argv(argv) ⇒ Object

Returns an array of argv parts: [global_argv, command_name, command_argv].

Parameters:

  • argv

    is ARGV like

Returns:

  • an array of argv parts: [global_argv, command_name, command_argv]



52
53
54
55
56
57
58
59
60
61
# File 'lib/miniparse/commander.rb', line 52

def split_argv(argv)
  index = index_command(argv)
  if index
    global_argv = (index == 0)  ?  []  :  argv[0..index-1]
    command_argv = argv[index+1..-1]
    [global_argv, Command.spec_to_name(argv[index]), command_argv]
  else  
    [argv, nil, []]
  end
end