epitools 0.5.121 → 0.5.122

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Lee Jarvis
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,190 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Epi
4
+ class Slop
5
+ class Commands
6
+ include Enumerable
7
+
8
+ attr_reader :config, :commands, :arguments
9
+ attr_writer :banner
10
+
11
+ # Create a new instance of Slop::Commands and optionally build
12
+ # Slop instances via a block. Any configuration options used in
13
+ # this method will be the default configuration options sent to
14
+ # each Slop object created.
15
+ #
16
+ # config - An optional configuration Hash.
17
+ # block - Optional block used to define commands.
18
+ #
19
+ # Examples:
20
+ #
21
+ # commands = Slop::Commands.new do
22
+ # on :new do
23
+ # on '-o', '--outdir=', 'The output directory'
24
+ # on '-v', '--verbose', 'Enable verbose mode'
25
+ # end
26
+ #
27
+ # on :generate do
28
+ # on '--assets', 'Generate assets', :default => true
29
+ # end
30
+ #
31
+ # global do
32
+ # on '-D', '--debug', 'Enable debug mode', :default => false
33
+ # end
34
+ # end
35
+ #
36
+ # commands[:new].class #=> Slop
37
+ # commands.parse
38
+ #
39
+ def initialize(config = {}, &block)
40
+ @config = config
41
+ @commands = {}
42
+ @banner = nil
43
+ @triggered_command = nil
44
+
45
+ warn "[DEPRECATED] Slop::Commands is deprecated and will be removed in "\
46
+ "Slop version 4. Check out http://injekt.github.com/slop/#commands for "\
47
+ "a new implementation of commands."
48
+
49
+ return unless block_given?
50
+
51
+ block.arity == 1 ? yield(self) : instance_eval(&block)
52
+ end
53
+
54
+ # Optionally set the banner for this command help output.
55
+ #
56
+ # banner - The String text to set the banner.
57
+ #
58
+ # Returns the String banner if one is set.
59
+ def banner(banner = nil)
60
+ @banner = banner if banner
61
+ @banner
62
+ end
63
+
64
+ # Add a Slop instance for a specific command.
65
+ #
66
+ # command - A String or Symbol key used to identify this command.
67
+ # config - A Hash of configuration options to pass to Slop.
68
+ # block - An optional block used to pass options to Slop.
69
+ #
70
+ # Returns the newly created Slop instance mapped to command.
71
+ def on(command, config = {}, &block)
72
+ commands[command.to_s] = Slop.new(@config.merge(config), &block)
73
+ end
74
+
75
+ # Add a Slop instance used when no other commands exist.
76
+ #
77
+ # config - A Hash of configuration options to pass to Slop.
78
+ # block - An optional block used to pass options to Slop.
79
+ #
80
+ # Returns the newly created Slop instance mapped to default.
81
+ def default(config = {}, &block)
82
+ on('default', config, &block)
83
+ end
84
+
85
+ # Add a global Slop instance.
86
+ #
87
+ # config - A Hash of configuration options to pass to Slop.
88
+ # block - An optional block used to pass options to Slop.
89
+ #
90
+ # Returns the newly created Slop instance mapped to global.
91
+ def global(config = {}, &block)
92
+ on('global', config, &block)
93
+ end
94
+
95
+ # Fetch the instance of Slop tied to a command.
96
+ #
97
+ # key - The String or Symbol key used to locate this command.
98
+ #
99
+ # Returns the Slop instance if this key is found, nil otherwise.
100
+ def [](key)
101
+ commands[key.to_s]
102
+ end
103
+ alias get []
104
+
105
+ # Check for a command presence.
106
+ #
107
+ # Examples:
108
+ #
109
+ # cmds.parse %w( foo )
110
+ # cmds.present?(:foo) #=> true
111
+ # cmds.present?(:bar) #=> false
112
+ #
113
+ # Returns true if the given key is present in the parsed arguments.
114
+ def present?(key)
115
+ key.to_s == @triggered_command
116
+ end
117
+
118
+ # Enumerable interface.
119
+ def each(&block)
120
+ @commands.each(&block)
121
+ end
122
+
123
+ # Parse a list of items.
124
+ #
125
+ # items - The Array of items to parse.
126
+ #
127
+ # Returns the original Array of items.
128
+ def parse(items = ARGV)
129
+ parse! items.dup
130
+ items
131
+ end
132
+
133
+ # Parse a list of items, removing any options or option arguments found.
134
+ #
135
+ # items - The Array of items to parse.
136
+ #
137
+ # Returns the original Array of items with options removed.
138
+ def parse!(items = ARGV)
139
+ if (opts = commands[items[0].to_s])
140
+ @triggered_command = items.shift
141
+ execute_arguments! items
142
+ opts.parse! items
143
+ elsif (opts = commands['default'])
144
+ opts.parse! items
145
+ elsif config[:strict] && items[0]
146
+ raise InvalidCommandError, "Unknown command `#{items[0]}`"
147
+ end
148
+ execute_global_opts! items
149
+ items
150
+ end
151
+
152
+ # Returns a nested Hash with Slop options and values. See Slop#to_hash.
153
+ def to_hash
154
+ Hash[commands.map { |k, v| [k.to_sym, v.to_hash] }]
155
+ end
156
+
157
+ # Returns the help String.
158
+ def to_s
159
+ defaults = commands.delete('default')
160
+ globals = commands.delete('global')
161
+ helps = commands.reject { |_, v| v.options.none? }
162
+ helps['Global options'] = globals.to_s if globals && globals.options.any?
163
+ helps['Other options'] = defaults.to_s if defaults && defaults.options.any?
164
+ banner = @banner ? "#{@banner}\n" : ""
165
+ banner + helps.map { |key, opts| " #{key}\n#{opts}" }.join("\n\n")
166
+ end
167
+ alias help to_s
168
+
169
+ # Returns the inspection String.
170
+ def inspect
171
+ "#<Slop::Commands #{config.inspect} #{commands.values.map(&:inspect)}>"
172
+ end
173
+
174
+ private
175
+
176
+ # Returns nothing.
177
+ def execute_arguments!(items)
178
+ @arguments = items.take_while { |arg| !arg.start_with?('-') }
179
+ items.shift @arguments.size
180
+ end
181
+
182
+ # Returns nothing.
183
+ def execute_global_opts!(items)
184
+ return unless (global_opts = commands['global'])
185
+
186
+ global_opts.parse!(items)
187
+ end
188
+ end
189
+ end
190
+ end
@@ -0,0 +1,210 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Epi
4
+ class Slop
5
+ class Option
6
+ # The default Hash of configuration options this class uses.
7
+ DEFAULT_OPTIONS = {
8
+ argument: false,
9
+ optional_argument: false,
10
+ tail: false,
11
+ default: nil,
12
+ callback: nil,
13
+ delimiter: ',',
14
+ limit: 0,
15
+ match: nil,
16
+ optional: true,
17
+ required: false,
18
+ as: String,
19
+ autocreated: false
20
+ }.freeze
21
+
22
+ attr_reader :short, :long, :description, :config, :types
23
+ attr_accessor :count, :argument_in_value
24
+
25
+ # Incapsulate internal option information, mainly used to store
26
+ # option specific configuration data, most of the meat of this
27
+ # class is found in the #value method.
28
+ #
29
+ # slop - The instance of Slop tied to this Option.
30
+ # short - The String or Symbol short flag.
31
+ # long - The String or Symbol long flag.
32
+ # description - The String description text.
33
+ # config - A Hash of configuration options.
34
+ # block - An optional block used as a callback.
35
+ def initialize(slop, short, long, description, config = {}, &block)
36
+ @slop = slop
37
+ @short = short
38
+ @long = long
39
+ @description = description
40
+ @config = DEFAULT_OPTIONS.merge(config)
41
+ @count = 0
42
+ @callback = block_given? ? block : config[:callback]
43
+ @value = nil
44
+
45
+ @types = {
46
+ string: proc { |v| v.to_s },
47
+ symbol: proc { |v| v.to_sym },
48
+ integer: proc { |v| value_to_integer(v) },
49
+ float: proc { |v| value_to_float(v) },
50
+ range: proc { |v| value_to_range(v) },
51
+ count: proc { @count }
52
+ }
53
+
54
+ if long && long.size > @slop.config[:longest_flag]
55
+ @slop.config[:longest_flag] = long.size
56
+ end
57
+
58
+ @config.each_key do |key|
59
+ predicate = :"#{key}?"
60
+ unless self.class.method_defined?(predicate)
61
+ self.class.__send__(:define_method, predicate) { !@config.key?(key) }
62
+ end
63
+ end
64
+ end
65
+
66
+ # Returns true if this option expects an argument.
67
+ def expects_argument?
68
+ config[:argument] && config[:argument] != :optional
69
+ end
70
+
71
+ # Returns true if this option accepts an optional argument.
72
+ def accepts_optional_argument?
73
+ config[:optional_argument] || config[:argument] == :optional
74
+ end
75
+
76
+ # Returns the String flag of this option. Preferring the long flag.
77
+ def key
78
+ long || short
79
+ end
80
+
81
+ # Call this options callback if one exists, and it responds to call().
82
+ #
83
+ # Returns nothing.
84
+ def call(*objects)
85
+ @callback.call(*objects) if @callback.respond_to?(:call)
86
+ end
87
+
88
+ # Set the new argument value for this option.
89
+ #
90
+ # We use this setter method to handle concatenating lists. That is,
91
+ # when an array type is specified and used more than once, values from
92
+ # both options will be grouped together and flattened into a single array.
93
+ def value=(new_value)
94
+ if config[:as].to_s.casecmp('array') == 0
95
+ @value ||= []
96
+
97
+ if new_value.respond_to?(:split)
98
+ @value.concat new_value.split(config[:delimiter], config[:limit])
99
+ end
100
+ else
101
+ @value = new_value
102
+ end
103
+ end
104
+
105
+ # Fetch the argument value for this option.
106
+ #
107
+ # Returns the Object once any type conversions have taken place.
108
+ def value
109
+ value = @value.nil? ? config[:default] : @value
110
+
111
+ return value if [true, false, nil].include?(value) && config[:as].to_s != 'count'
112
+
113
+ type = config[:as]
114
+ if type.respond_to?(:call)
115
+ type.call(value)
116
+ elsif (callable = types[type.to_s.downcase.to_sym])
117
+ callable.call(value)
118
+ else
119
+ value
120
+ end
121
+ end
122
+
123
+ # Returns the help String for this option.
124
+ def to_s
125
+ return config[:help] if config[:help].respond_to?(:to_str)
126
+
127
+ out = " #{short ? "-#{short}, " : ' ' * 4}"
128
+
129
+ if long
130
+ out += "--#{long}"
131
+ size = long.size
132
+ diff = @slop.config[:longest_flag] - size
133
+ out += (' ' * (diff + 6))
134
+ else
135
+ out += (' ' * (@slop.config[:longest_flag] + 8))
136
+ end
137
+
138
+ "#{out}#{description}"
139
+ end
140
+ alias help to_s
141
+
142
+ # Returns the String inspection text.
143
+ def inspect
144
+ "#<Slop::Option [-#{short} | --#{long}" \
145
+ "#{'=' if expects_argument?}#{'=?' if accepts_optional_argument?}]" \
146
+ " (#{description}) #{config.inspect}"
147
+ end
148
+
149
+ private
150
+
151
+ # Convert an object to an Integer if possible.
152
+ #
153
+ # value - The Object we want to convert to an integer.
154
+ #
155
+ # Returns the Integer value if possible to convert, else a zero.
156
+ def value_to_integer(value)
157
+ if @slop.strict?
158
+ begin
159
+ Integer(value.to_s, 10)
160
+ rescue ArgumentError
161
+ raise InvalidArgumentError, "#{value} could not be coerced into Integer"
162
+ end
163
+ else
164
+ value.to_s.to_i
165
+ end
166
+ end
167
+
168
+ # Convert an object to a Float if possible.
169
+ #
170
+ # value - The Object we want to convert to a float.
171
+ #
172
+ # Returns the Float value if possible to convert, else a zero.
173
+ def value_to_float(value)
174
+ if @slop.strict?
175
+ begin
176
+ Float(value.to_s)
177
+ rescue ArgumentError
178
+ raise InvalidArgumentError, "#{value} could not be coerced into Float"
179
+ end
180
+ else
181
+ value.to_s.to_f
182
+ end
183
+ end
184
+
185
+ # Convert an object to a Range if possible.
186
+ #
187
+ # value - The Object we want to convert to a range.
188
+ #
189
+ # Returns the Range value if one could be found, else the original object.
190
+ def value_to_range(value)
191
+ case value.to_s
192
+ when /\A(\-?\d+)\z/
193
+ Range.new(Regexp.last_match(1).to_i, Regexp.last_match(1).to_i)
194
+ when /\A(-?\d+?)(\.\.\.?|-|,)(-?\d+)\z/
195
+ Range.new(
196
+ Regexp.last_match(1).to_i,
197
+ Regexp.last_match(3).to_i,
198
+ Regexp.last_match(2) == '...'
199
+ )
200
+ else
201
+ if @slop.strict?
202
+ raise InvalidArgumentError, "#{value} could not be coerced into Range"
203
+ end
204
+
205
+ value
206
+ end
207
+ end
208
+ end
209
+ end
210
+ end
@@ -106,6 +106,23 @@ module WM
106
106
  system "wmctrl", "-i", "-a", window_id
