drydock 0.6.8 → 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.
Files changed (10) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGES.txt +28 -21
  3. data/LICENSE.txt +2 -2
  4. data/README.md +92 -0
  5. data/Rakefile +40 -54
  6. data/bin/example +92 -96
  7. data/drydock.gemspec +41 -37
  8. data/lib/drydock.rb +265 -281
  9. metadata +52 -48
  10. 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,9 +1,18 @@
1
1
  DRYDOCK, CHANGES
2
2
 
3
- #### 0.6.9 (2009-10-??) #############################
3
+ #### 1.0.0 (2025-07-14) #############################
4
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
+
10
+ #### 0.6.9 (2010-02-12) #############################
11
+
12
+ * CHANGE: Removed hanna dependency [Diego Elio 'Flameeyes' Pettenò]
5
13
  * CHANGE: Using autoload where appropriate.
6
14
 
15
+
7
16
  #### 0.6.8 (2009-09-15) #############################
8
17
 
9
18
  * FIXED: require 'thread' added to Drydock::Screen
@@ -19,24 +28,24 @@ DRYDOCK, CHANGES
19
28
  NOTE: Because at_exit has been removed, Drydock scripts will no longer
20
29
  run automatically. You can explicitly call the following at the bottom
21
30
  of your scripts:
22
-
31
+
23
32
  Drydock.run!(ARGV, STDIN) if Drydock.run?
24
33
 
25
- * CHANGE: Removed at_exit
34
+ * CHANGE: Removed at_exit
26
35
  * CHANGE: Drydock.run? will now return false if Drydock hasl already run
27
36
 
28
37
 
29
38
  #### 0.6.5 (2009-05-21) #############################
30
39
 
31
- * ADDED: "with_args" support for default command. When specified,
32
- 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
33
42
  short form. e.g. "script arg1 arg2" == "script cmdname arg1 arg2"
34
43
 
35
44
  #### 0.6.3 (2009-05-10) #############################
36
45
 
37
46
  * ADDED: show-commands now displays a note about which command is the default
38
47
  * CHANGE: Moved mixins to lib/drydock/mixins (so other projects can require 'drydock/mixins')
39
- * FIXED: Support for inline command aliases when specifying a class:
48
+ * FIXED: Support for inline command aliases when specifying a class:
40
49
  command [:name, :alias1, :alias2] => SomeClass
41
50
 
42
51
 
@@ -57,19 +66,19 @@ command [:name, :alias1, :alias2] => SomeClass
57
66
  * CHANGE: 'desc' is now 'about' (desc works, but it prints a notice)
58
67
  * CHANGE: I now recommend implementing the Drydock DSL in a module.
59
68
  bin/example was updated to reflect the change. This prevents Drydock
60
- keywords from being included in the global namespace.
69
+ keywords from being included in the global namespace.
61
70
  * ADDED: Inline commands aliases. command :cmd1, :cmd2 do; ...; end
62
- * ADDED: Unknown commands can be directed to a trawler.
71
+ * ADDED: Unknown commands can be directed to a trawler.
63
72
 
64
73
 
65
74
  #### 0.5.6 (2009-04-22) #############################
66
75
 
67
- * CHANGED: Interrupts now handled in rescue rather than a trap.
76
+ * CHANGED: Interrupts now handled in rescue rather than a trap.
68
77
  * ADDED: Drydock::ArgError and Drydock::OptError are rescued at runtime by default
69
78
 
70
79
  #### 0.5.5 (2009-04-19) #############################
71
80
 
72
- * CHANGED: Improved help screen formatting.
81
+ * CHANGED: Improved help screen formatting.
73
82
 
74
83
  #### 0.5.4 (2009-04-15) #############################
75
84
 
@@ -77,22 +86,22 @@ keywords from being included in the global namespace.
77
86
 
78
87
  #### 0.5.3 (2009-04-05) #############################
79
88
 
80
- * FIXED: Command actions were not being handled correctly. Added rdocs to
89
+ * FIXED: Command actions were not being handled correctly. Added rdocs to
81
90
  clarify the code.
82
91
 
83
92
  #### 0.5.2 (2009-04-04) #############################
84
93
 
85
- * 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
86
95
  command object (which gives them access to the globals and options)
87
96
  * CHANGE: The prep stuff in Drydock::Command#call is now split into a
88
- separate method: prepare so call no longer takes arguments.
89
- * 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.
90
99
 
91
100
 
92
101
  #### 0.5.1 (2009-03-15) #############################
93
102
 
94
103
  * FIXED: Prevent calling default command in at_exit when there's a LoadError.
95
- * FIXED: Exit gracefully when the application exits.
104
+ * FIXED: Exit gracefully when the application exits.
96
105
  * FIXED: Print command names with dashes rather than underscores
97
106
 
98
107
 
@@ -104,8 +113,8 @@ separate method: prepare so call no longer takes arguments.
104
113
  * ADDED: Named argv values.
