class SubCmdOptParser
Constants
- VERSION
Attributes
program_name[RW]
release[RW]
set_program_name[RW]
set_summary_indent[RW]
set_summary_width[RW]
summary[RW]
summary is shows in help message
summary_indent[RW]
summary_width[RW]
version[RW]
Public Class Methods
new(*args) { |self| ... }
click to toggle source
@overload initialize(width = 32, indent = ' ' * 4, opts = {})
@param [Fixnum] width Width of summary @param [String] indent Indent of summary @param [Hash] opts Options hash @option opts [boolean] :help_command If the value is false then command "help" is not set automatically. Default is true @option opts [boolean] :version_command If the value is false then command "version" is not set automatically. Default is true @option opts [boolean] :accept_undefined_command If the value is false then show help for undefined commands. Default is false @option opts [boolean] :parse_only Commands (help and version) do not exit with printing messages and just parse options @yield [sc] @yieldparam [SubCmdOptParser] sc Option parser
# File lib/subcommand_optparse.rb, line 92 def initialize(*args, &block) opts = args.extract_options! @summary_width = args[0] || 32 @summary_indent = args[1] || ' ' * 4 @global_option_setting = nil @subcommand = [] @help_subcommand_use_p = (!opts.has_key?(:help_command) || opts[:help_command]) @summary = nil @version_subcommand_use_p = (!opts.has_key?(:version_command) || opts[:version_command]) @accept_undefined_command = opts[:accept_undefined_command] @parse_only = opts[:parse_only] if block_given? yield(self) end end
Public Instance Methods
global_option(&block)
click to toggle source
Set options that are available for all subcommands @yield [opt] @yieldparam [OptionParserForSubCmd] opt Option parser for all subcommands
# File lib/subcommand_optparse.rb, line 111 def global_option(&block) @global_option_setting = block end
parse!(argv = ARGV)
click to toggle source
# File lib/subcommand_optparse.rb, line 229 def parse!(argv = ARGV) define_prepared_command subcmd = argv[0] if subcmd_data = get_subcmd_data(subcmd) argv.shift else subcmd = nil unless @accept_undefined_command subcmd = "help" unless subcmd_data = get_subcmd_data(subcmd) raise "Unknown command #{subcmd.inspect}" end end end opt = get_option_parser(subcmd, subcmd_data) if exec_prepared_command(opt, argv) exit(0) end opt.parse!(argv) subcmd end
subcommand(name, *args, &block)
click to toggle source
@overload subcommand(name, description = nil, opts = {})
@param [String] name Name of subcommand @param [String] description Description of subcommand whose first line is shows in list of subcommands @param [Hash] opts Options hash @option opts [boolean] :load_global_options If the value is false then global options are not loaded @yield [opt] @yieldparam [OptionParserForSubCmd] opt Option parser for the subcommand
# File lib/subcommand_optparse.rb, line 126 def subcommand(name, *args, &block) if subcommand_defined?(name) raise ArgumentError, "Command '#{name}' has been already defined" end opts = args.extract_options! description = args.shift if args.size > 0 raise ArgumentError, "Too many arguments" end h = { :description => description, :setting => block } h[:load_global_options] = !(opts.has_key?(:load_global_options) && !opts[:load_global_options]) @subcommand << [name, h] end
subcommand_clear(name)
click to toggle source
# File lib/subcommand_optparse.rb, line 140 def subcommand_clear(name) @subcommand.delete_if do |subcmd, data| subcmd == name end end
subcommand_defined?(subcmd)
click to toggle source
# File lib/subcommand_optparse.rb, line 115 def subcommand_defined?(subcmd) !!@subcommand.assoc(subcmd) end
Private Instance Methods
define_prepared_command()
click to toggle source
# File lib/subcommand_optparse.rb, line 191 def define_prepared_command if @help_subcommand_use_p unless subcommand_defined?("help") subcommand("help", "Show help message", :load_global_options => false) do |opt| opt.banner = get_banner_help(opt) end end end if @version_subcommand_use_p unless subcommand_defined?("version") subcommand("version", "Show version", :load_global_options => false) end end end
exec_prepared_command(opt, argv)
click to toggle source
# File lib/subcommand_optparse.rb, line 207 def exec_prepared_command(opt, argv) unless @parse_only case opt.subcommand_name when "help" if !argv.empty? if !subcommand_defined?(argv[0]) puts "Unknown command: #{argv[0].inspect}" else opt = get_option_parser(argv[0], get_subcmd_data(argv[0])) end end print opt.to_s return true when "version" puts opt.ver || "Unknown version" return true end end nil end
get_option_parser(subcmd, subcmd_data)
click to toggle source
# File lib/subcommand_optparse.rb, line 155 def get_option_parser(subcmd, subcmd_data) desc = subcmd_data && subcmd_data[:description] opt = OptionParserForSubCmd.new(subcmd, desc, @summary_width, @summary_indent) opt.program_name = program_name if @program_name opt.version = version if @version opt.release = release if @release subcmd_data[:setting].call(opt) if subcmd_data && subcmd_data[:setting] if @global_option_setting && (!subcmd_data || subcmd_data[:load_global_options]) @global_option_setting.call(opt) end opt end
get_subcmd_data(subcmd)
click to toggle source
# File lib/subcommand_optparse.rb, line 146 def get_subcmd_data(subcmd) subcmd_data = nil if ary = @subcommand.assoc(subcmd) subcmd_data = ary[1] end subcmd_data end
message_list_subcommands()
click to toggle source
# File lib/subcommand_optparse.rb, line 169 def message_list_subcommands mes = "Commands:\n" max_size_subcmd = (@subcommand.map { |name, val| name.size }).max str_size = (max_size_subcmd.even? ? max_size_subcmd : max_size_subcmd + 1) + 4 @subcommand.each do |name, val| desc = ((val && val[:description]) ? val[:description].each_line.first.strip : "") mes << (" %-#{str_size}s" % name) << desc << "\n" end mes end