Skip to content
Browse files

Running commands is now working. Stupid help method added - should

return a formatted string with all the flags, aliases and descriptions
  • Loading branch information...
1 parent 7514f34 commit c6668b9b8933df6a35e01ede3e77bd6517bbb66f @rbgrouleff committed
Showing with 73 additions and 9 deletions.
  1. +4 −4 lib/wheelbarrow.rb
  2. +58 −3 lib/wheelbarrow/command.rb
  3. +2 −2 lib/wheelbarrow/commands/setup.rb
  4. +9 −0 lib/wheelbarrow/commands/test.rb
View
8 lib/wheelbarrow.rb
@@ -1,5 +1,7 @@
require "wheelbarrow/version"
require "wheelbarrow/wagonload"
+require "wheelbarrow/command"
+require "wheelbarrow/commands/test"
module Wheelbarrow
def self.deploy app_dir, repo
@@ -12,7 +14,5 @@ def self.deploy app_dir, repo
end
end
-
-
-puts `ls`
-puts $?.exitstatus
+cmd = Wheelbarrow.Command(ARGV[0], ARGV.drop(1))
+cmd.execute if cmd
View
61 lib/wheelbarrow/command.rb
@@ -1,21 +1,76 @@
module Wheelbarrow
- def Command cmd_name, args
- Commands.const_get(cmd_name.capitalize).new args
+ def self.Command cmd_name, args
+ begin
+ Commands.get(cmd_name.capitalize).new args
+ rescue NameError => e
+ raise unless e.message.include? cmd_name.capitalize
+ puts "Command #{cmd_name} not found"
+ puts "Available commands are: #{Commands.available_commands.join ', '}"
+ end
end
module Commands
+ def self.get name
+ const_get(name)
+ end
+
+ def self.available_commands
+ constants.reject { |cmd|
+ cmd == :Command
+ }.map { |cmd|
+ cmd.to_s.downcase
+ }
+ end
+
class Command
def initialize args
@args = parse_args args
end
+ def execute
+ @args.each do |arg|
+ flag = get_flag arg[0]
+ instance_eval &flag[:action]
+ end
+ end
+
protected
- def self.flag name, options = {}
+ def self.flag name, options = {}, &action
+ @flags ||= {}
+ @flags[name] = {
+ options: options,
+ action: action
+ }
+ end
+
+ def self.flags
+ @flags
+ end
+
+ def help
+ "Yes!"
end
private
+ def get_flag name
+ name_sym = name.intern
+ if flag = self.class.flags[name_sym]
+ flag
+ else
+ flag = self.class.flags.find { |flag, conf|
+ aliases = conf[:options][:alias]
+ if aliases.respond_to? :include?
+ aliases.include? name_sym
+ else
+ aliases == name_sym
+ end
+ }
+ flag[1] if flag
+ end
+ end
+
def parse_args args
flags = []
args_enum = args.to_enum
View
4 lib/wheelbarrow/commands/setup.rb
@@ -1,8 +1,8 @@
module Wheelbarrow
module Commands
class Setup < Command
- flag :app_dir, short: :a, description: "Path to the directory where the app should be deployed"
- flag :repo, short: :r, description: "Path to the Git repository where the Git hook should be installed"
+ flag :app_dir, alias: :a, description: "Path to the directory where the app should be deployed"
+ flag :repo, alias: :r, description: "Path to the Git repository where the Git hook should be installed"
end
end
end
View
9 lib/wheelbarrow/commands/test.rb
@@ -0,0 +1,9 @@
+module Wheelbarrow
+ module Commands
+ class Test < Command
+ flag :h, description: "HEEEEEELP" do
+ puts help
+ end
+ end
+ end
+end

0 comments on commit c6668b9

Please sign in to comment.
Something went wrong with that request. Please try again.