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

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:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/subcommand_optparse.rb', line 51

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])
  @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.



28
29
30
# File 'lib/subcommand_optparse.rb', line 28

def program_name
  @program_name
end

#releaseObject

Returns the value of attribute release.



32
33
34
# File 'lib/subcommand_optparse.rb', line 32

def release
  @release
end

#summary_indentObject

Returns the value of attribute summary_indent.



30
31
32
# File 'lib/subcommand_optparse.rb', line 30

def summary_indent
  @summary_indent
end

#summary_widthObject

Returns the value of attribute summary_width.



29
30
31
# File 'lib/subcommand_optparse.rb', line 29

def summary_width
  @summary_width
end

#versionObject

Returns the value of attribute version.



31
32
33
# File 'lib/subcommand_optparse.rb', line 31

def version
  @version
end

Instance Method Details

#global_option {|opt| ... } ⇒ Object

Set options that are available for all subcommands

Yields:

  • (opt)

Yield Parameters:



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

def global_option(&block)
  @global_option_setting = block
end

#parse!(argv = ARGV) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/subcommand_optparse.rb', line 182

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:



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/subcommand_optparse.rb', line 84

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



98
99
100
101
102
# File 'lib/subcommand_optparse.rb', line 98

def subcommand_clear(name)
  @subcommand.delete_if do |subcmd, data|
    subcmd == name
  end
end

#subcommand_defined?(subcmd) ⇒ Boolean

Returns:

  • (Boolean)


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

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