105
114
  * CHANGE: argv are now part of the Command class (not passed to command blocks)
106
115
  * CHANGE: "project" now automatically requires the lowercase name of the project
107
- and gracefully continues if the require failed.
108
- * 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
109
118
  being executed. If a validation method is found it's executed and
110
119
  must return a true valid (it can also raise its own exceptions).
111
120
  * ADDED: command actions. These are boolean switches with a twist. Drydock looks
@@ -123,7 +132,7 @@ and sending to other methods manually.
123
132
  * ADDED: Tries to call obj.command if available when no block is supplied
124
133
  * ADDED: "show_commands" command built-in. Displays commands with descriptions
125
134
  * ADDED: A default usage help msg for every command: "#{$0} command-name"
126
- * ADDED: "usage" work multiple times for the same command.
135
+ * ADDED: "usage" work multiple times for the same command.
127
136
  * ADDED: "desc" method for per command descriptions
128
137
  * CHANGE: options are now stored as obj.option.name instead of obj.name
129
138
  * CHANGE: global options are now stored as obj.globals.name
@@ -143,7 +152,7 @@ and sending to other methods manually.
143
152
  #### 0.3 (2009-02-05) ###############################
144
153
 
145
154
  * Added support for custom Drydock::Commands objects
146
- * Global and command-specific options are now available as
155
+ * Global and command-specific options are now available as
147
156
  attributes of the Drydock::Commands class instance.
148
157
  * Automatic execution
149
158
  * Now in a single file (lib/drydock.rb)
@@ -155,5 +164,3 @@ and sending to other methods manually.
155
164
 
156
165
  * Initial release
157
166
  * Forked from bmizerany/frylock
158
-
159
-
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2008-2009 Delano Mandelbaum, Solutious Inc.
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,74 +1,60 @@
1
- require 'rubygems'
1
+ require 'bundler/gem_tasks'
2
2
  require 'rake/clean'
3
- require 'rake/gempackagetask'
4
- require 'hanna/rdoctask'
5
- require 'fileutils'
6
- include FileUtils
7
-
8
- task :default => :test
9
-
10
- # SPECS ===============================================================
11
-
12
- desc 'Run specs with unit test style output'
13
- task :test do |t|
14
- sh "ruby test/*_test.rb"
15
- end
3
+ require 'rdoc/task'
16
4
 
17
- desc 'Run bin/example and tryouts'
18
- task :tryouts do |t|
19
- sh "ruby bin/example"
20
- end
5
+ task default: :test
21
6
 
22
- # PACKAGE =============================================================
23
-
24
- name = "drydock"
25
- load "#{name}.gemspec"
26
-
27
- version = @spec.version
7
+ # SPECS ===============================================================
28
8
 
29
- Rake::GemPackageTask.new(@spec) do |p|
30
- p.need_tar = true if RUBY_PLATFORM !~ /mswin/
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
31
16
  end
32
17
 
