Class: Kerplutz::Executable
- Inherits:
-
Object
- Object
- Kerplutz::Executable
- Extended by:
- Forwardable
- Defined in:
- lib/kerplutz/executable.rb
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
-
#top ⇒ Object
readonly
Returns the value of attribute top.
Instance Method Summary collapse
- #add_command(command) ⇒ Object
- #add_option(option) ⇒ Object
- #banner ⇒ Object
-
#initialize(name, arguments = {}) ⇒ Executable
constructor
A new instance of Executable.
-
#parse(args) ⇒ Object
TODO: Extract this method into separate class that gives all the involved parties the chance to parse all of the args.
Constructor Details
#initialize(name, arguments = {}) ⇒ Executable
Returns a new instance of Executable.
12 13 14 15 16 |
# File 'lib/kerplutz/executable.rb', line 12 def initialize(name, arguments={}) @arguments = arguments @top = Command.new(name, '', @arguments) @commands = CommandMap.new end |
Instance Attribute Details
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
8 9 10 |
# File 'lib/kerplutz/executable.rb', line 8 def arguments @arguments end |
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
8 9 10 |
# File 'lib/kerplutz/executable.rb', line 8 def commands @commands end |
#top ⇒ Object (readonly)
Returns the value of attribute top.
8 9 10 |
# File 'lib/kerplutz/executable.rb', line 8 def top @top end |
Instance Method Details
#add_command(command) ⇒ Object
18 19 20 |
# File 'lib/kerplutz/executable.rb', line 18 def add_command(command) commands << command end |
#add_option(option) ⇒ Object
22 23 24 |
# File 'lib/kerplutz/executable.rb', line 22 def add_option(option) top.add_option(option, false) end |
#banner ⇒ Object
54 55 56 57 58 59 |
# File 'lib/kerplutz/executable.rb', line 54 def help = "" help << top.help << "\n" help << " Commands:\n" << commands.summary << "\n" help << "Type '#{name} help COMMAND' for help with a specific command.\n" end |
#parse(args) ⇒ Object
TODO: Extract this method into separate class that gives all the involved parties the chance to parse all of the args
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/kerplutz/executable.rb', line 28 def parse(args) if args[0] =~ /^(--help|help)$/ first, *rest = args if rest.empty? puts else puts commands[rest.first].help end elsif cmd = args.find { |el| commands.has_command?(el) } cmd_idx = args.index(cmd) top_args, cmd_args = args.partition.with_index do |_arg, idx| idx < cmd_idx end top.parse(top_args) remainder = commands[cmd].parse(cmd_args[1..-1]) else if args.empty? puts else remainder = top.parse(args) end end [(cmd and cmd.to_sym) || name.to_sym, arguments, remainder || []] end |