Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

displays global options for default help

  • Loading branch information...
commit 24dd618f3450ea15ca03af1c9efd33073d412ca1 1 parent 485ee68
@cldwalker authored
View
9 lib/boson/bare_runner.rb
@@ -59,11 +59,14 @@ def allowed_argument_error?(err, cmd, args)
(cmd_obj = Command.find(cmd)) && cmd_obj.arg_size != args.size)
end
+ def option_parser
+ @option_parser ||= OptionParser.new(self::GLOBAL_OPTIONS)
+ end
+
private
def parse_args(args)
- @option_parser = OptionParser.new(self::GLOBAL_OPTIONS)
- options = @option_parser.parse(args.dup, :opts_before_args=>true)
- new_args = @option_parser.non_opts
+ options = option_parser.parse(args.dup, :opts_before_args=>true)
+ new_args = option_parser.non_opts
[new_args[0], options, new_args[1..-1]]
end
View
5 lib/boson/runner.rb
@@ -47,8 +47,9 @@ def self.display_command_help(cmd)
def self.display_help
commands = Boson.commands.sort_by(&:name).map {|c| [c.name, c.desc.to_s] }
- puts "Usage: #{app_name} COMMAND [ARGS]", "", "Available commands:",
- Util.format_table(commands)
+ puts "Usage: #{app_name} [OPTIONS] COMMAND [ARGS]", "", "Available commands:",
+ Util.format_table(commands), "", "Options:"
+ option_parser.print_usage_table(no_headers: true)
end
def self.app_name
View
27 test/runner_test.rb
@@ -7,15 +7,10 @@
Boson.send(:remove_const, :DefaultCommandsRunner)
require 'boson/runner'
-class MyRunner < Boson::Runner
- GLOBAL_OPTIONS[:version] = {
- type: :boolean, :desc => 'Print version'
- }
-
- def self.execute(command, args, options)
- options[:version] ? puts("Version 1000.0") : super
- end
+# remove side effects from other tests
+Boson::Runner::GLOBAL_OPTIONS.delete_if {|k,v| k != :help }
+class MyRunner < Boson::Runner
desc "This is a small"
def small(*args)
p args
@@ -59,6 +54,10 @@ def no_run
end
class ExtendedRunner < Boson::Runner
+ def self.execute(command, args, options)
+ options[:version] ? puts("Version 1000.0") : super
+ end
+
def self.display_command_help(cmd)
super
puts "And don't forget to eat BAACCCONN"
@@ -84,7 +83,7 @@ def extended_command(cmd='')
def default_usage
<<-STR
-Usage: my_command COMMAND [ARGS]
+Usage: my_command [OPTIONS] COMMAND [ARGS]
Available commands:
boom
@@ -96,6 +95,9 @@ def default_usage
small This is a small
splot This is splot
test
+
+Options:
+ -h, --help Displays this help message
STR
end
@@ -198,7 +200,12 @@ def default_usage
end
it "executes custom global option" do
- my_command('-v').chomp.should == 'Version 1000.0'
+ # setup goes here to avoid coupling to other runner
+ ExtendedRunner::GLOBAL_OPTIONS[:version] = {
+ type: :boolean, :desc => 'Print version'
+ }
+
+ extended_command('-v').chomp.should == 'Version 1000.0'
end
it "allows Kernel-method command names" do
Please sign in to comment.
Something went wrong with that request. Please try again.