Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixed bugs in scaffold and error callback.

  • Loading branch information...
commit df586b72de6ef396840174fe394b0ae244921981 1 parent 7827b1a
@davetron5000 davetron5000 authored
Showing with 45 additions and 14 deletions.
  1. +5 −5 bin/gli
  2. +1 −1  gli.gemspec
  3. +4 −5 lib/gli.rb
  4. +35 −3 lib/support/scaffold.rb
View
10 bin/gli
@@ -14,7 +14,7 @@ switch :version
desc 'Dry run; don\'t change the disk'
switch :n
-desc 'Root dir in which to create project'
+desc 'Root dir of project'
default_value '.'
flag [:r,:root]
@@ -53,9 +53,9 @@ post do |global,command,options,args|
puts "Executed #{command.name}" if global[:v]
end
-on_error do |global,command,options,args|
- puts "Got an error" if global[:v]
- true
-end
+#on_error do |global,command,options,args|
+# puts "Got an error" if global[:v]
+# true
+#end
run(ARGV)
View
2  gli.gemspec
@@ -1,6 +1,6 @@
spec = Gem::Specification.new do |s|
s.name = 'gli'
- s.version = '0.1.4'
+ s.version = '0.1.5'
s.author = 'David Copeland'
s.email = 'davidcopeland@naildrivin5.com'
s.homepage = 'http://davetron5000.github.com/gli'
View
9 lib/gli.rb
@@ -10,7 +10,7 @@
module GLI
extend self
- VERSION = '0.1.4'
+ VERSION = '0.1.5'
@@program_name = $0.split(/\//)[-1]
@@post_block = nil
@@ -72,7 +72,8 @@ def post(&a_proc)
end
# Define a block to run if an error occurs.
- # The block will receive the global-options,command,options, and arguments
+ # The block will receive the exception that was caught.
+ # It should return false to avoid the built-in error handling
def on_error(&a_proc)
@@error_block = a_proc
end
@@ -91,9 +92,7 @@ def run(args)
end
rescue UnknownCommandException, UnknownArgumentException, MissingArgumentException => ex
regular_error_handling = true
- if @@error_block
- regular_error_handling = @@error_block.call(global_options,command,options,arguments,ex)
- end
+ regular_error_handling = @@error_block.call(ex) if @@error_block
if regular_error_handling
puts "error: #{ex}"
View
38 lib/support/scaffold.rb
@@ -20,10 +20,12 @@ def self.mk_binfile(root_dir,create_ext_dir,force,dry_run,project_name,commands)
if !File.exist?(bin_file) || force
if !dry_run
File.open(bin_file,'w') do |file|
+ file.chmod(0755)
file.puts '#!/usr/bin/ruby'
file.puts '$: << File.expand_path(File.dirname(__FILE__) + \'/../lib\')'
file.puts '$: << File.expand_path(File.dirname(__FILE__) + \'/../ext\')' if create_ext_dir
file.puts <<EOS
+require 'rubygems'
require 'gli'
include GLI
@@ -36,8 +38,10 @@ def self.mk_binfile(root_dir,create_ext_dir,force,dry_run,project_name,commands)
arg_name 'The name of the argument'
flag [:f,:flagname]
EOS
+ first = true
commands.each do |command|
- file.puts <<EOS
+ if first
+ file.puts <<EOS
desc 'Describe #{command} here'
arg_name 'Describe arguments to #{command} here'
@@ -48,14 +52,42 @@ def self.mk_binfile(root_dir,create_ext_dir,force,dry_run,project_name,commands)
c.desc 'Describe a flag to #{command}'
c.default_value 'default'
c.flag :s
-
c.action do |global_options,options,args|
# Your command logic here
end
end
EOS
+ else
+ file.puts <<EOS
+command :#{command} do |c|
+ c.action do |global_options,options,args|
+ end
+end
+EOS
+ end
+ first = false
end
- puts "Create #{bin_file}"
+ file.puts <<EOS
+pre do |global,command,options,args|
+ # Pre logic here
+ # Return true to proceed; false to abourt and not call the
+ # chosen command
+ true
+end
+
+post do |global,command,options,args|
+ # Post logic here
+end
+
+on_error do |exception|
+ # Error logic here
+ # return false to skip default error handling
+ true
+end
+
+GLI.run(ARGV)
+EOS
+ puts "Created #{bin_file}"
end
end
else
Please sign in to comment.
Something went wrong with that request. Please try again.