drydock 0.6.9 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGES.txt +26 -21
- data/LICENSE.txt +2 -2
- data/README.md +92 -0
- data/Rakefile +40 -58
- data/bin/example +92 -96
- data/drydock.gemspec +41 -37
- data/lib/drydock.rb +265 -281
- metadata +52 -48
- data/README.rdoc +0 -92
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c105c4a57f26570c841a285bd6a66e4f6431521a4ecea4e559d3bd2d9e41bfd9
|
4
|
+
data.tar.gz: 0bb037cb1ff1540840a6ce096dce08057189716c6a4bd073a568fe0aaca821e5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0251b4d015b0c8b61e25281a8df1e14b3008ee20049d925896ef98d45221200d2cb4ea529130f6b92212a6cf83bffe23788f8e59eda8941c274d7e32c164dc6b
|
7
|
+
data.tar.gz: 215ba8918bec521fecbc5491fac850ae8d573af321544514db808b21b45ace1d2d34eff911dac0ee93bc3cddf6fbb0d2752f53819fe895e2ce2c390201d627f3
|
data/CHANGES.txt
CHANGED
@@ -1,8 +1,15 @@
|
|
1
1
|
DRYDOCK, CHANGES
|
2
2
|
|
3
|
+
#### 1.0.0 (2025-07-14) #############################
|
4
|
+
|
5
|
+
* CHANGE: Support for literal string will be frozen warnings
|
6
|
+
* CHANGE: Update dependencies
|
7
|
+
* CHANGE: Clean up docs in repo root
|
8
|
+
|
9
|
+
|
3
10
|
#### 0.6.9 (2010-02-12) #############################
|
4
11
|
|
5
|
-
* CHANGE: Removed hanna dependency [Flameeyes]
|
12
|
+
* CHANGE: Removed hanna dependency [Diego Elio 'Flameeyes' Pettenò]
|
6
13
|
* CHANGE: Using autoload where appropriate.
|
7
14
|
|
8
15
|
|
@@ -21,24 +28,24 @@ DRYDOCK, CHANGES
|
|
21
28
|
NOTE: Because at_exit has been removed, Drydock scripts will no longer
|
22
29
|
run automatically. You can explicitly call the following at the bottom
|
23
30
|
of your scripts:
|
24
|
-
|
31
|
+
|
25
32
|
Drydock.run!(ARGV, STDIN) if Drydock.run?
|
26
33
|
|
27
|
-
* CHANGE: Removed at_exit
|
34
|
+
* CHANGE: Removed at_exit
|
28
35
|
* CHANGE: Drydock.run? will now return false if Drydock hasl already run
|
29
36
|
|
30
37
|
|
31
38
|
#### 0.6.5 (2009-05-21) #############################
|
32
39
|
|
33
|
-
* ADDED: "with_args" support for default command. When specified,
|
34
|
-
arguments can be passed to the default command with run in the
|
40
|
+
* ADDED: "with_args" support for default command. When specified,
|
41
|
+
arguments can be passed to the default command with run in the
|
35
42
|
short form. e.g. "script arg1 arg2" == "script cmdname arg1 arg2"
|
36
43
|
|
37
44
|
#### 0.6.3 (2009-05-10) #############################
|
38
45
|
|
39
46
|
* ADDED: show-commands now displays a note about which command is the default
|
40
47
|
* CHANGE: Moved mixins to lib/drydock/mixins (so other projects can require 'drydock/mixins')
|
41
|
-
* FIXED: Support for inline command aliases when specifying a class:
|
48
|
+
* FIXED: Support for inline command aliases when specifying a class:
|
42
49
|
command [:name, :alias1, :alias2] => SomeClass
|
43
50
|
|
44
51
|
|
@@ -59,19 +66,19 @@ command [:name, :alias1, :alias2] => SomeClass
|
|
59
66
|
* CHANGE: 'desc' is now 'about' (desc works, but it prints a notice)
|
60
67
|
* CHANGE: I now recommend implementing the Drydock DSL in a module.
|
61
68
|
bin/example was updated to reflect the change. This prevents Drydock
|
62
|
-
keywords from being included in the global namespace.
|
69
|
+
keywords from being included in the global namespace.
|
63
70
|
* ADDED: Inline commands aliases. command :cmd1, :cmd2 do; ...; end
|
64
|
-
* ADDED: Unknown commands can be directed to a trawler.
|
71
|
+
* ADDED: Unknown commands can be directed to a trawler.
|
65
72
|
|
66
73
|
|
67
74
|
#### 0.5.6 (2009-04-22) #############################
|
68
75
|
|
69
|
-
* CHANGED: Interrupts now handled in rescue rather than a trap.
|
76
|
+
* CHANGED: Interrupts now handled in rescue rather than a trap.
|
70
77
|
* ADDED: Drydock::ArgError and Drydock::OptError are rescued at runtime by default
|
71
78
|
|
72
79
|
#### 0.5.5 (2009-04-19) #############################
|
73
80
|
|
74
|
-
* CHANGED: Improved help screen formatting.
|
81
|
+
* CHANGED: Improved help screen formatting.
|
75
82
|
|
76
83
|
#### 0.5.4 (2009-04-15) #############################
|
77
84
|
|
@@ -79,22 +86,22 @@ keywords from being included in the global namespace.
|
|
79
86
|
|
80
87
|
#### 0.5.3 (2009-04-05) #############################
|
81
88
|
|
82
|
-
* FIXED: Command actions were not being handled correctly. Added rdocs to
|
89
|
+
* FIXED: Command actions were not being handled correctly. Added rdocs to
|
83
90
|
clarify the code.
|
84
91
|
|
85
92
|
#### 0.5.2 (2009-04-04) #############################
|
86
93
|
|
87
|
-
* ADDED: before and after blocks now receive a primed reference to the
|
94
|
+
* ADDED: before and after blocks now receive a primed reference to the
|
88
95
|
command object (which gives them access to the globals and options)
|
89
96
|
* CHANGE: The prep stuff in Drydock::Command#call is now split into a
|
90
|
-
separate method: prepare so call no longer takes arguments.
|
91
|
-
* FIXED: Drydock#capture_io was using yield. It now accepts a block instead.
|
97
|
+
separate method: prepare so call no longer takes arguments.
|
98
|
+
* FIXED: Drydock#capture_io was using yield. It now accepts a block instead.
|
92
99
|
|
93
100
|
|
94
101
|
#### 0.5.1 (2009-03-15) #############################
|
95
102
|
|
96
103
|
* FIXED: Prevent calling default command in at_exit when there's a LoadError.
|
97
|
-
* FIXED: Exit gracefully when the application exits.
|
104
|
+
* FIXED: Exit gracefully when the application exits.
|
98
105
|
* FIXED: Print command names with dashes rather than underscores
|
99
106
|
|
100
107
|
|
@@ -106,8 +113,8 @@ separate method: prepare so call no longer takes arguments.
|
|
106
113
|
* ADDED: Named argv values.
|
107
114
|
* CHANGE: argv are now part of the Command class (not passed to command blocks)
|
108
115
|
* CHANGE: "project" now automatically requires the lowercase name of the project
|
109
|
-
and gracefully continues if the require failed.
|
110
|
-
* CHANGE: Drydock will look for different validation method, based on the method
|
116
|
+
and gracefully continues if the require failed.
|
117
|
+
* CHANGE: Drydock will look for different validation method, based on the method
|
111
118
|
being executed. If a validation method is found it's executed and
|
112
119
|
must return a true valid (it can also raise its own exceptions).
|
113
120
|
* ADDED: command actions. These are boolean switches with a twist. Drydock looks
|
@@ -125,7 +132,7 @@ and sending to other methods manually.
|
|
125
132
|
* ADDED: Tries to call obj.command if available when no block is supplied
|
126
133
|
* ADDED: "show_commands" command built-in. Displays commands with descriptions
|
127
134
|
* ADDED: A default usage help msg for every command: "#{$0} command-name"
|
128
|
-
* ADDED: "usage" work multiple times for the same command.
|
135
|
+
* ADDED: "usage" work multiple times for the same command.
|
129
136
|
* ADDED: "desc" method for per command descriptions
|
130
137
|
* CHANGE: options are now stored as obj.option.name instead of obj.name
|
131
138
|
* CHANGE: global options are now stored as obj.globals.name
|
@@ -145,7 +152,7 @@ and sending to other methods manually.
|
|
145
152
|
#### 0.3 (2009-02-05) ###############################
|
146
153
|
|
147
154
|
* Added support for custom Drydock::Commands objects
|
148
|
-
* Global and command-specific options are now available as
|
155
|
+
* Global and command-specific options are now available as
|
149
156
|
attributes of the Drydock::Commands class instance.
|
150
157
|
* Automatic execution
|
151
158
|
* Now in a single file (lib/drydock.rb)
|
@@ -157,5 +164,3 @@ and sending to other methods manually.
|
|
157
164
|
|
158
165
|
* Initial release
|
159
166
|
* Forked from bmizerany/frylock
|
160
|
-
|
161
|
-
|
data/LICENSE.txt
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (c) 2008-
|
1
|
+
Copyright (c) 2008-2025 Delano Mandelbaum
|
2
2
|
|
3
3
|
Permission is hereby granted, free of charge, to any person
|
4
4
|
obtaining a copy of this software and associated documentation
|
@@ -19,4 +19,4 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
19
19
|
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
20
|
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
21
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
-
OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# Drydock - v1.0
|
2
|
+
|
3
|
+
**Build seaworthy command-line apps like a Captain with a powerful Ruby DSL.**
|
4
|
+
|
5
|
+
## Overview
|
6
|
+
|
7
|
+
Drydock is a seaworthy DSL for building really powerful command line applications. The core class is contained in a single .rb file so it's easy to copy directly into your project. See below for examples.
|
8
|
+
|
9
|
+
## Install
|
10
|
+
|
11
|
+
One of:
|
12
|
+
|
13
|
+
* `gem install drydock`
|
14
|
+
* copy `lib/drydock.rb` into your `lib` directory.
|
15
|
+
|
16
|
+
Or for GitHub fans:
|
17
|
+
|
18
|
+
* `git clone git://github.com/delano/drydock.git`
|
19
|
+
* `gem install delano-drydock`
|
20
|
+
|
21
|
+
## Examples
|
22
|
+
|
23
|
+
See `bin/example` for more.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'drydock'
|
27
|
+
extend Drydock
|
28
|
+
|
29
|
+
default :welcome
|
30
|
+
|
31
|
+
before do
|
32
|
+
# You can execute a block before the requests command is executed. Instance
|
33
|
+
# variables defined here will be available to all commands.
|
34
|
+
end
|
35
|
+
|
36
|
+
about "A friendly welcome to the Drydock"
|
37
|
+
command :welcome do
|
38
|
+
puts "Welcome to Drydock."
|
39
|
+
puts "For available commands:"
|
40
|
+
puts "#{$0} show-commands"
|
41
|
+
end
|
42
|
+
|
43
|
+
usage "USAGE: #{$0} laugh [-f]"
|
44
|
+
about "The captain commands his crew to laugh"
|
45
|
+
option :f, :faster, "A boolean value. Go even faster!"
|
46
|
+
command :laugh do |obj|
|
47
|
+
# +obj+ is an instance of Drydock::Command. The options you define are available
|
48
|
+
# via obj.option.name
|
49
|
+
|
50
|
+
answer = !obj.option.faster ? "Sort of" : "Yes! I'm literally laughing as fast as possible."
|
51
|
+
|
52
|
+
puts "Captain Stubing: Are you laughing?"
|
53
|
+
puts "Dr. Bricker: " << answer
|
54
|
+
end
|
55
|
+
|
56
|
+
class JohnWestSmokedOysters < Drydock::Command
|
57
|
+
# You can write your own command classes by inheriting from Drydock::Command
|
58
|
+
# and referencing it in the command definition.
|
59
|
+
def ahoy!; p "matey"; end
|
60
|
+
end
|
61
|
+
|
62
|
+
about "Do something with John West's Smoked Oysters"
|
63
|
+
command :oysters => JohnWestSmokedOysters do |obj|
|
64
|
+
p obj # => #<JohnWestSmokedOysters:0x42179c ... >
|
65
|
+
end
|
66
|
+
|
67
|
+
about "My way of saying hello!"
|
68
|
+
command :ahoy! => JohnWestSmokedOysters
|
69
|
+
# If you don't provide a block, Drydock will call JohnWestSmokedOysters#ahoy!
|
70
|
+
|
71
|
+
Drydock.run!
|
72
|
+
```
|
73
|
+
|
74
|
+
## More Information
|
75
|
+
|
76
|
+
* [GitHub](http://github.com/delano/drydock)
|
77
|
+
* [RDocs](http://drydock.rubyforge.org/)
|
78
|
+
* [Inspiration](http://www.youtube.com/watch?v=m_wFEB4Oxlo)
|
79
|
+
|
80
|
+
## Thanks
|
81
|
+
|
82
|
+
* Solutious Inc for putting up with my endless references to the sea! ([http://solutious.com](http://solutious.com))
|
83
|
+
* Blake Mizerany for the inspiration via [bmizerany-frylock](http://github.com/bmizerany/frylock)
|
84
|
+
|
85
|
+
## Credits
|
86
|
+
|
87
|
+
* Delano Mandelbaum (delano@solutious.com)
|
88
|
+
* Bernie Kopell (bernie@solutious.com)
|
89
|
+
|
90
|
+
## License
|
91
|
+
|
92
|
+
See LICENSE.txt
|
data/Rakefile
CHANGED
@@ -1,78 +1,60 @@
|
|
1
|
-
require '
|
1
|
+
require 'bundler/gem_tasks'
|
2
2
|
require 'rake/clean'
|
3
|
-
require '
|
4
|
-
require 'fileutils'
|
5
|
-
include FileUtils
|
6
|
-
|
7
|
-
task :default => :test
|
8
|
-
|
9
|
-
# SPECS ===============================================================
|
10
|
-
|
11
|
-
desc 'Run specs with unit test style output'
|
12
|
-
task :test do |t|
|
13
|
-
sh "ruby test/*_test.rb"
|
14
|
-
end
|
15
|
-
|
16
|
-
desc 'Run bin/example and tryouts'
|
17
|
-
task :tryouts do |t|
|
18
|
-
sh "ruby bin/example"
|
19
|
-
end
|
20
|
-
|
21
|
-
# PACKAGE =============================================================
|
3
|
+
require 'rdoc/task'
|
22
4
|
|
23
|
-
|
24
|
-
load "#{name}.gemspec"
|
5
|
+
task default: :test
|
25
6
|
|
26
|
-
|
27
|
-
|
28
|
-
Rake::GemPackageTask.new(@spec) do |p|
|
29
|
-
p.need_tar = true if RUBY_PLATFORM !~ /mswin/
|
30
|
-
end
|
31
|
-
|
32
|
-
task :release => [ :rdoc, :package ]
|
7
|
+
# SPECS ===============================================================
|
33
8
|
|
34
|
-
|
35
|
-
|
9
|
+
desc 'Run tests'
|
10
|
+
task :test do
|
11
|
+
if File.exist?('spec') && system('which rspec > /dev/null 2>&1')
|
12
|
+
sh 'bundle exec rspec'
|
13
|
+
else
|
14
|
+
sh 'ruby test/*_test.rb'
|
15
|
+
end
|
36
16
|
end
|
37
17
|
|
38
|
-
|
39
|
-
|
18
|
+
desc 'Run bin/example and tryouts'
|
19
|
+
task :tryouts do
|
20
|
+
sh 'ruby bin/example'
|
40
21
|
end
|
41
22
|
|
23
|
+
# DOCUMENTATION =======================================================
|
42
24
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
25
|
+
RDoc::Task.new(:rdoc) do |t|
|
26
|
+
t.rdoc_dir = 'doc'
|
27
|
+
t.title = 'Drydock - Build seaworthy command-line apps'
|
28
|
+
t.options << '--line-numbers' << '--charset=utf-8'
|
29
|
+
t.rdoc_files.include('LICENSE.txt')
|
30
|
+
t.rdoc_files.include('README.md')
|
31
|
+
t.rdoc_files.include('CHANGES.txt')
|
32
|
+
t.rdoc_files.include('bin/*')
|
33
|
+
t.rdoc_files.include('lib/**/*.rb')
|
48
34
|
end
|
49
35
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
36
|
+
# DEVELOPMENT =========================================================
|
37
|
+
|
38
|
+
desc 'Run RuboCop'
|
39
|
+
task :rubocop do
|
40
|
+
sh 'bundle exec rubocop'
|
55
41
|
end
|
56
42
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
require 'rake/rdoctask'
|
43
|
+
desc 'Run RuboCop with auto-correct'
|
44
|
+
task 'rubocop:auto_correct' do
|
45
|
+
sh 'bundle exec rubocop -a'
|
61
46
|
end
|
62
47
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
t.options << '--line-numbers' << '-A cattr_accessor=object'
|
67
|
-
t.options << '--charset' << 'utf-8'
|
68
|
-
t.rdoc_files.include('LICENSE.txt')
|
69
|
-
t.rdoc_files.include('README.rdoc')
|
70
|
-
t.rdoc_files.include('CHANGES.txt')
|
71
|
-
t.rdoc_files.include('bin/*')
|
72
|
-
t.rdoc_files.include('lib/*.rb')
|
48
|
+
desc 'Install development dependencies'
|
49
|
+
task :setup do
|
50
|
+
sh 'bundle install'
|
73
51
|
end
|
74
52
|
|
75
|
-
|
53
|
+
# ALIASES =============================================================
|
76
54
|
|
55
|
+
task lint: :rubocop
|
56
|
+
task doc: :rdoc
|
77
57
|
|
58
|
+
# CLEAN ===============================================================
|
78
59
|
|
60
|
+
CLEAN.include ['pkg', '*.gem', '.config', 'doc', 'coverage']
|
data/bin/example
CHANGED
@@ -1,207 +1,203 @@
|
|
1
1
|
#!/usr/bin/ruby
|
2
2
|
|
3
3
|
# Seafaring Drydock Examples
|
4
|
-
#
|
5
|
-
# This is a functioning script so you can copy it, run it,
|
4
|
+
#
|
5
|
+
# This is a functioning script so you can copy it, run it,
|
6
6
|
# and just generally be a longshoreman about things. This is
|
7
|
-
# a drydock after all.
|
7
|
+
# a drydock after all.
|
8
8
|
#
|
9
9
|
# If you're reading this via the Rdocs you won't see the code. See:
|
10
10
|
#
|
11
11
|
# http://github.com/delano/drydock/blob/master/bin/example
|
12
12
|
#
|
13
|
-
# For an example of a complex command-line application using
|
13
|
+
# For an example of a complex command-line application using
|
14
14
|
# Drydock, see:
|
15
15
|
#
|
16
16
|
# http://github.com/solutious/rudy/blob/master/bin/rudy
|
17
17
|
#
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
require 'drydock'
|
19
|
+
require_relative '../lib/drydock'
|
22
20
|
|
23
21
|
module Example
|
24
|
-
extend Drydock # Tell Drydock you want its methods!
|
25
|
-
|
22
|
+
extend Drydock # Tell Drydock you want its methods!
|
23
|
+
|
26
24
|
default :welcome # The welcome command will be run if no command is given
|
27
25
|
capture :stderr # Drydock will capture STDERR and keep it in the hold.
|
28
|
-
|
26
|
+
# You can use this to suppress errors.
|
29
27
|
|
30
|
-
about
|
28
|
+
about 'A friendly welcome to the Drydock'
|
31
29
|
command :welcome do
|
32
|
-
puts
|
30
|
+
puts 'Welcome to Drydock.', $/
|
33
31
|
puts "For available commands: #{$0} show-commands"
|
34
32
|
end
|
35
33
|
|
36
34
|
usage "USAGE: #{$0} laugh [-f]"
|
37
|
-
about
|
38
|
-
option :f, :faster,
|
35
|
+
about 'The captain commands his crew to laugh'
|
36
|
+
option :f, :faster, 'A boolean value. Go even faster!'
|
39
37
|
command :laugh do |obj|
|
40
|
-
|
41
|
-
|
38
|
+
# +obj+ is an instance of Drydock::Command. The options you define are available
|
39
|
+
# via obj.option.name
|
42
40
|
|
43
|
-
answer = !obj.option.faster ?
|
41
|
+
answer = !obj.option.faster ? 'Sort of' : "Yes! I'm literally laughing as fast as possible."
|
44
42
|
|
45
|
-
puts
|
46
|
-
puts
|
43
|
+
puts 'Captain Stubing: Are you laughing?'
|
44
|
+
puts 'Dr. Bricker: ' << answer
|
47
45
|
end
|
48
46
|
|
49
47
|
global_usage "USAGE: #{File.basename($0)} [global options] command [command options]"
|
50
|
-
global :s, :seconds,
|
51
|
-
global :v, :verbose,
|
52
|
-
|
53
|
-
|
48
|
+
global :s, :seconds, 'Display values in seconds'
|
49
|
+
global :v, :verbose, 'Verbosity level (i.e. -vvv is greater than -v)' do |_v|
|
50
|
+
# Use instance variables to maintain values between option blocks.
|
51
|
+
# This will increment for every -v found (i.e. -vvv)
|
54
52
|
@val ||= 0
|
55
53
|
@val += 1
|
56
54
|
end
|
57
55
|
|
58
56
|
before do |obj|
|
59
|
-
|
60
|
-
|
61
|
-
|
57
|
+
# You can execute a block before the requests command is executed. Instance
|
58
|
+
# variables defined here will be available to all commands.
|
59
|
+
# +obj+ is a reference to the command object, just like in command blocks.
|
62
60
|
end
|
63
61
|
|
64
62
|
after do |obj|
|
65
|
-
|
63
|
+
# And this will be called after the command.
|
66
64
|
end
|
67
65
|
|
68
|
-
usage "#{$
|
69
|
-
about
|
70
|
-
command :date do |obj|
|
66
|
+
usage "#{$PROGRAM_NAME} [-s] [-vv] date"
|
67
|
+
about 'Display the current date'
|
68
|
+
command :date do |obj|
|
71
69
|
require 'time'
|
72
70
|
now = Time.now
|
73
|
-
puts
|
74
|
-
puts
|
75
|
-
puts
|
71
|
+
puts '(Not verbose enough. Try adding a -v.)' if (obj.global.verbose || 0) == 1
|
72
|
+
puts 'More verbosely, the date is now: ' if (obj.global.verbose || 0) >= 2
|
73
|
+
puts obj.global.seconds ? now.to_i : now.to_s
|
76
74
|
end
|
77
75
|
|
78
|
-
|
79
76
|
ignore :options
|
80
|
-
about
|
77
|
+
about 'This command ignores options'
|
81
78
|
command :rogue do |obj|
|
82
|
-
|
83
|
-
|
84
|
-
|
79
|
+
# You can use ignore :options to tell Drydock to not process the
|
80
|
+
# command-specific options.
|
81
|
+
# Unnamed arguments are available from obj.argv
|
85
82
|
if obj.argv.empty?
|
86
|
-
puts
|
83
|
+
puts 'Had you supplied some arguments, I would have ignored them.'
|
87
84
|
else
|
88
|
-
puts
|
85
|
+
puts 'Hi! You supplied some arguments but I ignored them.'
|
89
86
|
puts "They're all still here in this array: %s" % obj.argv.join(', ')
|
90
87
|
end
|
91
88
|
end
|
92
89
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
def ahoy
|
90
|
+
# You can write your own command classes by inheriting from Drydock::Command
|
91
|
+
# and referencing it in the command definition.
|
92
|
+
class JohnWestSmokedOysters < Drydock::Command
|
93
|
+
def ahoy!
|
94
|
+
p('matey')
|
95
|
+
end
|
97
96
|
end
|
98
97
|
|
99
98
|
about "Do something with John West's Smoked Oysters"
|
100
|
-
command :
|
101
|
-
p obj
|
99
|
+
command oysters: JohnWestSmokedOysters do |obj|
|
100
|
+
p obj # => #<JohnWestSmokedOysters:0x42179c ... >
|
102
101
|
end
|
103
102
|
|
104
|
-
about
|
105
|
-
command [
|
103
|
+
about 'My way of saying hello!'
|
104
|
+
command %i[ahoy! hello!] => JohnWestSmokedOysters
|
106
105
|
# If you don't provide a block, Drydock will call JohnWestSmokedOysters#ahoy!
|
107
106
|
|
108
|
-
|
109
107
|
require 'yaml'
|
110
108
|
|
111
109
|
usage 'ruby bin/example uri -c -d " " -t 15 http://solutious.com/'
|
112
110
|
usage 'echo "http://solutious.com/" | ruby bin/example uri -c -d " " -t 15'
|
113
|
-
about
|
114
|
-
option :c, :check,
|
115
|
-
option :d, :delim, String,
|
116
|
-
option :t, :timeout, Float,
|
117
|
-
# You can provide an block to process the option value.
|
118
|
-
# This block must return the final value.
|
119
|
-
v = 10 if
|
111
|
+
about 'Check for broken URIs'
|
112
|
+
option :c, :check, 'Check response codes for each URI'
|
113
|
+
option :d, :delim, String, 'Output delimiter'
|
114
|
+
option :t, :timeout, Float, 'Timeout value for HTTP request' do |v|
|
115
|
+
# You can provide an block to process the option value.
|
116
|
+
# This block must return the final value.
|
117
|
+
v = 10 if v > 10
|
120
118
|
v
|
121
119
|
end
|
122
120
|
argv :uris
|
123
|
-
|
121
|
+
|
124
122
|
command :uri do |obj|
|
125
|
-
|
126
|
-
|
127
|
-
|
123
|
+
# This command processes the output of the stdin block (below this definition).
|
124
|
+
# The output of that block is available as obj.stdin. If there is no stdin block
|
125
|
+
# obj.stdin will be STDIN's IO object.
|
128
126
|
|
129
127
|
require 'net/http'
|
130
128
|
require 'uri'
|
131
129
|
require 'timeout'
|
132
|
-
|
130
|
+
|
133
131
|
uris = []
|
134
132
|
uris += obj.stdin if obj.stdin
|
135
133
|
uris += obj.argv.uris if obj.argv.uris
|
136
|
-
|
134
|
+
|
137
135
|
delim = obj.option.delim || ','
|
138
136
|
timeout = obj.option.timeout || 5
|
139
|
-
code = :notchecked
|
140
|
-
|
137
|
+
code = :notchecked # The default code when :check is false
|
138
|
+
|
141
139
|
if uris.empty?
|
142
140
|
puts "Frylock: You didn't provide any URIs. "
|
143
141
|
puts "Master Shake: Ya, see #{$0} #{obj.alias} -h"
|
144
142
|
exit 0
|
145
143
|
end
|
146
|
-
|
144
|
+
|
147
145
|
uris.each_with_index do |uri, index|
|
148
|
-
code = response_code(uri, timeout) if
|
149
|
-
puts [index+1, uri, code].join(delim)
|
146
|
+
code = response_code(uri, timeout) if obj.option.check
|
147
|
+
puts [index + 1, uri, code].join(delim)
|
150
148
|
end
|
151
|
-
|
152
149
|
end
|
153
150
|
|
154
|
-
about
|
155
|
-
# We can define command aliases by providing a list of command
|
151
|
+
about 'Prints the alias used to access the command'
|
152
|
+
# We can define command aliases by providing a list of command
|
156
153
|
# names. The first name is still consider to be the main name.
|
157
154
|
command :printalias, :reveal do |obj|
|
158
|
-
puts
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
155
|
+
puts 'This is printalias!'
|
156
|
+
if obj.alias == obj.cmd
|
157
|
+
puts 'You did not use an alias'
|
158
|
+
else
|
159
|
+
puts 'You used the alias ' << obj.alias
|
160
|
+
end
|
164
161
|
end
|
165
|
-
|
162
|
+
|
166
163
|
stdin do |stdin, output|
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
# We only want piped data. If this is not included
|
164
|
+
# Pre-process STDIN for all commands. This example returns an array of lines.
|
165
|
+
# The command processuris uses this array.
|
166
|
+
|
167
|
+
# We only want piped data. If this is not included
|
171
168
|
# execution will wait for input from the user.
|
172
|
-
unless stdin.tty?
|
173
|
-
|
174
|
-
|
169
|
+
unless stdin.tty?
|
170
|
+
|
171
|
+
until stdin.eof?
|
175
172
|
line = stdin.readline
|
176
173
|
line.chomp!
|
177
174
|
(output ||= []) << line
|
178
175
|
end
|
179
|
-
|
176
|
+
|
180
177
|
end
|
181
178
|
output
|
182
179
|
end
|
183
180
|
|
184
|
-
|
185
181
|
# And one final feature for the intrepid swabbies like myself.
|
186
|
-
# Drydock can handle unknown commands by catching them with a
|
182
|
+
# Drydock can handle unknown commands by catching them with a
|
187
183
|
# trawler. It's like the captain of all aliases. Just specify
|
188
184
|
# the command name to direct all unknown commands to. Simple!
|
189
185
|
trawler :printalias
|
190
|
-
|
191
|
-
|
192
|
-
# Return the HTTP response code for the given URI. Used by
|
186
|
+
|
187
|
+
# Return the HTTP response code for the given URI. Used by
|
193
188
|
# uri command.
|
194
189
|
#
|
195
190
|
# +uri+ A valid HTTP URI
|
196
|
-
# +duration+ The timeout threshold (in seconds) for the request.
|
197
|
-
def response_code(uri_str, duration=5)
|
191
|
+
# +duration+ The timeout threshold (in seconds) for the request.
|
192
|
+
def response_code(uri_str, duration = 5) # :nodoc:
|
198
193
|
response = :unavailable
|
199
|
-
begin
|
200
|
-
uri =
|
194
|
+
begin
|
195
|
+
uri = uri_str.is_a?(URI::HTTP) ? uri_str : URI.parse(uri_str)
|
201
196
|
timeout(duration) do
|
202
197
|
response = Net::HTTP.get_response(uri).code
|
203
|
-
end
|
204
|
-
rescue
|
198
|
+
end
|
199
|
+
rescue StandardError => e
|
200
|
+
puts "Error: #{e.message}"
|
205
201
|
end
|
206
202
|
response
|
207
203
|
end
|