Class: SubCmdOptParser

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(width = 32, indent = ' '*4, opts = {}) {|sc| ... } ⇒ SubCmdOptParser

Returns a new instance of SubCmdOptParser.

Parameters:

  • width (Fixnum) (defaults to: 32)

    Width of summary

  • indent (String) (defaults to: ' '*4)

    Indent of summary

  • opts (Hash) (defaults to: {})

    Options hash

Options Hash (opts):

  • :help_command (boolean)

    If the value is false then command “help” is not set automatically. Default is true

  • :version_command (boolean)

    If the value is false then command “version” is not set automatically. Default is true

  • :accept_undefined_command (boolean)

    If the value is false then show help for undefined commands. Default is false

  • :parse_only (boolean)

    Commands (help and version) do not exit with printing messages and just parse options

Yields:

  • (sc)

Yield Parameters:



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.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

Instance Attribute Details

#program_nameObject

Returns the value of attribute program_name.



69
70
71
# File 'lib/subcommand_optparse.rb', line 69

def program_name
  @program_name
end

#releaseObject

Returns the value of attribute release.



73
74
75
# File 'lib/subcommand_optparse.rb', line 73

def release
  @release
end

#summaryObject

summary is shows in help message



68
69
70
# File 'lib/subcommand_optparse.rb', line 68

def summary
  @summary
end

#summary_indentObject

Returns the value of attribute summary_indent.



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

def summary_indent
  @summary_indent
end

#summary_widthObject

Returns the value of attribute summary_width.



70
71
72
# File 'lib/subcommand_optparse.rb', line 70

def summary_width
  @summary_width
end

#versionObject

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

Yields:

  • (opt)

Yield Parameters:



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

Parameters:

  • name (String)

    Name of subcommand

  • description (String) (defaults to: nil)

    Description of subcommand whose first line is shows in list of subcommands

  • opts (Hash) (defaults to: {})

    Options hash

Options Hash (opts):

  • :load_global_options (boolean)

    If the value is false then global options are not loaded

Yields:

  • (opt)

Yield Parameters:



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.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) ⇒ 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

Returns:

  • (Boolean)


115
116
117
# File 'lib/subcommand_optparse.rb', line 115

def subcommand_defined?(subcmd)
  !!@subcommand.assoc(subcmd)
end