Module: Launchr::Mixlib::CLI::ClassMethods

Defined in:
lib/launchr/mixin/mixlib_cli.rb

Instance Method Summary collapse

Instance Method Details

#argument(name, args) ⇒ Object

Add a command line argument.

Parameters

name<Symbol>

The name of the argument to add

args<Hash>

A hash of arguments for the argument, specifying how it should be parsed.

Returns

true

Always returns true.

Raises:

  • (ArgumentError)


78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/launchr/mixin/mixlib_cli.rb', line 78

def argument(name, args)
  @arguments ||= Launchr::OrderedHash.new
  @options_arguments ||= Launchr::OrderedHash.new
  raise(ArgumentError, "Argument name must be a symbol") unless name.kind_of?(Symbol)

  strip_arg(args,:short)
  strip_arg(args,:long)
  convert_argument_to_option(args)

  @arguments[name.to_sym] = args.dup
  @options_arguments[name.to_sym] = args
end

#argumentsObject

Get the hash of current arguments.

Returns

@arguments<Hash>

The current arguments hash.



111
112
113
114
# File 'lib/launchr/mixin/mixlib_cli.rb', line 111

def arguments
  @arguments ||= Launchr::OrderedHash.new
  @arguments
end

#arguments=(val) ⇒ Object

Set the current arguments hash

Parameters

val<Hash>

The hash to set the arguments to

Returns

@arguments<Hash>

The current arguments hash.

Raises:

  • (ArgumentError)


123
124
125
126
# File 'lib/launchr/mixin/mixlib_cli.rb', line 123

def arguments=(val)
  raise(ArgumentError, "Arguments must recieve a hash") unless val.kind_of?(Hash)
  @arguments = val
end

Change the banner. Defaults to:

Usage: #{0} (options)

Parameters

bstring<String>

The string to set the banner to

Returns

@banner<String>

The current banner



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/launchr/mixin/mixlib_cli.rb', line 166

def banner(bstring=nil)
  case bstring
  when true
    # @banner = "usage: #{File.basename $0} [options]"
    @banner = "Usage: #{File.basename $0} [options]"
  when false
    @banner = ""
  when String
    @banner = bstring
  else
    # @banner ||= "usage: #{File.basename $0} [options]"
    @banner ||= "Usage: #{File.basename $0} [options]"
    # @banner ||= ""
    @banner
  end
end

#convert_argument_to_option(args) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/launchr/mixin/mixlib_cli.rb', line 98

def convert_argument_to_option args
  # args = args.dup
  args[:short] = "-" + args[:short] if args[:short]
  args[:short_strip] = "-" + args[:short_strip] if args[:short_strip]
  args[:long] = "--" + args[:long]  if args[:long]
  args[:long_strip] = "--" + args[:long_strip]  if args[:long_strip]
  args
end

#filtered_argvObject

The remaining argv command line arguments, after parsing. Defaults to: [] (an empty array) if un-parsed

Returns

@filtered_argv<Array>

The remaining command line arguments, after CLI options parsing.



273
274
275
276
# File 'lib/launchr/mixin/mixlib_cli.rb', line 273

def filtered_argv
  @filtered_argv ||= []
  @filtered_argv
end

Add a line to the footer.

Parameters

fstring<String>

The next string to push onto the footer

Returns

@footer<Array>

The current footer, an array of strings



209
210
211
212
213
214
215
216
217
218
219
# File 'lib/launchr/mixin/mixlib_cli.rb', line 209

def footer(fstring=nil)
  @footer ||= []
  case fstring
  when Array
    @footer = fstring
  when String
    @footer << fstring
  when nil
    @footer
  end
end

#header(hstring = nil) ⇒ Object

Add a line to the header.

Parameters

hstring<String>

The next string to push onto the header

Returns

@header<Array>

The current header, an array of strings



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/launchr/mixin/mixlib_cli.rb', line 190

def header(hstring=nil)
  @header ||= []
  case hstring
  when Array
    @header = hstring
  when String
    @header << hstring
  when nil
    @header
  end
end

#option(name, args) ⇒ Object

Add a command line option.

Parameters

name<Symbol>

The name of the option to add

args<Hash>

A hash of arguments for the option, specifying how it should be parsed.

Returns

true

