Class: Phoenx::Cli::Command
- Inherits:
-
Object
- Object
- Phoenx::Cli::Command
- Defined in:
- lib/phoenx/cli/command.rb
Constant Summary collapse
- @@indent =
20
Instance Attribute Summary collapse
-
#base_command ⇒ Object
Returns the value of attribute base_command.
-
#description ⇒ Object
Returns the value of attribute description.
-
#name ⇒ Object
Returns the value of attribute name.
-
#usage ⇒ Object
Returns the value of attribute usage.
Instance Method Summary collapse
- #add_command(command) ⇒ Object
- #add_option(option) ⇒ Object
-
#initialize(name, description, &block) ⇒ Command
constructor
A new instance of Command.
- #parse(args) ⇒ Object
- #print ⇒ Object
Constructor Details
#initialize(name, description, &block) ⇒ Command
Returns a new instance of Command.
18 19 20 21 22 23 24 |
# File 'lib/phoenx/cli/command.rb', line 18 def initialize(name, description, &block) @name = name @description = description @block = block @options = [] @commands = [] end |
Instance Attribute Details
#base_command ⇒ Object
Returns the value of attribute base_command.
9 10 11 |
# File 'lib/phoenx/cli/command.rb', line 9 def base_command @base_command end |
#description ⇒ Object
Returns the value of attribute description.
8 9 10 |
# File 'lib/phoenx/cli/command.rb', line 8 def description @description end |
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/phoenx/cli/command.rb', line 7 def name @name end |
#usage ⇒ Object
Returns the value of attribute usage.
10 11 12 |
# File 'lib/phoenx/cli/command.rb', line 10 def usage @usage end |
Instance Method Details
#add_command(command) ⇒ Object
30 31 32 |
# File 'lib/phoenx/cli/command.rb', line 30 def add_command(command) @commands << command end |
#add_option(option) ⇒ Object
26 27 28 |
# File 'lib/phoenx/cli/command.rb', line 26 def add_option(option) @options << option end |
#parse(args) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/phoenx/cli/command.rb', line 34 def parse(args) args_tmp = args.dup if args_tmp.empty? @block.call self else args_tmp.each do |arg| @commands.each do |command| if arg == command.name args_tmp.delete(arg) command.parse(args_tmp) end end @options.each do |option| if arg == option.name || arg == option.short_cut args_tmp.delete(arg) option.execute end end end end unless args_tmp.empty? error = "Unknown arguments: " args_tmp.each do |arg| error += arg + " " end puts error print exit end end |
#print ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/phoenx/cli/command.rb', line 65 def print puts puts "Usage".underline puts cmd = " $ ".bold + base_command unless @commands.empty? cmd += " " + "[command]".green.bold end unless @options.empty? cmd += " [options]".blue.bold end puts cmd unless @usage.nil? puts puts " " + @usage end if !@commands.empty? puts puts "Commands".underline puts @commands.each do |command| puts " ".green + command.name.green.bold + whitespace(command.name) + command.description end end if !@options.empty? puts puts "Options".underline puts @options.each do |option| output = option.name + ", " + option.short_cut puts " " + output.bold.blue + whitespace(output) + option.description end end puts end |