Class: OptionGrouper::Group

Inherits:
Object
  • Object
show all
Includes:
Blockenspiel::DSL
Defined in:
lib/optiongrouper.rb

Overview

A Group contains some parameters

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Group

Initialize a new group

Parameters:

  • name (Symbol)

    name of the group



15
16
17
18
19
# File 'lib/optiongrouper.rb', line 15

def initialize name
  @name = name
  @long = name.to_s.gsub(/_/, '-')
  @opts = {}
end

Instance Method Details

#headerString #header(str) ⇒ String

Overloads:

  • #headerString

    Gets current header message.

    Returns:

    • (String)

      header message

  • #header(str) ⇒ String

    Sets a new header message

    Parameters:

    • str (String)

      new header message

    Returns:

    • (String)

      header message



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/optiongrouper.rb', line 76

def header *args
  if args.size == 0
    if @header
      "#{@header} (#{@long}):"
    else
      "#{@long}:"
    end
  else
    @header = args[0]
  end
end

#invoke(&blk) ⇒ Object

Invoke Group in a block to configure



22
23
24
# File 'lib/optiongrouper.rb', line 22

def invoke &blk
  Blockenspiel.invoke blk, self
end

#longString #long(str) ⇒ String

Defines the name used in command line. By default it's the normal name with underscores replaced with hyphens. It's not advised to change it.

Overloads:

  • #longString

    Gets current command line name.

    Returns:

    • (String)

      current name

  • #long(str) ⇒ String

    Sets current command line name.

    Returns:

    • (String)

      the newly set name



61
62
63
64
65
66
67
# File 'lib/optiongrouper.rb', line 61

def long *args
  if args.size == 0
    @long
  else
    @long = args[0]
  end
end

#opt(name, desc, opts = {}) ⇒ void

This method returns an undefined value.

Define a new option

Parameters:

  • name (Symbol)

    name of the parameter. Underscores will be converted to hypens in long argument, and a short one will be generated unless you define one or you declare not to generate one.

  • desc (#to_s)

    description of the argument.

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

    additional options.

Options Hash (opts):

  • :long (String)

    use this as a long option.

  • :short (String)

    use this as a short option (must be 1 character long to work)

  • :no_short (Boolean)

    do not automatically generate a short option if set to true

  • :on (Proc)

    invoke block after parsing argument

  • :value (Symbol, Array<Symbol>, Array<Array<Symbol, String>>)

    describe additional values to the argument. Symbols declare the type of the value, and also the string displayed on help page using [[Symbol, String], ...] syntax.

  • :default (Object) — default: nil

    default value of the option if not specified on command line.

  • :set (Object) — default: true

    set valueless commands to this if specified.



46
47
48
49
50
51
# File 'lib/optiongrouper.rb', line 46

def opt name, desc, opts = {}
  opts[:desc] = desc
  opts[:long] ||= name.to_s.gsub(/_/, '-')
  opts[:set] ||= true
  @opts[name] = opts
end