Class: Peeek::CLI::Options

Inherits:
Object
  • Object
show all
Includes:
EncodingOptions
Defined in:
lib/peeek/cli/options.rb

Constant Summary collapse

SILENCE =
0
MEDIUM =
1
VERBOSE =
2

Instance Attribute Summary collapse

Attributes included from EncodingOptions

#external_encoding, #internal_encoding

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = []) ⇒ Options

Initialize the CLI options.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/peeek/cli/options.rb', line 47

def initialize(argv = [])
  @debug             = $DEBUG
  @verbose           = $VERBOSE
  @version_requested = false

  opt = OptionParser.new
  opt.banner = 'Usage: peeek [switches] [--] [programfile] [arguments]'
  opt.summary_indent = ' ' * 2
  opt.summary_width = 15

  @working_directory = nil

  opt.on('-Cdirectory', 'cd to directory before executing your script') do |directory|
    @working_directory = directory
  end

  opt.on('-d', '--debug', 'set debugging flags (set $DEBUG to true)') do
    @debug = true
    @verbose = verbose_by(VERBOSE)
  end

  commands = []

  opt.on("-e 'command'", "one line of script. Several -e's allowed. Omit [programfile]") do |command|
    commands << command
  end

  if self.class.encoding_options_enabled?
    @external_encoding = Encoding.default_external
    @internal_encoding = Encoding.default_internal

    opt.on('-Eex[:in]', '--encoding=ex[:in]', 'specify the default external and internal character encodings') do |encodings|
      external_encoding, internal_encoding = parse_encodings(encodings)
      @external_encoding = external_encoding if external_encoding
      @internal_encoding = internal_encoding
    end
  end

  @hook_targets = []

  opt.on('-Hstring', 'object and method name that is target of hook') do |string|
    hook_spec = Hook::Specifier.parse(string)
    @hook_targets << hook_spec unless @hook_targets.include?(hook_spec)
  end

  @directories_to_load = []

  opt.on('-Idirectory', 'specify $LOAD_PATH directory (may be used more than once)') do |directory|
    @directories_to_load << directory unless @directories_to_load.include?(directory)
  end

  @required_libraries = []

  opt.on('-rlibrary', 'require the library before executing your script') do |library|
    @required_libraries << library unless @required_libraries.include?(library)
  end

  opt.on('-v', 'print version number, then turn on verbose mode') do
    @version_requested = true
    @verbose = verbose_by(VERBOSE)
  end

  opt.on('-w', '--verbose', 'turn warnings on for your script') do
    @verbose = verbose_by(VERBOSE)
  end

  level_strings = [:SILENCE, :MEDIUM, :VERBOSE].map do |const_name|
    "#{self.class.const_get(const_name)}=#{const_name.to_s.downcase}"
  end

  opt.on("-W[level=#{VERBOSE}]", "set warning level; #{level_strings * ', '}", Integer) do |level|
    @verbose = verbose_by(level || VERBOSE)
  end

  @continued = true

  opt.on('--version', 'print the version') do
    @version_requested = true
    @continued = false
  end

  opt.on_tail('-h', '--help', 'show this message') do
    raise Help, opt.help
  end

  @arguments = opt.order(argv)
  @command = commands * '; '
end

Instance Attribute Details

#argumentsArray<String>



166
167
168
# File 'lib/peeek/cli/options.rb', line 166

def arguments
  @arguments
end

#commandString



162
163
164
# File 'lib/peeek/cli/options.rb', line 162

def command
  @command
end

#debugBoolean



138
139
140
# File 'lib/peeek/cli/options.rb', line 138

def debug
  @debug
end

#directories_to_loadArray<String>



150
151
152
# File 'lib/peeek/cli/options.rb', line 150

def directories_to_load
  @directories_to_load
end

#hook_targetsArray<Peeek::Hook::Specifier>



158
159
160
# File 'lib/peeek/cli/options.rb', line 158

def hook_targets
  @hook_targets
end

#required_librariesArray<String>



154
155
156
# File 'lib/peeek/cli/options.rb', line 154

def required_libraries
  @required_libraries
end

#verboseBoolean?



142
143
144
# File 'lib/peeek/cli/options.rb', line 142

def verbose
  @verbose
end

#working_directoryString



146
147
148
# File 'lib/peeek/cli/options.rb', line 146

def working_directory
  @working_directory
end

Class Method Details

.encoding_options_enabled?Boolean

Determine if CLI options class has enable encoding options.



40
41
42
# File 'lib/peeek/cli/options.rb', line 40

def self.encoding_options_enabled?
  include?(EncodingOptions)
end

Instance Method Details

#arguments_given?Boolean

Determine if arguments given.



185
186
187
# File 'lib/peeek/cli/options.rb', line 185

def arguments_given?
  !@arguments.empty?
end

#command_given?Boolean

Determine if a command given.



178
179
180
# File 'lib/peeek/cli/options.rb', line 178

def command_given?
  !@command.empty?
end

#continued?Boolean

Determine if continue process.



192
193
194
# File 'lib/peeek/cli/options.rb', line 192

def continued?
  @continued
end

#version_requested?Boolean

Determine if print of the version requested.



171
172
173
# File 'lib/peeek/cli/options.rb', line 171

def version_requested?
  @version_requested
end