lib/methadone/main.rb in methadone-1.0.0.rc4 vs lib/methadone/main.rb in methadone-1.0.0.rc5
- old
+ new
@@ -41,20 +41,29 @@
# on("--[no]-switch")
# on("--flag VALUE")
#
# arg :needed
# arg :maybe, :optional
- #
+ #
+ # defaults_from_env_var SOME_VAR
+ # defaults_from_config_file '.my_app.rc'
+ #
# go!
# end
#
# Our app then acts as follows:
#
# $ our_app
# # => parse error: 'needed' is required
# $ our_app foo
# # => succeeds; "maybe" in main is nil
+ # $ our_app --flag foo
+ # # => options[:flag] has the value "foo"
+ # $ SOME_VAR='--flag foo' our_app
+ # # => options[:flag] has the value "foo"
+ # $ SOME_VAR='--flag foo' our_app --flag bar
+ # # => options[:flag] has the value "bar"
#
# Note that we've done all of this inside a class that we called +App+. This isn't strictly
# necessary, and you can just +include+ Methadone::Main and Methadone::CLILogging at the root
# of your +bin+ file if you like. This is somewhat unsafe, because +self+ inside the +bin+
# file is Object, and any methods you create (or cause to be created via +include+) will be
@@ -128,26 +137,10 @@
# filename:: name of the file, relative to the user's home directory
def defaults_from_config_file(filename,options={})
@rc_file = File.join(ENV['HOME'],filename)
end
- def add_defaults_to_docs
- if @env_var && @rc_file
- opts.separator ''
- opts.separator 'Default values can be placed in:'
- opts.separator ''
- opts.separator " #{@env_var} environment variable, as a String of options"
- opts.separator " #{@rc_file} with contents either a String of options or a YAML-encoded Hash"
- elsif @env_var
- opts.separator ''
- opts.separator "Default values can be placed in the #{@env_var} environment variable"
- elsif @rc_file
- opts.separator ''
- opts.separator "Default values can be placed in #{@rc_file}"
- end
- end
-
# Start your command-line app, exiting appropriately when
# complete.
#
# This *will* exit your program when it completes. If your
# #main block evaluates to an integer, that value will be sent
@@ -156,21 +149,13 @@
# If the command-line options couldn't be parsed, this
# will exit with 64 and whatever message OptionParser provided.
#
# If a required argument (see #arg) is not found, this exits with
# 64 and a message about that missing argument.
- #
def go!
- add_defaults_to_docs
- set_defaults_from_rc_file
- normalize_defaults
+ setup_defaults
opts.post_setup
- if @env_var
- String(ENV[@env_var]).split(/\s+/).each do |arg|
- ::ARGV.unshift(arg)
- end
- end
opts.parse!
opts.check_args!
result = call_main
if result.kind_of? Fixnum
exit result
@@ -279,9 +264,42 @@
exit 0
end
end
private
+
+ def setup_defaults
+ add_defaults_to_docs
+ set_defaults_from_rc_file
+ normalize_defaults
+ set_defaults_from_env_var
+ end
+
+ def add_defaults_to_docs
+ if @env_var && @rc_file
+ opts.separator ''
+ opts.separator 'Default values can be placed in:'
+ opts.separator ''
+ opts.separator " #{@env_var} environment variable, as a String of options"
+ opts.separator " #{@rc_file} with contents either a String of options "
+ spaces = (0..@rc_file.length).reduce('') { |a,_| a << ' ' }
+ opts.separator " #{spaces}or a YAML-encoded Hash"
+ elsif @env_var
+ opts.separator ''
+ opts.separator "Default values can be placed in the #{@env_var} environment variable"
+ elsif @rc_file
+ opts.separator ''
+ opts.separator "Default values can be placed in #{@rc_file}"
+ end
+ end
+
+ def set_defaults_from_env_var
+ if @env_var
+ String(ENV[@env_var]).split(/\s+/).each do |arg|
+ ::ARGV.unshift(arg)
+ end
+ end
+ end
def set_defaults_from_rc_file
if @rc_file && File.exists?(@rc_file)
File.open(@rc_file) do |file|
parsed = YAML::load(file)