Class: TreasureData::Command::List::CommandOption

Inherits:
Object
  • Object
show all
Defined in:
lib/td/command/list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, args, description, examples = [], req_conn = true) ⇒ CommandOption

Returns a new instance of CommandOption.



56
57
58
59
60
61
62
63
# File 'lib/td/command/list.rb', line 56

def initialize(name, args, description, examples = [], req_conn = true)
  @name = name
  @args = args
  @description = description.to_s
  @examples = examples
  @req_conn = req_conn
  @override_message = nil
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



65
66
67
# File 'lib/td/command/list.rb', line 65

def args
  @args
end

#descriptionObject (readonly)

Returns the value of attribute description.



65
66
67
# File 'lib/td/command/list.rb', line 65

def description
  @description
end

#examplesObject (readonly)

Returns the value of attribute examples.



65
66
67
# File 'lib/td/command/list.rb', line 65

def examples
  @examples
end

#nameObject (readonly)

Returns the value of attribute name.



65
66
67
# File 'lib/td/command/list.rb', line 65

def name
  @name
end

#override_messageObject

Returns the value of attribute override_message.



66
67
68
# File 'lib/td/command/list.rb', line 66

def override_message
  @override_message
end

#req_connObject (readonly)

Returns the value of attribute req_conn.



65
66
67
# File 'lib/td/command/list.rb', line 65

def req_conn
  @req_conn
end

Instance Method Details

#compile!Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/td/command/list.rb', line 68

def compile!
  return if @usage_args

  args = @args.dup
  if args.last.to_s =~ /_$/
    @varlen = true
    args.push args.pop.to_s[0..-2]+'...'
  elsif args.last.to_s =~ /_\?$/
    @varlen = true
    args.push args.pop.to_s[0..-3]+'...?'
  end

  @req_args, @opt_args = args.partition {|a| a.to_s !~ /\?$/ }
  @opt_args = @opt_args.map {|a| a.to_s[0..-2].to_sym }

  @usage_args = "#{@name}"
  @req_args.each {|a| @usage_args << " <#{a}>" }
  @opt_args.each {|a| @usage_args << " [#{a}]" }
end

#create_optparse(argv) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/td/command/list.rb', line 88

def create_optparse(argv)
  compile!
  op = CommandParser.new(@name, @req_args, @opt_args, @varlen, argv, @req_conn)

  message = "usage:\n"
  message << "  $ #{File.basename($0)} #{@usage_args}\n"
  unless @examples.empty?
    message << "\n"
    message << "example:\n"
    @examples.each {|l|
      message << "  $ #{File.basename($0)} #{l}\n"
    }
  end
  message << "\n"
  message << "description:\n"
  @description.split("\n").each {|l|
    message << "  #{l}\n"
  }

  op.message = message
  op.summary_indent = "  "

  if msg = @override_message
    (class << op;self;end).module_eval do
      define_method(:to_s) { msg }
    end
  end

  op
end

#groupObject



132
133
134
# File 'lib/td/command/list.rb', line 132

def group
  @name.split(':', 2).first
end

#usageObject



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/td/command/list.rb', line 119

def usage
  compile!
  description_lines = @description.split("\n")
  strout = "%-40s   # %s" % [@usage_args, description_lines[0]]
  if description_lines.length > 1
    description_lines[1..-1].each {|lines|
      strout += "\n"
      strout += "%-40s     # %s" % ["", lines]
    }
  end
  strout
end