Always returns true.

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
47
48
# File 'lib/launchr/mixin/mixlib_cli.rb', line 38

def option(name, args)
  @options ||= Launchr::OrderedHash.new
  @options_arguments ||= Launchr::OrderedHash.new
  raise(ArgumentError, "Option name must be a symbol") unless name.kind_of?(Symbol)

  strip_arg(args,:short)
  strip_arg(args,:long)
  
  @options[name.to_sym] = args
  @options_arguments[name.to_sym] = args
end

#optionsObject

Get the hash of current options.

Returns

@options<Hash>

The current options hash.



54
55
56
57
# File 'lib/launchr/mixin/mixlib_cli.rb', line 54

def options
  @options ||= Launchr::OrderedHash.new
  @options
end

#options=(val) ⇒ Object

Set the current options hash

Parameters

val<Hash>

The hash to set the options to

Returns

@options<Hash>

The current options hash.

Raises:

  • (ArgumentError)


66
67
68
69
# File 'lib/launchr/mixin/mixlib_cli.rb', line 66

def options=(val)
  raise(ArgumentError, "Options must recieve a hash") unless val.kind_of?(Hash)
  @options = val
end

#options_argumentsObject

Get the combined hash of combined current options plus current arguments.

Returns

@options_arguments<Hash>

The combined current options and current arguments hash.



132
133
134
135
# File 'lib/launchr/mixin/mixlib_cli.rb', line 132

def options_arguments
  @options_arguments ||= Launchr::OrderedHash.new
  @options_arguments
end

#options_arguments=(val) ⇒ Object

Set the current options and current arguments combined hash

Parameters

val<Hash>

The hash to set the combined options and arguments to

Returns

@options_arguments<Hash>

The current options and current arguments hash.

Raises:

  • (ArgumentError)


144
145
146
147
# File 'lib/launchr/mixin/mixlib_cli.rb', line 144

def options_arguments=(val)
  raise(ArgumentError, "Options must recieve a hash") unless val.kind_of?(Hash)
  @options_arguments = val
end

#show_argumentsObject

Return the hash of current arguments as human-readable string.

Returns

<String>

The arguments hash, one per line.



153
154
155
156
# File 'lib/launchr/mixin/mixlib_cli.rb', line 153

def show_arguments
  @arguments ||= Launchr::OrderedHash.new
  summarize_arguments
end

#spaced_summary(bool = nil) ⇒ Object

Seperate options with empty lines. Defaults to: false

Parameters

bool<true,false>

Set to true for newline spacing

Returns

@spaced_summary<String>

The current line spacing setting



260
261
262
263
264
265
266
267
# File 'lib/launchr/mixin/mixlib_cli.rb', line 260

def spaced_summary(bool=nil)
  if bool
    @spaced_summary = bool
  else
    @spaced_summary ||= false
    @spaced_summary
  end
end

#strip_arg(args, arg) ⇒ Object



91
92
93
94
95
96
# File 'lib/launchr/mixin/mixlib_cli.rb', line 91

def strip_arg args, arg
  if args[arg]
    args["#{arg}_strip".to_sym] = args[arg].sub(/\[no-\]/,"").sub(/\s*(\<|\[|=|[A-Z]|[a-zA-z]+\,|\s).*$/,"")
  end
  args
end

#summary_indent(i_string = nil) ⇒ Object

Summary indent. Passed to option parser. Defaults to: ‘ ’ * 4

Parameters

i_string<String>

Set to the indent string

Returns

@summary_indent<String>

The summary indent



228
229
230
231
232
233
234
235
# File 'lib/launchr/mixin/mixlib_cli.rb', line 228

def summary_indent(i_string=nil)
  if i_string
    @summary_indent = i_string
  else
    @summary_indent ||= ' ' * 4
    @summary_indent
  end
end

#summary_width(w_integer = nil) ⇒ Object

Summary indent. Passed to option parser. Defaults to: 32

Parameters

i_string<String>

Set to the indent string

Returns

@summary_indent<String>

The summary indent



244
245
246
247
248
249
250
251
# File 'lib/launchr/mixin/mixlib_cli.rb', line 244

def summary_width(w_integer=nil)
  if w_integer
    @summary_width = w_integer
  else
    @summary_width ||= 32
    @summary_width
  end
end