Module: RubyCLI
- Defined in:
- lib/ruby_cli.rb
Overview
The goal of this library is to factor out code needed to create Ruby command line applications which follow the Unix Philosophy design method (www.faqs.org/docs/artu/ch01s06.html).
See README.rdoc for more information.
Instance Method Summary collapse
-
#arguments_valid? ⇒ Boolean
Check if the required number of arguments remains in the argv array after it has been processed by the option parser.
-
#command ⇒ Object
Application logic.
-
#define_command_option_parsing ⇒ Object
Redefine this method if you have command specific options to tell the OptionParser object how to parse and handle your options Introduced in versio 0.2.0 to reduce LOC in CLI application.
-
#initialize(default_argv, command_name, usage = "[OPTIONS]... [ARGUMENTS]...") ⇒ Object
Initialization of this application requires the command line arguments.
-
#initialize_command_arguments ⇒ Object
This method can be overwritten if you want to set defaults for your command specific arguments.
-
#initialize_command_options ⇒ Object
This method can be overwritten if you want to set defaults for your command specific options.
- #output_help(exit_code) ⇒ Object
- #output_options_and_arguments ⇒ Object
-
#parse_options? ⇒ Boolean
Parse the options.
-
#process_arguments ⇒ Object
Redefine if you need to process arguments.
-
#process_options ⇒ Object
Performs post-parse processing on options For instance, some options may be mutually exclusive Redefine if you need to process options.
-
#run ⇒ Object
Run the application.
Instance Method Details
#arguments_valid? ⇒ Boolean
Check if the required number of arguments remains in the argv array after it has been processed by the option parser
70 71 72 73 |
# File 'lib/ruby_cli.rb', line 70 def arguments_valid?() return true if @arguments.size == 0 return @default_argv.size == @arguments.size end |
#command ⇒ Object
Application logic
95 96 97 98 |
# File 'lib/ruby_cli.rb', line 95 def command raise "This method should be overwritten." return 0 end |
#define_command_option_parsing ⇒ Object
Redefine this method if you have command specific options to tell the OptionParser object how to parse and handle your options Introduced in versio 0.2.0 to reduce LOC in CLI application
65 |
# File 'lib/ruby_cli.rb', line 65 def define_command_option_parsing() end |
#initialize(default_argv, command_name, usage = "[OPTIONS]... [ARGUMENTS]...") ⇒ Object
Initialization of this application requires the command line arguments.
11 12 13 14 15 16 17 18 19 |
# File 'lib/ruby_cli.rb', line 11 def initialize(default_argv, command_name, usage = "[OPTIONS]... [ARGUMENTS]...") @default_argv = default_argv @default_options = {:help => false, :verbose => false} initialize_command_arguments @opt_parser = nil @command_name = command_name @usage = usage end |
#initialize_command_arguments ⇒ Object
This method can be overwritten if you want to set defaults for your command specific arguments.
27 |
# File 'lib/ruby_cli.rb', line 27 def initialize_command_arguments() @arguments = {} end |
#initialize_command_options ⇒ Object
This method can be overwritten if you want to set defaults for your command specific options.
23 |
# File 'lib/ruby_cli.rb', line 23 def () @options = {} end |
#output_help(exit_code) ⇒ Object
100 101 102 103 104 |
# File 'lib/ruby_cli.rb', line 100 def output_help(exit_code) puts @opt_parser puts exit_code if @default_options[:verbose] return exit_code end |
#output_options_and_arguments ⇒ Object
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/ruby_cli.rb', line 75 def puts "OPTIONS:" @default_options.each {|name, value| puts "#{name} = #{value}"} @options.each {|name, value| puts "#{name} = #{value}"} puts "No options" if @options.length == 0 and @default_options == 0 puts "ARGUMENTS:" @arguments.each {|name,value| puts "#{name} = #{value}"} puts "No arguments" if @arguments.length == 0 end |
#parse_options? ⇒ Boolean
Parse the options
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/ruby_cli.rb', line 42 def @opt_parser = OptionParser.new do |opts| opts. = "Usage: #{@command_name} #{@usage}" opts.separator "" opts.separator "Specific options:" opts.on('-h', '--help', 'displays help information') do @default_options[:help] = true exit output_help(0) end opts.on('-V','--verbose','Run verbosely') do @default_options[:verbose] = true end # If you redefine, you can copy this method and add command specific options here! end define_command_option_parsing @opt_parser.parse!(@default_argv) rescue return false return true end |
#process_arguments ⇒ Object
Redefine if you need to process arguments.
92 |
# File 'lib/ruby_cli.rb', line 92 def process_arguments() return true end |
#process_options ⇒ Object
Performs post-parse processing on options For instance, some options may be mutually exclusive Redefine if you need to process options.
89 |
# File 'lib/ruby_cli.rb', line 89 def () return true end |
#run ⇒ Object
Run the application
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/ruby_cli.rb', line 30 def run if && arguments_valid? process_arguments if @default_options[:verbose] command else output_help(1) end end |