33
- task :release => [ :rdoc, :package ]
34
-
35
- task :install => [ :rdoc, :package ] do
36
- sh %{sudo gem install pkg/#{name}-#{version}.gem}
18
+ desc 'Run bin/example and tryouts'
19
+ task :tryouts do
20
+ sh 'ruby bin/example'
37
21
  end
38
22
 
39
- task :uninstall => [ :clean ] do
40
- sh %{sudo gem uninstall #{name}}
41
- end
23
+ # DOCUMENTATION =======================================================
42
24
 
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')
34
+ end
43
35
 
44
- # Rubyforge Release / Publish Tasks ==================================
36
+ # DEVELOPMENT =========================================================
45
37
 
46
- desc 'Publish website to rubyforge'
47
- task 'publish:rdoc' => 'doc/index.html' do
48
- sh "scp -rp doc/* rubyforge.org:/var/www/gforge-projects/#{name}/"
38
+ desc 'Run RuboCop'
39
+ task :rubocop do
40
+ sh 'bundle exec rubocop'
49
41
  end
50
42
 
51
- task 'publish:gem' => [:package] do |t|
52
- sh <<-end
53
- rubyforge add_release -o Any -a CHANGES.txt -f -n README.rdoc #{name} #{name} #{@spec.version} pkg/#{name}-#{@spec.version}.gem &&
54
- rubyforge add_file -o Any -a CHANGES.txt -f -n README.rdoc #{name} #{name} #{@spec.version} pkg/#{name}-#{@spec.version}.tgz
55
- end
43
+ desc 'Run RuboCop with auto-correct'
44
+ task 'rubocop:auto_correct' do
45
+ sh 'bundle exec rubocop -a'
56
46
  end
57
47
 
58
-
59
- Rake::RDocTask.new do |t|
60
- t.rdoc_dir = 'doc'
61
- t.title = @spec.summary
62
- t.options << '--line-numbers' << '-A cattr_accessor=object'
63
- t.options << '--charset' << 'utf-8'
64
- t.rdoc_files.include('LICENSE.txt')
65
- t.rdoc_files.include('README.rdoc')
66
- t.rdoc_files.include('CHANGES.txt')
67
- t.rdoc_files.include('bin/*')
68
- t.rdoc_files.include('lib/*.rb')
48
+ desc 'Install development dependencies'
49
+ task :setup do
50
+ sh 'bundle install'
69
51
  end
70
52
 
71
- CLEAN.include [ 'pkg', '*.gem', '.config', 'doc' ]
53
+ # ALIASES =============================================================
72
54
 
55
+ task lint: :rubocop
56
+ task doc: :rdoc
73
57
 
58
+ # CLEAN ===============================================================
74
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
- $:.unshift File.expand_path(File.join(File.dirname(__FILE__), '..')), 'lib'
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
- # You can use this to suppress errors.
26
+ # You can use this to suppress errors.
29
27
 
30
- about "A friendly welcome to the Drydock"
28
+ about 'A friendly welcome to the Drydock'
31
29
  command :welcome do
32
- puts "Welcome to Drydock.", $/
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 "The captain commands his crew to laugh"
38
- option :f, :faster, "A boolean value. Go even 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
- # +obj+ is an instance of Drydock::Command. The options you define are available
41
- # via obj.option.name
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 ? "Sort of" : "Yes! I'm literally laughing as fast as possible."
41
+ answer = !obj.option.faster ? 'Sort of' : "Yes! I'm literally laughing as fast as possible."
44
42
 
45
- puts "Captain Stubing: Are you laughing?"
46
- puts "Dr. Bricker: " << answer
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, "Display values in seconds"
51
- global :v, :verbose, "Verbosity level (i.e. -vvv is greater than -v)" do |v|
52
- # Use instance variables to maintain values between option blocks.
53
- # This will increment for every -v found (i.e. -vvv)
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
- # You can execute a block before the requests command is executed. Instance
60
- # variables defined here will be available to all commands.
61
- # +obj+ is a reference to the command object, just like in command blocks.
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
- # And this will be called after the command.
63
+ # And this will be called after the command.
66
64
  end
67
65
 
68
- usage "#{$0} [-s] [-vv] date"
69
- about "Display the current date"
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 "(Not verbose enough. Try adding a -v.)" if (obj.global.verbose || 0) == 1
74
- puts "More verbosely, the date is now: " if (obj.global.verbose || 0) >= 2
75
- puts (obj.global.seconds) ? now.to_i : now.to_s
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 "This command ignores options"
77
+ about 'This command ignores options'
81
78
  command :rogue do |obj|
82
- # You can use ignore :options to tell Drydock to not process the
83
- # command-specific options.
84
- # Unnamed arguments are available from obj.argv
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 "Had you supplied some arguments, I would have ignored them."
83
+ puts 'Had you supplied some arguments, I would have ignored them.'
87
84
  else
88
- puts "Hi! You supplied some arguments but I ignored them."
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
- class JohnWestSmokedOysters < Drydock::Command
94
- # You can write your own command classes by inheriting from Drydock::Command
95
- # and referencing it in the command definition.
96
- def ahoy!; p "matey"; end
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 :oysters => JohnWestSmokedOysters do |obj|
101
- p obj # => #<JohnWestSmokedOysters:0x42179c ... >
99
+ command oysters: JohnWestSmokedOysters do |obj|
100
+ p obj # => #<JohnWestSmokedOysters:0x42179c ... >
102
101
  end
103
102
 
104
- about "My way of saying hello!"
105
- command [:ahoy!, :hello!] => JohnWestSmokedOysters
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 "Check for broken URIs"
114
- option :c, :check, "Check response codes for each URI"
115
- option :d, :delim, String, "Output delimiter"
116
- option :t, :timeout, Float, "Timeout value for HTTP request" do |v|
117
- # You can provide an block to process the option value.
118
- # This block must return the final value.
119
- v = 10 if (v > 10)
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
- # This command processes the output of the stdin block (below this definition).
126
- # The output of that block is available as obj.stdin. If there is no stdin block
127
- # obj.stdin will be STDIN's IO object.
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 # The default code when :check is false
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 (obj.option.check)
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 "Prints the alias used to access the command"
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 "This is printalias!"
159
- if (obj.alias == obj.cmd)
160
- puts "You did not use an alias"
161
- else
162
- puts "You used the alias " << obj.alias
163
- end
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
- # Pre-process STDIN for all commands. This example returns an array of lines.
168
- # The command processuris uses this array.
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
- while !stdin.eof? do
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) #:nodoc:
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 = (uri_str.kind_of? URI::HTTP) ? uri_str : URI.parse(uri_str)
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 Exception => ex
198
+ end
199
+ rescue StandardError => e
200
+ puts "Error: #{e.message}"
205
201
  end
206
202
  response
207
203
  end