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.1"
Instance Method Summary collapse
-
#global_option {|opt| ... } ⇒ Object
Set options that are available for all subcommands.
-
#initialize(banner = nil, width = 32, indent = ' '*4, opts = {}) {|sc| ... } ⇒ SubCmdOptParser
constructor
A new instance of SubCmdOptParser.
- #parse!(argv = ARGV) ⇒ Object
- #subcommand(name, banner = nil, opts = {}) {|opt| ... } ⇒ Object
Constructor Details
#initialize(banner = nil, width = 32, indent = ' '*4, opts = {}) {|sc| ... } ⇒ SubCmdOptParser
Returns a new instance of SubCmdOptParser.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/subcommand_optparse.rb', line 26 def initialize(*args, &block) opts = args. @default_banner = args.shift @args_option_parser = args @global_option_setting = nil @subcommand = [] @help_subcommand_use_p = (!opts.has_key?(:help_command) || opts[:help_command]) if block_given? yield(self) end end |
Instance Method Details
#global_option {|opt| ... } ⇒ Object
Set options that are available for all subcommands
41 42 43 |
# File 'lib/subcommand_optparse.rb', line 41 def global_option(&block) @global_option_setting = block end |
#parse!(argv = ARGV) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/subcommand_optparse.rb', line 95 def parse!(argv = ARGV) if @help_subcommand_use_p subcommand("help", :load_global_options => false) end subcmd = argv[0] if subcmd_data = get_subcmd_data(subcmd) argv.shift else subcmd = nil end opt = get_option_parser(subcmd, subcmd_data) opt.parse!(argv) if @help_subcommand_use_p && subcmd == "help" print opt.to_s + "\n" puts (opt) exit end subcmd end |
#subcommand(name, banner = nil, opts = {}) {|opt| ... } ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/subcommand_optparse.rb', line 51 def subcommand(name, *args, &block) opts = args. = args.shift h = { :banner => , :setting => block } h[:load_global_options] = !(opts.has_key?(:load_global_options) && !opts[:load_global_options]) @subcommand << [name, h] end |