usage 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -1,7 +1,7 @@
1
1
  #
2
2
  # This is the simplist usage case (only required args)
3
3
  #
4
- require "Usage"
4
+ require "usage"
5
5
 
6
6
  usage = Usage.new "infile outfile"
7
7
 
data/samples/Sample2.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  #
2
2
  # This is illustrates handling infinite arguments
3
3
  #
4
- require "Usage"
4
+ require "usage"
5
5
 
6
6
  usage = Usage.new "outfile infiles..."
7
7
 
data/samples/Sample3.rb CHANGED
@@ -5,7 +5,7 @@
5
5
  # #optional_arg returns the first argument and #required_arg returns the
6
6
  # second.
7
7
  #
8
- require "Usage"
8
+ require "usage"
9
9
 
10
10
  usage = Usage.new "required_arg [optional_arg] "
11
11
 
data/samples/Sample4.rb CHANGED
@@ -7,7 +7,7 @@
7
7
  # When the usage is reported to the user, the parenthesis are not included.
8
8
  #
9
9
 
10
- require "Usage"
10
+ require "usage"
11
11
 
12
12
  usage = Usage.new "[-y] [-x excluded_tags] (-z ztag) (-w warning_arg) files..."
13
13
 
data/samples/Sample5.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  #
2
2
  # This illustrates how to use long options (ie. --option)
3
3
  #
4
- require "Usage"
4
+ require "usage"
5
5
 
6
6
  usage = Usage.new "-x files...", <<EOT
7
7
  -x,--exclusive specifies exclusive access to the files
data/samples/Sample6.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  # starts with % indicates and Integer and a @ indicates a
4
4
  # date/time (a Time object is produced).
5
5
  #
6
- require "Usage"
6
+ require "usage"
7
7
 
8
8
  usage = Usage.new "%num_times @on_date"
9
9
 
data/samples/Sample7.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  #
2
2
  # This sample shows how to use choice arguments
3
3
  #
4
- require "Usage"
4
+ require "usage"
5
5
 
6
6
  usage = Usage.new "[-a coffee|tea|milk]"
7
7
 
data/samples/Sample8.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  #
2
2
  # This is an example of a simple copy program
3
3
  #
4
- require "Usage"
4
+ require "usage"
5
5
 
6
6
  Usage.new "<infile >outfile" do |usage|
7
7
  usage.outfile.write(usage.infile.read)
data/samples/sample10.rb CHANGED
@@ -2,7 +2,7 @@
2
2
  # This appends the input file to the output file and prompts the
3
3
  # user if the output file exists
4
4
  #
5
- require "Usage"
5
+ require "usage"
6
6
 
7
7
  Usage.new "<infile >>?outfile" do |usage|
8
8
  usage.outfile.write(usage.infile.read)
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 "Usage"
5
+ require "usage"
6
6
 
7
7
  Usage.new "<<infile >outfile" do |usage|
8
8
  usage.outfile.write(usage.infile.sort.join(""))
@@ -0,0 +1,8 @@
1
+ #
2
+ # This is an example of a copying where the input uses open-uri program
3
+ #
4
+ require "usage"
5
+
6
+ Usage.new "<@infile >outfile" do |usage|
7
+ usage.outfile.write(usage.infile.read)
8
+ end
data/samples/sample9.rb CHANGED
@@ -2,7 +2,7 @@
2
2
  # This copy program prompts the user if they wish to overwrite
3
3
  # the output file (if it exists)
4
4
  #
5
- require "Usage"
5
+ require "usage"
6
6
 
7
7
  Usage.new "<infile >?outfile" do |usage|
8
8
  usage.outfile.write(usage.infile.read)
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.3
7
- date: 2005-10-15
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