command_lion 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -1
- data/lib/command_lion/app.rb +41 -15
- data/lib/command_lion/base.rb +2 -0
- data/lib/command_lion/command.rb +254 -26
- data/lib/command_lion/flags.rb +28 -0
- data/lib/command_lion/option.rb +18 -0
- data/lib/command_lion/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e1a46eb35cac2757a52188adce29bcb41c03a26c
|
4
|
+
data.tar.gz: f38a9d1c5559252a4232a5ec0d221e6f73708e55
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8a2c13b8007fdeff85b9bbeadcc1157deb5008e5087910b3b0c6216d0c993e25d264c2583204f968f843e4c0d03f89dc39c5f3b75c8e357f837977656090753c
|
7
|
+
data.tar.gz: 1a8513c041c2097d2fce2bb393f654aa76a718860640404af10ef25e0b1bc31ec9b7352076648f699ea008783ef9e3e5451d7349d6386c398bb22e34803cade1
|
data/README.md
CHANGED
data/lib/command_lion/app.rb
CHANGED
@@ -1,6 +1,23 @@
|
|
1
1
|
module CommandLion
|
2
2
|
|
3
|
-
|
3
|
+
# The App class provides what can be considered the "main" function for the a Command Lion application.
|
4
|
+
#
|
5
|
+
# The App class is primarily used in one of two ways:
|
6
|
+
#
|
7
|
+
# == Building Block
|
8
|
+
# To build an application using the DSL, but not run it right away, the build method block is available.
|
9
|
+
# app = CommandLion::App.build do
|
10
|
+
# # ...
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# app.run!
|
14
|
+
#
|
15
|
+
# == Run Block
|
16
|
+
# To build, parse, and run everything in one concise block, the run method block is available.
|
17
|
+
# CommandLion::App.run do
|
18
|
+
# # ...
|
19
|
+
# end
|
20
|
+
#
|
4
21
|
# == DSL Keywords:
|
5
22
|
# name::
|
6
23
|
# The name of your application. This is how your application would be referenced in conversation.
|
@@ -179,20 +196,21 @@ module CommandLion
|
|
179
196
|
|
180
197
|
# An application usually has multiple commands.
|
181
198
|
#
|
182
|
-
#
|
183
|
-
#
|
184
|
-
#
|
185
|
-
#
|
186
|
-
#
|
187
|
-
#
|
199
|
+
# == Example
|
200
|
+
# app = CommandLion::App.build
|
201
|
+
# # meta information
|
202
|
+
#
|
203
|
+
# command :example1 do
|
204
|
+
# # more code
|
205
|
+
# end
|
188
206
|
#
|
189
|
-
#
|
190
|
-
#
|
207
|
+
# command :example2 do
|
208
|
+
# # more code
|
209
|
+
# end
|
191
210
|
# end
|
192
|
-
# end
|
193
211
|
#
|
194
|
-
#
|
195
|
-
#
|
212
|
+
# app.commands.map(&:name)
|
213
|
+
# # => [:example1, :example2]
|
196
214
|
def command(index, &block)
|
197
215
|
if index.is_a? Command
|
198
216
|
cmd = index
|
@@ -227,22 +245,26 @@ module CommandLion
|
|
227
245
|
cmd
|
228
246
|
end
|
229
247
|
|
230
|
-
# Plugin a command.
|
248
|
+
# Plugin a command that's probably been built outside of the application's run or build block.
|
249
|
+
# This is helpful for sharing or reusing commands in applications.
|
250
|
+
# @param command [Command]
|
231
251
|
def plugin(command)
|
232
252
|
command(command)
|
233
253
|
end
|
234
254
|
|
235
|
-
# Direct access to the various flags an application has.
|
255
|
+
# Direct access to the various flags an application has. Helpfulp for debugging.
|
236
256
|
def flags
|
237
257
|
@flags
|
238
258
|
end
|
239
259
|
|
240
|
-
# Direct access to the various commands an application has.
|
260
|
+
# Direct access to the various commands an application has. Helpful for debugging.
|
241
261
|
def commands
|
242
262
|
@commands
|
243
263
|
end
|
244
264
|
|
245
265
|
# Parse arguments off of ARGV.
|
266
|
+
#
|
267
|
+
# @TODO Re-visit this.
|
246
268
|
def parse
|
247
269
|
@commands.each do |_, cmd|
|
248
270
|
if cmd.flags?
|
@@ -266,6 +288,7 @@ module CommandLion
|
|
266
288
|
end
|
267
289
|
|
268
290
|
# Parse a given command with its
|
291
|
+
# @TODO Re-visit this.
|
269
292
|
def parse_cmd(cmd, flags)
|
270
293
|
if cmd.flags?
|
271
294
|
args = Raw.arguments_to(cmd.flags.short, flags)
|
@@ -344,12 +367,15 @@ module CommandLion
|
|
344
367
|
nil
|
345
368
|
end
|
346
369
|
|
370
|
+
# @TODO Re-visit this.
|
347
371
|
def run!
|
348
372
|
parse do |cmd|
|
349
373
|
cmd.action.call
|
350
374
|
end
|
351
375
|
end
|
352
376
|
|
377
|
+
# Run the application if the file is the main file being run.
|
378
|
+
# It's almost kind of narcisitc, if you think about it.
|
353
379
|
if __FILE__== $0
|
354
380
|
run!
|
355
381
|
end
|
data/lib/command_lion/base.rb
CHANGED
data/lib/command_lion/command.rb
CHANGED
@@ -1,54 +1,282 @@
|
|
1
1
|
module CommandLion
|
2
2
|
|
3
|
-
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
3
|
+
# Every command or option for Command Lion is built on the Command class.
|
4
|
+
#
|
5
|
+
# A Command is typically built using the DSL provided within a build method block:
|
6
|
+
# == ⚙️ Build Block
|
7
|
+
# cmd = CommandLion::Command.build do
|
8
|
+
# # ...
|
9
|
+
# end
|
10
|
+
# This is used under the hood within an Application's DSL run or block:
|
11
|
+
# == ⚙️ Application Build Block
|
12
|
+
# app = CommandLion::App.build do
|
13
|
+
# command :example_index do
|
14
|
+
# # ...
|
15
|
+
# end
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# The DSL keywords are simple methods provided for any Command object and can be accessed
|
19
|
+
# or modified outside of the DSL building block itself to, for example, query if it exists.
|
20
|
+
#
|
21
|
+
# == 🗣 DSL
|
22
|
+
# Command Lion's DSL is meant to be as flexible as possible without being too compelx.
|
23
|
+
#
|
24
|
+
# ==⚡️ Example
|
25
|
+
# cmd = CommandLion::Command.build do
|
26
|
+
# index :example
|
27
|
+
# flags do
|
28
|
+
# short "-e"
|
29
|
+
# long "--example"
|
30
|
+
# end
|
31
|
+
# type :strings
|
32
|
+
# default ["Jim", "Pam", "Dwight", "Michael"]
|
33
|
+
# before do
|
34
|
+
# unless arguments.count > 2
|
35
|
+
# abort "Must provide more than two arguments!"
|
36
|
+
# end
|
37
|
+
# end
|
38
|
+
# action do
|
39
|
+
# arguments.each do |argument|
|
40
|
+
# puts "Hello #{argument}!"
|
41
|
+
# end
|
42
|
+
# end
|
43
|
+
# after do
|
44
|
+
# exit 0
|
45
|
+
# end
|
46
|
+
# end
|
47
|
+
#
|
48
|
+
# == Keywords
|
49
|
+
# 🔑 description::
|
50
|
+
# To provide further context for your command's existence, it's fairly nice
|
51
|
+
# to have a description.
|
20
52
|
#
|
21
53
|
# == Example
|
22
|
-
#
|
54
|
+
# cmd = CommandLion::Command.build do
|
23
55
|
# description "Example"
|
24
56
|
# end
|
25
57
|
#
|
26
|
-
#
|
58
|
+
# cmd.description?
|
27
59
|
# # => true
|
28
60
|
#
|
29
|
-
#
|
61
|
+
# cmd.description = "Changed"
|
30
62
|
# # => "Changed"
|
31
63
|
#
|
32
|
-
#
|
64
|
+
# cmd.description
|
33
65
|
# # => Changed
|
34
|
-
#
|
66
|
+
# 🔑 type::
|
67
|
+
# A command may require a specific argument from the command-line. The type
|
68
|
+
# of argument(s) that the command utilizies can be specified with this keyword.
|
69
|
+
# == Example
|
70
|
+
# cmd = CommandLion::Command.build do
|
71
|
+
# type :string
|
72
|
+
# end
|
73
|
+
#
|
74
|
+
# cmd.type?
|
75
|
+
# # => true
|
76
|
+
#
|
77
|
+
# cmd.type = :strings
|
78
|
+
# # => :strings
|
79
|
+
#
|
80
|
+
# cmd.type
|
81
|
+
# # => :strings
|
82
|
+
# 🔑 default::
|
83
|
+
# To specify a command's default arguments, the default keyword can be used.
|
84
|
+
# == Example
|
85
|
+
# cmd = CommandLion::Command.build do
|
86
|
+
# default "example"
|
87
|
+
# end
|
88
|
+
#
|
89
|
+
# cmd.default?
|
90
|
+
# # => true
|
91
|
+
#
|
92
|
+
# cmd.default = "EXAMPLE"
|
93
|
+
# # => "EXAMPLE"
|
94
|
+
#
|
95
|
+
# cmd.default
|
96
|
+
# # => "EXAMPLE"
|
97
|
+
#
|
98
|
+
# cmd.argument
|
99
|
+
# # => "EXAMPLE"
|
100
|
+
# 🔑 delimiter::
|
101
|
+
# In the case of multiple command-line arguments, a custom delimter can be used
|
102
|
+
# to help split up the arguments. Command Lion uses the space betwen arguments as the
|
103
|
+
# default delimter.
|
104
|
+
#
|
105
|
+
# == Example
|
106
|
+
# cmd = CommandLion::Command.build do
|
107
|
+
# delimter ","
|
108
|
+
# end
|
109
|
+
#
|
110
|
+
# cmd.delimter?
|
111
|
+
# # => true
|
112
|
+
#
|
113
|
+
# cmd.delimter = ":"
|
114
|
+
# # => ":"
|
115
|
+
#
|
116
|
+
# cmd.delimter
|
117
|
+
# # => :
|
118
|
+
# 🔑 flag::
|
119
|
+
# If you'd like for one specfic flag to be used for the command, then this keyword is for you!
|
120
|
+
#
|
121
|
+
# == Example
|
122
|
+
# cmd = CommandLion::Command.build do
|
123
|
+
# flag "example"
|
124
|
+
# end
|
125
|
+
#
|
126
|
+
# cmd.flag?
|
127
|
+
# # => true
|
128
|
+
#
|
129
|
+
# cmd.flag = "EXAMPLE"
|
130
|
+
# # => "EXAMPLE"
|
131
|
+
#
|
132
|
+
# cmd.flag
|
133
|
+
# # => "EXAMPLE"
|
134
|
+
# 🔑 flags::
|
135
|
+
# The flags keywords can be used two specify the short and long flags option for a command.
|
136
|
+
#
|
137
|
+
# == Example
|
138
|
+
# cmd = CommandLion::Command.build do
|
139
|
+
# flags do
|
140
|
+
# short "-e"
|
141
|
+
# long "--example
|
142
|
+
# end
|
143
|
+
# end
|
144
|
+
#
|
145
|
+
# cmd.flags?
|
146
|
+
# # => true
|
147
|
+
#
|
148
|
+
# cmd.flags.short?
|
149
|
+
# # => true
|
150
|
+
#
|
151
|
+
# cmd.flags.long?
|
152
|
+
# # => true
|
153
|
+
#
|
154
|
+
# cmd.flags.short = "-E"
|
155
|
+
# # => "-E"
|
156
|
+
#
|
157
|
+
# cmd.flags.long = "--EXAMPLE"
|
158
|
+
# # => "--EXAMPLE"
|
159
|
+
#
|
160
|
+
# cmd.flags.long
|
161
|
+
# # => "--EXAMPLE"
|
162
|
+
#
|
163
|
+
# cmd.flags.short
|
164
|
+
# # => "-E"
|
165
|
+
# 🔑 threaded::
|
35
166
|
# To have your command spawn a thread and have the action block
|
36
167
|
# for your command run in its own background thread.
|
37
168
|
#
|
38
169
|
# == Example
|
39
|
-
#
|
170
|
+
# cmd = CommandLion::Command.build do
|
40
171
|
# description "Example"
|
41
172
|
# end
|
42
173
|
#
|
43
|
-
#
|
174
|
+
# cmd.description?
|
44
175
|
# # => true
|
45
176
|
#
|
46
|
-
#
|
177
|
+
# cmd.description = "Changed"
|
47
178
|
# # => "Changed"
|
48
179
|
#
|
49
|
-
#
|
180
|
+
# cmd.description
|
50
181
|
# # => Changed
|
182
|
+
# 🔑 action::
|
183
|
+
# What do you want a command to do when it is used? The action keyword can be used
|
184
|
+
# to capture the block you'd like to run when the command is used.
|
185
|
+
#
|
186
|
+
# == Example
|
187
|
+
# cmd = CommandLion::Command.build do
|
188
|
+
# action do
|
189
|
+
# puts "Hello World!"
|
190
|
+
# end
|
191
|
+
# end
|
192
|
+
#
|
193
|
+
# cmd.action?
|
194
|
+
# # => true
|
195
|
+
#
|
196
|
+
# cmd.action
|
197
|
+
# # => #<Proc:...>
|
198
|
+
#
|
199
|
+
# cmd.action.call
|
200
|
+
# # => Hello World! .. to STDOUT
|
201
|
+
# 🔑 before::
|
202
|
+
# Before the action block is called, you can specify anouther block to be used beforehand
|
203
|
+
# which can be used to help setup or do some custom error checking.
|
204
|
+
#
|
205
|
+
# == Example
|
206
|
+
# cmd = CommandLion::Command.build do
|
207
|
+
# before do
|
208
|
+
# abort "Not on Mondays!" Time.now.monday?
|
209
|
+
# end
|
210
|
+
# action do
|
211
|
+
# puts "Hello World!"
|
212
|
+
# end
|
213
|
+
# end
|
214
|
+
#
|
215
|
+
# cmd.before?
|
216
|
+
# # => true
|
217
|
+
#
|
218
|
+
# cmd.before
|
219
|
+
# # => #<Proc:...>
|
220
|
+
#
|
221
|
+
# cmd.before.call
|
222
|
+
# # aborts application if it's Monday
|
223
|
+
# 🔑 after::
|
224
|
+
# After the action block has been called and completed, anouther optional block
|
225
|
+
# can be used within the block given in the after keyword. This can be used for all sorts
|
226
|
+
# of nifty things: from stopping the application from moving on, to logging, to whatever else.
|
227
|
+
# == Example
|
228
|
+
# cmd = CommandLion::Command.build do
|
229
|
+
# action do
|
230
|
+
# puts "Hello World!"
|
231
|
+
# end
|
232
|
+
# after do
|
233
|
+
# exit 0
|
234
|
+
# end
|
235
|
+
# end
|
236
|
+
#
|
237
|
+
# cmd.after?
|
238
|
+
# # => true
|
239
|
+
#
|
240
|
+
# cmd.after
|
241
|
+
# # => #<Proc:...>
|
242
|
+
#
|
243
|
+
# cmd.after.call
|
244
|
+
# # exits application with successful status code
|
245
|
+
# 🔑 index::
|
246
|
+
# A command's index should be unique. It is used to used to accesses the command amongst other
|
247
|
+
# commands when used within an application. However, this keyword usually isn't used unless being utilized
|
248
|
+
# when using Command Lion's plugin system.
|
249
|
+
# == Example
|
250
|
+
# cmd = CommandLion::Command.build do
|
251
|
+
# index :example
|
252
|
+
# end
|
253
|
+
#
|
254
|
+
# cmd.index?
|
255
|
+
# # => :example
|
256
|
+
#
|
257
|
+
# cmd.index
|
258
|
+
# # => :example
|
259
|
+
# 🔑 option::
|
260
|
+
# a command may have mutiple sub commands or options associated with it. these effectively
|
261
|
+
# work exactly like any other command, but are just started as a leaf under the paren't command's options.
|
262
|
+
# == Example
|
263
|
+
# cmd = CommandLion::Command.build do
|
264
|
+
# # ...
|
265
|
+
# option :example do
|
266
|
+
# action do
|
267
|
+
# puts "hello world!"
|
268
|
+
# end
|
269
|
+
# end
|
270
|
+
# end
|
271
|
+
#
|
272
|
+
# cmd.options?
|
273
|
+
# # => true
|
274
|
+
#
|
275
|
+
# cmd.options[:example]
|
276
|
+
# # => #<proc:...>
|
51
277
|
#
|
278
|
+
# cmd.after.call
|
279
|
+
# # exits the application with successful status code
|
52
280
|
#
|
53
281
|
class Command < Base
|
54
282
|
|
data/lib/command_lion/flags.rb
CHANGED
@@ -1,5 +1,33 @@
|
|
1
1
|
module CommandLion
|
2
2
|
|
3
|
+
# The way a user is able to call or access a command or option for
|
4
|
+
# a command-line application is by passing their flags when the application
|
5
|
+
# is run at the command-line.
|
6
|
+
#
|
7
|
+
# == 🗣 DSL
|
8
|
+
# The flags DSL works three different ways.
|
9
|
+
#
|
10
|
+
# == Index as Flag
|
11
|
+
# app = CommandLion::Command.build do
|
12
|
+
# command :hello do
|
13
|
+
# # just use the index as the flag
|
14
|
+
# end
|
15
|
+
# end
|
16
|
+
# == One Flag
|
17
|
+
# app = CommandLion::Command.build do
|
18
|
+
# command :hello do
|
19
|
+
# flag "--hello"
|
20
|
+
# end
|
21
|
+
# end
|
22
|
+
# == Short & Long Flags
|
23
|
+
# app = CommandLion::Command.build do
|
24
|
+
# command :hello do
|
25
|
+
# flags do
|
26
|
+
# short "-e"
|
27
|
+
# long "--example"
|
28
|
+
# end
|
29
|
+
# end
|
30
|
+
# end
|
3
31
|
class Flags < Base
|
4
32
|
simple_attrs :short, :long
|
5
33
|
end
|
data/lib/command_lion/option.rb
CHANGED
@@ -1,3 +1,21 @@
|
|
1
1
|
module CommandLion
|
2
|
+
|
3
|
+
# The Option class is a direct sub-class of the Command class. In pretty much
|
4
|
+
# every way it is just a command under the hood. However, instead of being indexed
|
5
|
+
# in an application's commands index, it will be available in whatever command's
|
6
|
+
# options index.
|
7
|
+
#
|
8
|
+
# == Example
|
9
|
+
#
|
10
|
+
# app = CommandLion::App.build do
|
11
|
+
# command :example_command do
|
12
|
+
# # ...
|
13
|
+
# option :example_option do
|
14
|
+
# # ...
|
15
|
+
# end
|
16
|
+
# end
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# app.commands[:example_command].options[:example_option]
|
2
20
|
class Option < Command; end
|
3
21
|
end
|
data/lib/command_lion/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: command_lion
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kent 'picat' Gruber
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-09-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|