lib/methadone/main.rb in methadone-0.4.1 vs lib/methadone/main.rb in methadone-0.5.1
- old
+ new
@@ -193,11 +193,27 @@
def description(desc)
opts.description(desc)
end
# Returns a Hash that you can use to store or retrieve options
- # parsed from the command line
+ # parsed from the command line. When you put values in here, if you do so
+ # *before* you've declared your command-line interface via #on, the value
+ # will be used in the docstring to indicate it is the default.
+ #
+ # Example
+ #
+ # main do
+ # puts options[:foo] # put the value of --foo that the user provided
+ # end
+ #
+ # options[:foo] = "bar" # set "bar" as the default value for --foo, which
+ # # will cause us to include "(default: bar)" in the
+ # # docstring
+ #
+ # on("--foo FOO","Sets the foo")
+ # go!
+ #
def options
@options
end
# Set the version of your app so it appears in the
@@ -289,10 +305,11 @@
# to the constructor will be used to store
# the parsed command-line value. See #opts in the Main module
# for how that works.
def on(*args,&block)
@accept_options = true
+ args = add_default_value_to_docstring(*args)
if block
@option_parser.on(*args,&block)
else
opt_names = option_names(*args)
@option_parser.on(*args) do |value|
@@ -342,9 +359,25 @@
@version = version
set_banner
end
private
+
+ def add_default_value_to_docstring(*args)
+ default_value = nil
+ option_names_from(args).each do |option|
+ default_value = (@options[option.to_s] || @options[option.to_sym]) if default_value.nil?
+ end
+ if default_value.nil?
+ args
+ else
+ args + ["(default: #{default_value})"]
+ end
+ end
+
+ def option_names_from(args)
+ args.select { |_| _ =~ /^\-/ }.map { |_| _.gsub(/^\-+/,'').gsub(/\s.*$/,'') }
+ end
def set_banner
unless @user_specified_banner
new_banner="Usage: #{::File.basename($0)}"
new_banner += " [options]" if @accept_options