Class: Kut::Application
- Inherits:
-
Object
- Object
- Kut::Application
- Defined in:
- lib/kut/application.rb
Instance Attribute Summary collapse
-
#commands ⇒ Object
Returns the value of attribute commands.
Instance Method Summary collapse
- #command_by_name(cmd_name) ⇒ Object
-
#initialize ⇒ Application
constructor
A new instance of Application.
- #run ⇒ Object
- #run_command(cmd_name, args) ⇒ Object
- #show_help ⇒ Object
Constructor Details
#initialize ⇒ Application
Returns a new instance of Application.
8 9 10 11 12 |
# File 'lib/kut/application.rb', line 8 def initialize @option_parser = OptionParser.new @options = OpenStruct.new @commands = [] end |
Instance Attribute Details
#commands ⇒ Object
Returns the value of attribute commands.
6 7 8 |
# File 'lib/kut/application.rb', line 6 def commands @commands end |
Instance Method Details
#command_by_name(cmd_name) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/kut/application.rb', line 25 def command_by_name(cmd_name) cmd = nil commands.each { |c| if c.name == cmd_name cmd = c break end } return cmd end |
#run ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/kut/application.rb', line 46 def run show_help() unless ARGV show_help() if ARGV.size() == 0 show_help() if ['-h', '--help'].include?(ARGV[0]) args = ARGV.dup() args.delete_at(0) run_command(ARGV[0], args) end |
#run_command(cmd_name, args) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/kut/application.rb', line 36 def run_command(cmd_name, args) cmd = command_by_name(cmd_name) if cmd then cmd.run(args) else puts "kut: #{cmd_name} is not a kut-command. See 'kut --help'" exit end end |
#show_help ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/kut/application.rb', line 14 def show_help puts 'usage: kut [--help] COMMAND [ARGS]' puts 'The most commonly used kut commands are:' cmd_name_len = 10 commands.each { |cmd| cmd_space = ' ' * (cmd_name_len - cmd.name.size) puts "#{' '*3}#{cmd.name}#{cmd_space}#{cmd.}" } exit end |