usage 0.0.3 → 0.0.4
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.
- data/CHANGES +10 -0
- data/README +3 -0
- data/lib/Usage.rb +51 -0
- data/samples/Sample1.rb +1 -1
- data/samples/Sample2.rb +1 -1
- data/samples/Sample3.rb +1 -1
- data/samples/Sample4.rb +1 -1
- data/samples/Sample5.rb +1 -1
- data/samples/Sample6.rb +1 -1
- data/samples/Sample7.rb +1 -1
- data/samples/Sample8.rb +1 -1
- data/samples/sample10.rb +1 -1
- data/samples/sample11.rb +1 -1
- data/samples/sample12.rb +8 -0
- data/samples/sample9.rb +1 -1
- metadata +3 -2
data/CHANGES
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
======== Version 0.4 ==============================================================
|
2
|
+
|
3
|
+
This version adds open-uri support. This adds a new built-in file type:
|
4
|
+
|
5
|
+
<@ - Either a local file or a URI that is http:// or ftp://. Also, filenames that
|
6
|
+
start with www. are prepended automatically with http:// and filenames that
|
7
|
+
start with ftp. are prepended automatically with ftp://
|
8
|
+
|
9
|
+
Added sample12.rb to show how this new type works.
|
10
|
+
|
1
11
|
======== Version 0.3 ==============================================================
|
2
12
|
|
3
13
|
The biggest change in version 0.3 is that the argument type architecture is
|
data/README
CHANGED
@@ -124,6 +124,9 @@ long, long ago). Note: These can be replaced or added to.
|
|
124
124
|
>> - A file that will be appended to
|
125
125
|
>? - A file that will be written to but the user is prompted if it already exists
|
126
126
|
>>? - A file that will be appended to but the user is prompted if the file doesn't exist
|
127
|
+
<@ - Either a local file or a URI that is http:// or ftp://. Also, filenames that
|
128
|
+
start with www. are prepended automatically with http:// and filenames that
|
129
|
+
start with ftp. are prepended automatically with ftp://
|
127
130
|
|
128
131
|
So when you send the argument message to the usage object, you will get a value of that
|
129
132
|
type and if the user does not give that type, then they get an error message.
|
data/lib/Usage.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require "rubygems"
|
2
2
|
require_gem "SimpleTrace", "<= 0.1.0"
|
3
|
+
require "open-uri"
|
3
4
|
|
4
5
|
#
|
5
6
|
# Use module to hide usage classes
|
@@ -116,6 +117,22 @@ class FileAppendDoesntExistError < Error
|
|
116
117
|
end
|
117
118
|
end
|
118
119
|
|
120
|
+
class IOInputError < Error
|
121
|
+
attr_reader :uri, :msg
|
122
|
+
def initialize(uri, msg)
|
123
|
+
@uri = uri
|
124
|
+
super("error opening input: '#{uri}' because #{msg}")
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
class OpenURIBadFormatError < Error
|
129
|
+
attr_reader :uri
|
130
|
+
def initialize(uri)
|
131
|
+
@uri = uri
|
132
|
+
super("URI must be of the form: http:// or ftp://, not '#{uri}'")
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
119
136
|
# ------------------------------- Parsing Errors -----------------------------
|
120
137
|
|
121
138
|
#
|
@@ -505,6 +522,39 @@ class FileReadLinesPlugin < ArgumentParserPlugin
|
|
505
522
|
end
|
506
523
|
end
|
507
524
|
|
525
|
+
class OpenURIInputPlugin < FilePlugin
|
526
|
+
def initialize(usage_ui, str)
|
527
|
+
$TRACE.debug 5, "trying to open uri '#{str}' for input"
|
528
|
+
|
529
|
+
# see if it looks like a URI
|
530
|
+
m = /^(\w+):\/\//.match(str)
|
531
|
+
|
532
|
+
# if it matches and its not either http or ftp (which is all open-uri
|
533
|
+
# does at this point
|
534
|
+
if m && !(["http", "ftp"].include?(m[1].downcase)) then
|
535
|
+
# tell the user
|
536
|
+
raise OpenURIBadFormatError.new(str)
|
537
|
+
end
|
538
|
+
|
539
|
+
# if it starts with www. or ftp. then just pre-pend the transport protocol
|
540
|
+
if m = /^(www|ftp)\./i.match(str) then
|
541
|
+
if m[1].downcase == "www" then
|
542
|
+
str = "http://" + str
|
543
|
+
else
|
544
|
+
str = "ftp://" + str
|
545
|
+
end
|
546
|
+
end
|
547
|
+
|
548
|
+
# try opening it with open-uri
|
549
|
+
begin
|
550
|
+
@value = open(str)
|
551
|
+
rescue Exception => e
|
552
|
+
puts e.message
|
553
|
+
raise IOInputError.new(str, e.message)
|
554
|
+
end
|
555
|
+
end
|
556
|
+
end
|
557
|
+
|
508
558
|
#
|
509
559
|
# This is the class that does the heavy lifting for the Usage class. It parses the
|
510
560
|
# usage string, options string and makes the information available by using
|
@@ -529,6 +579,7 @@ class Base
|
|
529
579
|
add_type_handler(">?", FileOutputQueryPlugin)
|
530
580
|
add_type_handler("<", FileInputPlugin)
|
531
581
|
add_type_handler(">", FileOutputPlugin)
|
582
|
+
add_type_handler("<@", OpenURIInputPlugin)
|
532
583
|
end
|
533
584
|
end
|
534
585
|
|
data/samples/Sample1.rb
CHANGED
data/samples/Sample2.rb
CHANGED
data/samples/Sample3.rb
CHANGED
data/samples/Sample4.rb
CHANGED
data/samples/Sample5.rb
CHANGED
data/samples/Sample6.rb
CHANGED
data/samples/Sample7.rb
CHANGED
data/samples/Sample8.rb
CHANGED
data/samples/sample10.rb
CHANGED
data/samples/sample11.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
# This sample takes the input file, sorts and writes it to the output
|
3
3
|
# It demonstrates the use of readlines to read in the input file.
|
4
4
|
#
|
5
|
-
require "
|
5
|
+
require "usage"
|
6
6
|
|
7
7
|
Usage.new "<<infile >outfile" do |usage|
|
8
8
|
usage.outfile.write(usage.infile.sort.join(""))
|
data/samples/sample12.rb
ADDED
data/samples/sample9.rb
CHANGED
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.3
|
|
3
3
|
specification_version: 1
|
4
4
|
name: usage
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.0.
|
7
|
-
date: 2005-10-
|
6
|
+
version: 0.0.4
|
7
|
+
date: 2005-10-17
|
8
8
|
summary: This module implements a simple no thought command line option parser
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -35,6 +35,7 @@ files:
|
|
35
35
|
- samples/Sample1.rb
|
36
36
|
- samples/sample10.rb
|
37
37
|
- samples/sample11.rb
|
38
|
+
- samples/sample12.rb
|
38
39
|
- samples/Sample2.rb
|
39
40
|
- samples/Sample3.rb
|
40
41
|
- samples/Sample4.rb
|