getopt-declare 1.09.7
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/lib/Getopt/Declare.rb +1638 -0
- data/lib/Getopt/DelimScanner.rb +1278 -0
- data/samples/cmdline_array.rb +25 -0
- data/samples/cmdline_basic.rb +31 -0
- data/samples/cmdline_code.rb +31 -0
- data/samples/cmdline_defer.rb +23 -0
- data/samples/cmdline_file.rb +38 -0
- data/samples/cmdline_inlines.rb +24 -0
- data/samples/cmdline_mid.rb +39 -0
- data/samples/cmdline_noargv.rb +29 -0
- data/samples/cmdline_parameters.rb +23 -0
- data/samples/cmdline_pvtype.rb +20 -0
- data/samples/cmdline_pvtype2.rb +20 -0
- data/samples/cmdline_regex.rb +27 -0
- data/samples/cmdline_singles.rb +28 -0
- data/samples/demo_cmdline.rb +70 -0
- data/samples/demo_csv.rb +49 -0
- data/samples/demo_interp.rb +44 -0
- data/samples/demo_shell.rb +37 -0
- metadata +55 -0
@@ -0,0 +1,25 @@
|
|
1
|
+
#! /usr/bin/ruby
|
2
|
+
|
3
|
+
require "Getopt/Declare"
|
4
|
+
|
5
|
+
args = Getopt::Declare.new( %q{
|
6
|
+
|
7
|
+
-v <value> [etc] One or more values
|
8
|
+
<infile> Input file [required]
|
9
|
+
-o <outfiles>... Output files
|
10
|
+
} )
|
11
|
+
|
12
|
+
if args['-v']
|
13
|
+
print "Using value: ", args['-v']['<value>']
|
14
|
+
print " (etcetera)" if args['-v']['etc']
|
15
|
+
print "\n"
|
16
|
+
end
|
17
|
+
|
18
|
+
infile = File.new( args['<infile>'] )
|
19
|
+
data = infile
|
20
|
+
|
21
|
+
for outfile in args['-o']
|
22
|
+
#outfile = File.new(outfile,'w')
|
23
|
+
print "processed ",outfile
|
24
|
+
#outfile.close
|
25
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
require "Getopt/Declare"
|
4
|
+
|
5
|
+
args = Getopt::Declare.new(<<'EOPARAM')
|
6
|
+
|
7
|
+
============================================================
|
8
|
+
Required parameter:
|
9
|
+
|
10
|
+
-in <infile> Input file [required]
|
11
|
+
|
12
|
+
------------------------------------------------------------
|
13
|
+
|
14
|
+
Optional parameters:
|
15
|
+
|
16
|
+
(The first two are mutually exclusive) [mutex: -r -p]
|
17
|
+
|
18
|
+
-r[and[om]] Output in random order
|
19
|
+
-p[erm[ute]] Output all permutations
|
20
|
+
|
21
|
+
---------------------------------------------------
|
22
|
+
|
23
|
+
-out <outfile> Optional output file
|
24
|
+
|
25
|
+
------------------------------------------------------------
|
26
|
+
Note: this program is known to run very slowly of files with
|
27
|
+
long individual lines.
|
28
|
+
============================================================
|
29
|
+
EOPARAM
|
30
|
+
|
31
|
+
print args.inspect
|
@@ -0,0 +1,31 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
require "Getopt/Declare"
|
4
|
+
|
5
|
+
args = Getopt::Declare.new(<<'EOPARAM', ['-BUILD'])
|
6
|
+
|
7
|
+
============================================================
|
8
|
+
Required parameter:
|
9
|
+
|
10
|
+
-in <infile> Input file [required]
|
11
|
+
|
12
|
+
------------------------------------------------------------
|
13
|
+
|
14
|
+
Optional parameters:
|
15
|
+
|
16
|
+
(The first two are mutually exclusive) [mutex: -r -p]
|
17
|
+
|
18
|
+
-r[and[om]] Output in random order
|
19
|
+
-p[erm[ute]] Output all permutations
|
20
|
+
|
21
|
+
---------------------------------------------------
|
22
|
+
|
23
|
+
-out <outfile> Optional output file
|
24
|
+
|
25
|
+
------------------------------------------------------------
|
26
|
+
Note: this program is known to run very slowly of files with
|
27
|
+
long individual lines.
|
28
|
+
============================================================
|
29
|
+
EOPARAM
|
30
|
+
|
31
|
+
puts args.code()
|
@@ -0,0 +1,23 @@
|
|
1
|
+
#! /usr/bin/ruby
|
2
|
+
|
3
|
+
require "Getopt/Declare"
|
4
|
+
|
5
|
+
|
6
|
+
# With the next specification, the <tt>-rev</tt> and/or <tt>-rand</tt> flags
|
7
|
+
# can be specified _after_ the list of files, but still affect the processing
|
8
|
+
# of those files. Moreover, if the command-line parsing fails for some reason
|
9
|
+
# (perhaps due to an unrecognized argument), the deferred processing will
|
10
|
+
# not be performed.
|
11
|
+
args = Getopt::Declare.new( %q{
|
12
|
+
|
13
|
+
<files>... Files to be processed
|
14
|
+
{ defer { files.each { |i|
|
15
|
+
puts i, " ",$ordered } } }
|
16
|
+
|
17
|
+
-rev[erse] Process in reverse order
|
18
|
+
{ $ordered = -1; }
|
19
|
+
|
20
|
+
-rand[om] Process in random order
|
21
|
+
{ $ordered = 0; }
|
22
|
+
} )
|
23
|
+
|
@@ -0,0 +1,38 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
require "Getopt/Declare"
|
4
|
+
|
5
|
+
args = Getopt::Declare.new(<<'EOPARAM', ['-BUILD'])
|
6
|
+
|
7
|
+
============================================================
|
8
|
+
Required parameter:
|
9
|
+
|
10
|
+
-in <infile> Input file [required]
|
11
|
+
|
12
|
+
------------------------------------------------------------
|
13
|
+
|
14
|
+
Optional parameters:
|
15
|
+
|
16
|
+
(The first two are mutually exclusive) [mutex: -r -p]
|
17
|
+
|
18
|
+
-r[and[om]] Output in random order
|
19
|
+
-p[erm[ute]] Output all permutations
|
20
|
+
|
21
|
+
---------------------------------------------------
|
22
|
+
|
23
|
+
-out <outfile> Optional output file
|
24
|
+
|
25
|
+
------------------------------------------------------------
|
26
|
+
Note: this program is known to run very slowly of files with
|
27
|
+
long individual lines.
|
28
|
+
============================================================
|
29
|
+
EOPARAM
|
30
|
+
|
31
|
+
|
32
|
+
# TRY STANDARD CONFIG FILES
|
33
|
+
args.parse(['-CONFIG'])
|
34
|
+
#args.parse('/usr/local/config/.demo_rc')
|
35
|
+
|
36
|
+
#args.parse() or raise "cannot parse";
|
37
|
+
|
38
|
+
print args.inspect
|
@@ -0,0 +1,24 @@
|
|
1
|
+
|
2
|
+
require "Getopt/Declare"
|
3
|
+
|
4
|
+
def encode(t)
|
5
|
+
return Getopt::Declare.new(t,['-BUILD']).code
|
6
|
+
end
|
7
|
+
|
8
|
+
=begin
|
9
|
+
Just type in something, like:
|
10
|
+
|
11
|
+
=for Getopt::Declare
|
12
|
+
|
13
|
+
-a Append mode
|
14
|
+
=cut
|
15
|
+
|
16
|
+
=end
|
17
|
+
|
18
|
+
$/ = '=cut'
|
19
|
+
if t = $stdin.readline
|
20
|
+
t.sub!( /^=for\s+Getopt::Declare\s*\n(.*?)\n=cut/esm ) {
|
21
|
+
'(self,source) = []'+encode("#$1") }
|
22
|
+
end
|
23
|
+
|
24
|
+
print t
|
@@ -0,0 +1,39 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
require "Getopt/Declare"
|
4
|
+
|
5
|
+
def delold
|
6
|
+
print "would have deleted\n"
|
7
|
+
end
|
8
|
+
|
9
|
+
args = Getopt::Declare.new(<<EOF)
|
10
|
+
|
11
|
+
-a Process all data
|
12
|
+
|
13
|
+
-b <n:n> Set mean byte length threshold to <N>
|
14
|
+
{ bytelen = n }
|
15
|
+
|
16
|
+
+c <FILE> Create new file <FILE>
|
17
|
+
|
18
|
+
--del Delete old file
|
19
|
+
{ delold(); }
|
20
|
+
|
21
|
+
delete [ditto]
|
22
|
+
|
23
|
+
e <w:i>x<h:i> Expand image to height <h> and width <w>
|
24
|
+
{ }
|
25
|
+
|
26
|
+
-F <file>... Process named file(s)
|
27
|
+
{ defer { file.each {|i|
|
28
|
+
process(i) } } }
|
29
|
+
|
30
|
+
=getrand [<n>] Get a random number
|
31
|
+
(or, optionally, <n> of them)
|
32
|
+
{ n = 1 unless !n.empty? }
|
33
|
+
|
34
|
+
-- Traditionally indicates end of arguments
|
35
|
+
{ finish }
|
36
|
+
|
37
|
+
EOF
|
38
|
+
|
39
|
+
print args.inspect
|
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
require "Getopt/Declare"
|
4
|
+
|
5
|
+
argv2 = '-in test'
|
6
|
+
|
7
|
+
# Parse options from another string instead of ARGV
|
8
|
+
args = Getopt::Declare.new(<<'EOPARAM', argv2 )
|
9
|
+
|
10
|
+
============================================================
|
11
|
+
-in <infile> Input file
|
12
|
+
-r[and[om]] Output in random order
|
13
|
+
-p[erm[ute]] Output all permutations
|
14
|
+
-out <outfile> Optional output file
|
15
|
+
============================================================
|
16
|
+
EOPARAM
|
17
|
+
|
18
|
+
puts args.inspect
|
19
|
+
|
20
|
+
# Parse options from another string instead of ARGV
|
21
|
+
argv3 = "-r -in argv3"
|
22
|
+
args.parse(argv3)
|
23
|
+
puts args.inspect
|
24
|
+
|
25
|
+
# Parse options from another array instead of ARGV
|
26
|
+
argv4 = [ '-in', 'opt with spaces' ]
|
27
|
+
|
28
|
+
args.parse(['-ARGV', argv4])
|
29
|
+
puts args.inspect
|
@@ -0,0 +1,23 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
require "Getopt/Declare"
|
4
|
+
|
5
|
+
specification = %q%
|
6
|
+
-list <l>... ttt
|
7
|
+
-out <out> ttt
|
8
|
+
in=<infile> ttt
|
9
|
+
+range <from>..<to> ttt
|
10
|
+
--lines <start> - <stop> ttt
|
11
|
+
ignore bad lines ttt
|
12
|
+
<outfile> ttt
|
13
|
+
-copy <files>... <dir> copy files to dir
|
14
|
+
-range <from:i> [[..] [<to:i>] ] opt. range
|
15
|
+
{ to = 10 if to == "" }
|
16
|
+
|
17
|
+
%
|
18
|
+
|
19
|
+
|
20
|
+
args = Getopt::Declare.new(specification)
|
21
|
+
|
22
|
+
|
23
|
+
puts args.inspect
|
@@ -0,0 +1,20 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
require "Getopt/Declare"
|
4
|
+
|
5
|
+
args = Getopt::Declare.new( <<'EOPARAM' )
|
6
|
+
[pvtype: type /AB|[OAB]/ ]
|
7
|
+
[pvtype: Rh? /Rh[+-]/ ]
|
8
|
+
[pvtype: days :+i {
|
9
|
+
reject( (_VAL_.to_i < 14)," #{_PARAM_} (too soon!)" )
|
10
|
+
}
|
11
|
+
]
|
12
|
+
|
13
|
+
-donated <d:days> Days since last donation
|
14
|
+
-applied <a:days> Days since applied to donate
|
15
|
+
|
16
|
+
-blood <type:type> [<rh:Rh?>] Specify blood type
|
17
|
+
and (optionally) rhesus factor
|
18
|
+
EOPARAM
|
19
|
+
|
20
|
+
print args.inspect
|
@@ -0,0 +1,20 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
require "Getopt/Declare"
|
4
|
+
|
5
|
+
specification = %q(
|
6
|
+
[strict]
|
7
|
+
[pvtype: num /\d+/ { reject if Time.new.localtime.day==3 } ]
|
8
|
+
[pvtype: 'a num' :n { puts "a num!" } ]
|
9
|
+
[pvtype: %q{nbr} :'a num' { reject $no_nbr } ]
|
10
|
+
|
11
|
+
-count1 <n:num> test
|
12
|
+
-count2 <n:a num> test2
|
13
|
+
-count3 <n:nbr> test3
|
14
|
+
|
15
|
+
)
|
16
|
+
|
17
|
+
args = Getopt::Declare.new(specification)
|
18
|
+
|
19
|
+
print args.inspect
|
20
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
require "Getopt/Declare"
|
4
|
+
|
5
|
+
specification = <<-'EOFPARAMS'
|
6
|
+
-ar <r:n> Set aspect ratio (will be clipped to [0..1] )
|
7
|
+
{
|
8
|
+
r = 0 if r < 0
|
9
|
+
r = 1 if r > 1
|
10
|
+
}
|
11
|
+
-w <pixels:+i> Specify width in pixels
|
12
|
+
-h <pixels:+i> Specify height in pixels
|
13
|
+
-list <all:i>... list of numbers
|
14
|
+
-range <from:i> [- [<to:i>] ] opt. range
|
15
|
+
{ to = 10 if to.empty? }
|
16
|
+
-parity <p:/even|odd|both/> Set parity (<p> must be "even",
|
17
|
+
"odd" or "both")
|
18
|
+
-file <name:/\w*\.[A-Z]{3}/> File name must have a three-
|
19
|
+
capital-letter extension
|
20
|
+
-find <what:/(%T.)+/> ; look ahead test
|
21
|
+
|
22
|
+
EOFPARAMS
|
23
|
+
|
24
|
+
|
25
|
+
args = Getopt::Declare.new(specification)
|
26
|
+
|
27
|
+
print args.inspect
|
@@ -0,0 +1,28 @@
|
|
1
|
+
#! /usr/bin/ruby
|
2
|
+
|
3
|
+
require "Getopt/Declare"
|
4
|
+
|
5
|
+
# In the next example, only the <tt>-a</tt> and <tt>-b</tt> parameters may be clustered.
|
6
|
+
# The <tt>-bu</tt> parameter is excluded because it consists of more than one
|
7
|
+
# letter, whilst the <tt>-c</tt> and <tt>-d</tt> parameters are excluded because they
|
8
|
+
# take (or may take, in <tt>-d</tt>'s case) a variable. The <tt>-e[xec]</tt> parameter
|
9
|
+
# is excluded because it may take a trailing punctuator (<tt>[xec]</tt>).
|
10
|
+
#
|
11
|
+
# By comparison, if the directive had been <tt>[cluster: flags]</tt>, then
|
12
|
+
# <tt>-bu</tt> _could_ be clustered, though <tt>-c</tt>, <tt>-d</tt> and <tt>-e[xec]</tt> would
|
13
|
+
# still be excluded since they are not "pure flags").
|
14
|
+
#
|
15
|
+
|
16
|
+
args = Getopt::Declare.new( <<-'EOSPEC' )
|
17
|
+
|
18
|
+
-a Append mode
|
19
|
+
-b Back-up mode
|
20
|
+
-bu [ditto]
|
21
|
+
-c <file> Copy mode
|
22
|
+
-d [<file>] Delete mode
|
23
|
+
-e[xec] Execute mode
|
24
|
+
|
25
|
+
[cluster:singles]
|
26
|
+
EOSPEC
|
27
|
+
|
28
|
+
print args.inspect
|
@@ -0,0 +1,70 @@
|
|
1
|
+
#! /usr/bin/ruby
|
2
|
+
|
3
|
+
require "Getopt/Declare"
|
4
|
+
|
5
|
+
$VERSION = "1.00b";
|
6
|
+
|
7
|
+
config = Getopt::Declare.new( <<'EOCONFIG', ['-CONFIG']);
|
8
|
+
[strict]
|
9
|
+
min = <n> Minimum value [required]
|
10
|
+
max = <n> Maximum value
|
11
|
+
|
12
|
+
EOCONFIG
|
13
|
+
|
14
|
+
print "min: ", config['min'], "\n" if config['min']
|
15
|
+
print "max: ", config['max'], "\n" if config['max']
|
16
|
+
|
17
|
+
args = Getopt::Declare.new( <<'EOARGS' )
|
18
|
+
General options:
|
19
|
+
[tight]
|
20
|
+
-e <f:i>..<t:i> Set expansion factor to specified range
|
21
|
+
[requires: <file>]
|
22
|
+
{ puts "k = [#{f}..#{t}]" }
|
23
|
+
|
24
|
+
-e [<k:n>...] Set expansion factor to <k> (or 2 by default)
|
25
|
+
[required]
|
26
|
+
{ k = [2] unless k
|
27
|
+
print "k = [", k.join(','), "]\n"; }
|
28
|
+
|
29
|
+
-b <blen:i> Use byte length of <blen>
|
30
|
+
[excludes: -a +c]
|
31
|
+
{ print "byte len: #{blen}\n"; }
|
32
|
+
|
33
|
+
<file>... Process files [required] [implies: -a]
|
34
|
+
{ print "files: #{file}\n"; }
|
35
|
+
|
36
|
+
-a [<n:n>] Process all data [except item <n>]
|
37
|
+
{ print "proc all\n"; print "except #{n}\n" if n }
|
38
|
+
|
39
|
+
-fab The fabulous option (is always required :-)
|
40
|
+
[required]
|
41
|
+
{ defer { print "fabulous!\n" } }
|
42
|
+
|
43
|
+
File creation options:
|
44
|
+
|
45
|
+
+c <file> Create file [mutex: +c -a]
|
46
|
+
{ print "create: file\n"; }
|
47
|
+
|
48
|
+
+d <file> Duplicate file [implies: -a and -b 8]
|
49
|
+
This is a second line
|
50
|
+
{ print "dup (+d) $file\n"; }
|
51
|
+
--dup <file> [ditto] (long form)
|
52
|
+
|
53
|
+
-how <n:i> Set height to <n> [repeatable]
|
54
|
+
|
55
|
+
Garbling options:
|
56
|
+
|
57
|
+
-g [<seed:i>] Garble output with optional seed [requires: +c]
|
58
|
+
{ print "garbling with $seed\n"; }
|
59
|
+
-i Case insensitive garbling [required]
|
60
|
+
{ print "insensitive\n"; }
|
61
|
+
-s Case sensitive garbling
|
62
|
+
-w WaReZ m0De 6aRBL1N6
|
63
|
+
|
64
|
+
[mutex: -i -s -w]
|
65
|
+
EOARGS
|
66
|
+
|
67
|
+
print args.unused
|
68
|
+
|
69
|
+
#args.usage();
|
70
|
+
__END__
|