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

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

Returns:

  • (Boolean)


71
72
73
74
# File 'lib/ruby_cli.rb', line 71

def arguments_valid?() 
	return true if @arguments.size == 0
	return @default_argv.size == @arguments.size 
end

#commandObject

Application logic



96
97
98
99
# File 'lib/ruby_cli.rb', line 96

def command
	raise "This method should be overwritten." 
	return 0
end

#define_command_option_parsingObject

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



66
# File 'lib/ruby_cli.rb', line 66

def define_command_option_parsing() end

#initialize(default_argv, command_name, usage = "[OPTIONS]... [ARGUMENTS]...") ⇒ Object

Initialization of this application requires the command line arguments.



12
13
14
15
16
17
18
19
20
# File 'lib/ruby_cli.rb', line 12

def initialize(default_argv, command_name, usage = "[OPTIONS]... [ARGUMENTS]...")
	@default_argv = default_argv
	@default_options = {:help => false, :verbose => false}
	initialize_command_options
	initialize_command_arguments
	@opt_parser = nil
	@command_name = command_name
	@usage = usage
end

#initialize_command_argumentsObject

This method can be overwritten if you want to set defaults for your command specific arguments.



28
# File 'lib/ruby_cli.rb', line 28

def initialize_command_arguments() @arguments = {} end

#initialize_command_optionsObject

This method can be overwritten if you want to set defaults for your command specific options.



24
# File 'lib/ruby_cli.rb', line 24

def initialize_command_options() @options = {} end

#output_help(exit_code) ⇒ Object



101
102
103
104
105
# File 'lib/ruby_cli.rb', line 101

def output_help(exit_code) 
	puts @opt_parser
	puts exit_code if @default_options[:verbose]
	return exit_code 
end

#output_options_and_argumentsObject



76
77
78
79
80
81
82
83
84
85
# File 'lib/ruby_cli.rb', line 76

def output_options_and_arguments
  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

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ruby_cli.rb', line 43

def parse_options?
	@opt_parser = OptionParser.new do |opts|		
		opts.banner = "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_argumentsObject

Redefine if you need to process arguments.



93
# File 'lib/ruby_cli.rb', line 93

def process_arguments() return true end

#process_optionsObject

Performs post-parse processing on options For instance, some options may be mutually exclusive Redefine if you need to process options.



90
# File 'lib/ruby_cli.rb', line 90

def process_options() return true end

#runObject

Run the application



31
32
33
34
35
36
37
38
39
40
# File 'lib/ruby_cli.rb', line 31

def run
  if parse_options? && arguments_valid?
	process_options
    process_arguments
    output_options_and_arguments if @default_options[:verbose]
    command
  else
    output_help(1)
  end
end