107
107
  end
108
108
 
109
+ def maximized?
110
+ end
111
+
112
+ def maximize!
113
+ system "wmctrl", "-i", "-a", window_id
114
+ end
115
+
116
+
117
+ def unmazimize!
118
+ end
119
+
120
+ def minimized?
121
+ end
122
+
123
+ def minimize!
124
+ end
125
+
109
126
  #
110
127
  # string is made up of regular text, plus <>'d keypresses
111
128
  # eg: "Hello<Ctrl-T><Ctrl-L><Ctrl-Shift-K><Return>!!!"
@@ -147,6 +147,44 @@ describe Class do
147
147
  end
148
148
  end
149
149
 
150
+ it "traces messages (when $DEBUG is set)" do
151
+ $DEBUG = true
152
+
153
+ class TestButt
154
+ trace_messages_to :a, :b, :c
155
+
156
+ def a(x)
157
+ end
158
+
159
+ def b(x)
160
+ end
161
+
162
+ def c(x,y,z,&block)
163
+ end
164
+ end
165
+
166
+ class ButtTest
167
+ trace_messages_to :*
168
+
169
+ def d
170
+ end
171
+
172
+ def e
173
+ end
174
+ end
175
+
176
+ t = TestButt.new
177
+ t.a(1)
178
+ t.b(2)
179
+ t.c(3,4,5) { :butt }
180
+
181
+ b = ButtTest.new
182
+ b.a
183
+ b.b
184
+
185
+ $DEBUG = false
186
+ end
187
+
150
188
  end
151
189
 
152
190