Permalink
Browse files

Experimenting with building the commands api for wheelbarrow

  • Loading branch information...
1 parent 70d8757 commit 7514f34223916810b0dea222923a9deb04ff629e @rbgrouleff committed Oct 7, 2012
Showing with 54 additions and 0 deletions.
  1. +46 −0 lib/wheelbarrow/command.rb
  2. +8 −0 lib/wheelbarrow/commands/setup.rb
View
46 lib/wheelbarrow/command.rb
@@ -0,0 +1,46 @@
+module Wheelbarrow
+ def Command cmd_name, args
+ Commands.const_get(cmd_name.capitalize).new args
+ end
+
+ module Commands
+ class Command
+ def initialize args
+ @args = parse_args args
+ end
+
+ protected
+
+ def self.flag name, options = {}
+ end
+
+ private
+
+ def parse_args args
+ flags = []
+ args_enum = args.to_enum
+ arg = ''
+ loop do
+ begin
+ arg = args_enum.next
+ rescue StopIteration
+ break
+ end
+ if arg.include? "="
+ arg_name, arg_val = arg.split "="
+ flags << [strip_dashes(arg_name), arg_val]
+ elsif arg.start_with? '-'
+ flags << [strip_dashes(arg)]
+ else
+ flags.last << arg
+ end
+ end
+ flags
+ end
+
+ def strip_dashes str
+ str[/-*([^-]*)/,1]
+ end
+ end
+ end
+end
View
8 lib/wheelbarrow/commands/setup.rb
@@ -0,0 +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"
+ end
+ end
+end

0 comments on commit 7514f34

Please sign in to comment.