Class: SubCmdOptParser
- Inherits:
-
Object
- Object
- SubCmdOptParser
- Defined in:
- lib/subcommand_optparse.rb,
lib/subcommand_optparse/version.rb
Defined Under Namespace
Classes: OptionParserForSubCmd
Constant Summary collapse
- VERSION =
"0.0.3"
Instance Attribute Summary collapse
-
#program_name ⇒ Object
Returns the value of attribute program_name.
-
#release ⇒ Object
Returns the value of attribute release.
-
#summary ⇒ Object
summary is shows in help message.
-
#summary_indent ⇒ Object
Returns the value of attribute summary_indent.
-
#summary_width ⇒ Object
Returns the value of attribute summary_width.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
-
#global_option {|opt| ... } ⇒ Object
Set options that are available for all subcommands.
-
#initialize(width = 32, indent = ' '*4, opts = {}) {|sc| ... } ⇒ SubCmdOptParser
constructor
A new instance of SubCmdOptParser.
- #parse!(argv = ARGV) ⇒ Object
- #subcommand(name, description = nil, opts = {}) {|opt| ... } ⇒ Object
- #subcommand_clear(name) ⇒ Object
- #subcommand_defined?(subcmd) ⇒ Boolean
Constructor Details
#initialize(width = 32, indent = ' '*4, opts = {}) {|sc| ... } ⇒ SubCmdOptParser
Returns a new instance of SubCmdOptParser.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/subcommand_optparse.rb', line 92 def initialize(*args, &block) opts = args. @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 |
Instance Attribute Details
#program_name ⇒ Object
Returns the value of attribute program_name.
69 70 71 |
# File 'lib/subcommand_optparse.rb', line 69 def program_name @program_name end |
#release ⇒ Object
Returns the value of attribute release.
73 74 75 |
# File 'lib/subcommand_optparse.rb', line 73 def release @release end |
#summary ⇒ Object
summary is shows in help message
68 69 70 |
# File 'lib/subcommand_optparse.rb', line 68 def summary @summary end |
#summary_indent ⇒ Object
Returns the value of attribute summary_indent.
71 72 73 |
# File 'lib/subcommand_optparse.rb', line 71 def summary_indent @summary_indent end |
#summary_width ⇒ Object
Returns the value of attribute summary_width.
70 71 72 |
# File 'lib/subcommand_optparse.rb', line 70 def summary_width @summary_width end |
#version ⇒ Object
Returns the value of attribute version.
72 73 74 |
# File 'lib/subcommand_optparse.rb', line 72 def version @version end |
Instance Method Details
#global_option {|opt| ... } ⇒ Object
Set options that are available for all subcommands
111 112 113 |
# File 'lib/subcommand_optparse.rb', line 111 def global_option(&block) @global_option_setting = block end |
#parse!(argv = ARGV) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# 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, description = nil, opts = {}) {|opt| ... } ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 |
# 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. 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) ⇒ Object
140 141 142 143 144 |
# File 'lib/subcommand_optparse.rb', line 140 def subcommand_clear(name) @subcommand.delete_if do |subcmd, data| subcmd == name end end |
#subcommand_defined?(subcmd) ⇒ Boolean
115 116 117 |
# File 'lib/subcommand_optparse.rb', line 115 def subcommand_defined?(subcmd) !!@subcommand.assoc(subcmd) end |