Class: Usage
- Inherits:
-
Object
- Object
- Usage
- Defined in:
- lib/Usage.rb
Overview
This is the main interface to this usage functionality. It operates by using method_missing to extract information about the command line arguments. Most all the functionality is implemented in the UsageMod::Base class with the Usage class being a wrapper around it to handle errors in a nice graceful fashion.
The main way to use it is to give the usage string as the argument to build the usage object like below:
usage = Usage.new “input_file output_file”
see the README for a more complete description of what can appear in that string.
Instance Method Summary collapse
- #ask_yes_no(str, default) ⇒ Object
-
#die(message) ⇒ Object
Allows the program to die in a graceful manner.
-
#die_long ⇒ Object
Allows the program to die in a graceful manner.
-
#initialize(*args, &block) ⇒ Usage
constructor
usage string as well.
-
#method_missing(symbol, *args, &block) ⇒ Object
forward all other calls to UsageMod::Base class.
-
#print_program_name ⇒ Object
Prints out the program’s name in a graceful manner.
-
#print_short_usage ⇒ Object
Print out the short usage string (without the options).
Constructor Details
#initialize(*args, &block) ⇒ Usage
usage string as well.
976 977 978 979 980 981 982 983 984 985 986 987 988 |
# File 'lib/Usage.rb', line 976 def initialize(*args, &block) @usageBase = UsageMod::Base.new(self, *args) begin @usageBase.parse_args(&block) rescue UsageMod::LongUsageRequested die_long rescue UsageMod::Error => usageError $TRACE.debug 1, usageError.backtrace.join("\n") die(usageError.) rescue raise end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args, &block) ⇒ Object
forward all other calls to UsageMod::Base class
1038 1039 1040 1041 |
# File 'lib/Usage.rb', line 1038 def method_missing(symbol, *args, &block) $TRACE.debug 5, "Usage method missing: #{symbol}" @usageBase.send(symbol, *args, &block) end |
Instance Method Details
#ask_yes_no(str, default) ⇒ Object
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 |
# File 'lib/Usage.rb', line 1043 def ask_yes_no(str, default) while true print str + " [" + (default == UsageMod::YES_RESPONSE ? "Yn" : "yN") + "]? " $stdout.flush s = $stdin.gets case s when /^$/ return default when /^y/i return UsageMod::YES_RESPONSE when /^n/i return UsageMod::NO_RESPONSE end end end |
#die(message) ⇒ Object
Allows the program to die in a graceful manner. It displays
ERROR: the error message
It then calls exit (so it doesn’t return)
997 998 999 1000 1001 1002 1003 |
# File 'lib/Usage.rb', line 997 def die() print_program_name print "ERROR: #{message}\n" print "\n" print_short_usage exit 1 end |
#die_long ⇒ Object
Allows the program to die in a graceful manner. It displays
ERROR: the error message
followed by the usage string. It then calls exit (so it doesn’t return)
1012 1013 1014 1015 1016 1017 |
# File 'lib/Usage.rb', line 1012 def die_long print_program_name print_short_usage print @usageBase.long_usage + "\n" exit 1 end |
#print_program_name ⇒ Object
Prints out the program’s name in a graceful manner.
PROGRAM: program name
1024 1025 1026 |
# File 'lib/Usage.rb', line 1024 def print_program_name print "PROGRAM: #{$0}\n" end |
#print_short_usage ⇒ Object
Print out the short usage string (without the options)
1031 1032 1033 |
# File 'lib/Usage.rb', line 1031 def print_short_usage print "USAGE: #{File.basename($0)} #{@usageBase.usage}\n" end |