Class: ApplicationFan

Inherits:
Application show all
Defined in:
lib/applfan.rb

Defined Under Namespace

Classes: CommandError

Constant Summary collapse

AVAILCMDS =
"Available commands (say -h after one for help)"
NOCOMMAND =
"No command given. Say -h for a list."
UNKNWNCMD =
"Unknown command: `%s'. Say -h for a list."
W_CMDS =
16

Constants inherited from Application

Application::ALIASES, Application::APPL_VERSION, Application::OPTIONS_ENV, Application::STOPOPT, Application::UNKNOWN, Application::UNPROCA, Application::W_ARGS, Application::W_OPTS

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Application

alias_option, all_names, cmdline_arguments, define_option, delete_option, each_option, #execute, full_name, #help, #initialize, is_cmd?, option_act, root, run, show_message, show_options, unalias_option, #version, version, #warn_unprocessed

Constructor Details

This class inherits a constructor from Application

Class Attribute Details

.commandsObject

Returns the value of attribute commands.



20
21
22
# File 'lib/applfan.rb', line 20

def commands
  @commands
end

Class Method Details

.find_command(name) ⇒ Object



22
23
24
25
26
27
# File 'lib/applfan.rb', line 22

def find_command name
  @commands.find { |c|
    next unless c.is_cmd?
    c::NAME == name or c::ALIASES.include? name
  }
end

.helpObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/applfan.rb', line 30

def help
  super do
    if block_given? then
      yield
      puts
    end
    puts self::AVAILCMDS
    puts
    @commands.each { |c|
      next unless c.is_cmd?
      puts "  %-*s  %s" % [ self::W_CMDS, c.all_names, c::SUMMARY]
    }
  end
end

Instance Method Details

#run {|sub| ... } ⇒ Object

Yields:

  • (sub)


67
68
69
70
71
72
73
74
75
# File 'lib/applfan.rb', line 67

def run
  c = @args.shift
  c or raise CommandError, self.class::NOCOMMAND
  cmd = self.class.find_command c
  cmd or raise CommandError, self.class::UNKNWNCMD % c
  sub = cmd.new c, (@args.slice! 0, @args.length)
  yield sub if block_given?
  sub.execute
end