getopt 1.4.2 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,122 @@
1
+ ###################################################################
2
+ # test_getopt_std.rb
3
+ #
4
+ # Test suite for the Getopt::Std class. You should run this test
5
+ # via the 'rake test' task.
6
+ ###################################################################
7
+ require 'rspec'
8
+ require 'getopt/std'
9
+ include Getopt
10
+
11
+ RSpec.describe Getopt::Std do
12
+ example "version" do
13
+ expect(Std::VERSION).to eq('1.6.0')
14
+ expect(Std::VERSION).to be_frozen
15
+ end
16
+
17
+ example "getopts basic functionality" do
18
+ expect(Std).to respond_to(:getopts)
19
+ expect{ Std.getopts("ID") }.not_to raise_error
20
+ expect(Std.getopts("ID")).to be_kind_of(Hash)
21
+ end
22
+
23
+ example "getopts with separated switches" do
24
+ ARGV.push("-I", "-D")
25
+ expect(Std.getopts("ID")).to eq({"I"=>true, "D"=>true})
26
+ end
27
+
28
+ # Inspired by RF bug #23477
29
+ example "getopts with arguments that match switch are ok" do
30
+ ARGV.push("-d", "d")
31
+ expect(Std.getopts("d:")).to eq({"d" => "d"})
32
+
33
+ ARGV.push("-d", "ad")
34
+ expect(Std.getopts("d:")).to eq({"d" => "ad"})
35
+
36
+ ARGV.push("-a", "ad")
37
+ expect(Std.getopts("d:a:")).to eq({"a" => "ad"})
38
+
39
+ ARGV.push("-a", "da")
40
+ expect(Std.getopts("d:a:")).to eq({"a" => "da"})
41
+
42
+ ARGV.push("-a", "d")
43
+ expect(Std.getopts("d:a:")).to eq({"a" => "d"})
44
+
45
+ ARGV.push("-a", "dad")
46
+ expect(Std.getopts("d:a:")).to eq({"a" => "dad"})
47
+
48
+ ARGV.push("-d", "d", "-a", "a")
49
+ expect(Std.getopts("d:a:")).to eq({"d" => "d", "a" => "a"})
50
+ end
51
+
52
+ example "getopts with joined switches" do
53
+ ARGV.push("-ID")
54
+ expect(Std.getopts("ID")).to eq({"I"=>true, "D"=>true})
55
+ end
56
+
57
+ example "getopts with separated switches and mandatory argument" do
58
+ ARGV.push("-o", "hello", "-I", "-D")
59
+ expect(Std.getopts("o:ID")).to eq({"o"=>"hello", "I"=>true, "D"=>true})
60
+ end
61
+
62
+ example "getopts with joined switches and mandatory argument" do
63
+ ARGV.push("-IDo", "hello")
64
+ expect( Std.getopts("o:ID")).to eq({"o"=>"hello", "I"=>true, "D"=>true})
65
+ end
66
+
67
+ example "getopts with no arguments" do
68
+ expect{ Std.getopts("ID") }.not_to raise_error
69
+ expect(Std.getopts("ID")).to eq({})
70
+ expect(Std.getopts("ID")["I"]).to be_nil
71
+ expect(Std.getopts("ID")["D"]).to be_nil
72
+ end
73
+
74
+ # If a switch that accepts an argument appears more than once, the values
75
+ # are rolled into an array.
76
+ example "getopts with switch repeated" do
77
+ ARGV.push("-I", "-I", "-o", "hello", "-o", "world")
78
+ expect( Std.getopts("o:ID")).to eq({"o" => ["hello","world"], "I"=>true})
79
+ end
80
+
81
+ # EXPECTED ERRORS
82
+
83
+ example "getopts raises expected errors when passing a switch to another switch" do
84
+ ARGV.push("-d", "-d")
85
+ expect{ Std.getopts("d:a:") }.to raise_error(Getopt::Std::Error)
86
+
87
+ ARGV.push("-d", "-a")
88
+ expect{ Std.getopts("d:a:") }.to raise_error(Getopt::Std::Error)
89
+
90
+ ARGV.push("-a", "-d")
91
+ expect{ Std.getopts("d:a:") }.to raise_error(Getopt::Std::Error)
92
+
93
+ ARGV.push("-d", "-d")
94
+ expect{ Std.getopts("d:a:") }.to raise_error(Getopt::Std::Error, "cannot use switch '-d' as argument to another switch")
95
+ end
96
+
97
+ example "getopts raises expected errors if argument is missing" do
98
+ ARGV.push("-ID")
99
+ expect{ Std.getopts("I:D") }.to raise_error(Std::Error)
100
+
101
+ ARGV.push("-ID")
102
+ expect{ Std.getopts("ID:") }.to raise_error(Std::Error)
103
+ end
104
+
105
+ example "getopts raises expected errors if there are extra arguments" do
106
+ ARGV.push("-I", "-D", "-X")
107
+ expect{ Std.getopts("ID") }.to raise_error(Std::Error)
108
+
109
+ ARGV.push("-IDX")
110
+ expect{ Std.getopts("ID") }.to raise_error(Std::Error, "invalid option 'X'")
111
+ end
112
+
113
+ example "getopts raises expected errors with invalid or no arguments" do
114
+ expect{ Std.getopts }.to raise_error(ArgumentError)
115
+ expect{ Std.getopts(0) }.to raise_error(NoMethodError)
116
+ expect{ Std.getopts(nil) }.to raise_error(NoMethodError)
117
+ end
118
+
119
+ after do
120
+ ARGV.clear
121
+ end
122
+ end
metadata CHANGED
@@ -1,29 +1,70 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: getopt
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.2
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel J. Berger
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
- cert_chain: []
11
- date: 2014-10-13 00:00:00.000000000 Z
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIEcDCCAtigAwIBAgIBATANBgkqhkiG9w0BAQsFADA/MREwDwYDVQQDDAhkamJl
14
+ cmc5NjEVMBMGCgmSJomT8ixkARkWBWdtYWlsMRMwEQYKCZImiZPyLGQBGRYDY29t
15
+ MB4XDTE4MDMxODE1MjIwN1oXDTI4MDMxNTE1MjIwN1owPzERMA8GA1UEAwwIZGpi
16
+ ZXJnOTYxFTATBgoJkiaJk/IsZAEZFgVnbWFpbDETMBEGCgmSJomT8ixkARkWA2Nv
17
+ bTCCAaIwDQYJKoZIhvcNAQEBBQADggGPADCCAYoCggGBALgfaroVM6CI06cxr0/h
18
+ A+j+pc8fgpRgBVmHFaFunq28GPC3IvW7Nvc3Y8SnAW7pP1EQIbhlwRIaQzJ93/yj
19
+ u95KpkP7tA9erypnV7dpzBkzNlX14ACaFD/6pHoXoe2ltBxk3CCyyzx70mTqJpph
20
+ 75IB03ni9a8yqn8pmse+s83bFJOAqddSj009sGPcQO+QOWiNxqYv1n5EHcvj2ebO
21
+ 6hN7YTmhx7aSia4qL/quc4DlIaGMWoAhvML7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
38
+ date: 2021-03-02 00:00:00.000000000 Z
12
39
  dependencies:
13
40
  - !ruby/object:Gem::Dependency
14
- name: test-unit
41
+ name: rake
15
42
  requirement: !ruby/object:Gem::Requirement
16
43
  requirements:
17
44
  - - ">="
18
45
  - !ruby/object:Gem::Version
19
- version: 2.5.0
46
+ version: '0'
20
47
  type: :development
21
48
  prerelease: false
22
49
  version_requirements: !ruby/object:Gem::Requirement
23
50
  requirements:
24
51
  - - ">="
25
52
  - !ruby/object:Gem::Version
26
- version: 2.5.0
53
+ version: '0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: rspec
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '3.9'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '3.9'
27
68
  description: |2
28
69
  The getopt library provides two different command line option parsers.
29
70
  They are meant as easier and more convenient replacements for the
@@ -32,27 +73,37 @@ description: |2
32
73
  email: djberg96@gmail.com
33
74
  executables: []
34
75
  extensions: []
35
- extra_rdoc_files:
36
- - README
37
- - CHANGES
38
- - MANIFEST
76
+ extra_rdoc_files: []
39
77
  files:
40
- - CHANGES
41
- - MANIFEST
42
- - README
78
+ - CHANGES.md
79
+ - Gemfile
80
+ - LICENSE
81
+ - MANIFEST.md
82
+ - README.md
43
83
  - Rakefile
84
+ - certs/djberg96_pub.pem
44
85
  - examples/example_long.rb
45
86
  - examples/example_std.rb
46
87
  - getopt.gemspec
88
+ - lib/getopt-long.rb
89
+ - lib/getopt-std.rb
90
+ - lib/getopt.rb
47
91
  - lib/getopt/long.rb
48
92
  - lib/getopt/std.rb
49
- - test/test_getopt_long.rb
50
- - test/test_getopt_std.rb
93
+ - lib/getopt/version.rb
94
+ - spec/getopt_long_spec.rb
95
+ - spec/getopt_std_spec.rb
51
96
  homepage: https://github.com/djberg96/getopt
52
97
  licenses:
53
- - Artistic 2.0
54
- metadata: {}
55
- post_install_message:
98
+ - Apache-2.0
99
+ metadata:
100
+ homepage_uri: https://github.com/djberg96/getopt
101
+ bug_tracker_uri: https://github.com/djberg96/getopt/issues
102
+ changelog_uri: https://github.com/djberg96/getopt/blob/master/CHANGES
103
+ documentation_uri: https://github.com/djberg96/getopt/wiki
104
+ source_code_uri: https://github.com/djberg96/getopt
105
+ wiki_uri: https://github.com/djberg96/getopt/wiki
106
+ post_install_message:
56
107
  rdoc_options: []
57
108
  require_paths:
58
109
  - lib
@@ -67,11 +118,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
118
  - !ruby/object:Gem::Version
68
119
  version: '0'
69
120
  requirements: []
70
- rubyforge_project:
71
- rubygems_version: 2.4.2
72
- signing_key:
121
+ rubygems_version: 3.0.3
122
+ signing_key:
73
123
  specification_version: 4
74
124
  summary: Getopt::Std and Getopt::Long option parsers for Ruby
75
125
  test_files:
76
- - test/test_getopt_long.rb
77
- - test/test_getopt_std.rb
126
+ - spec/getopt_std_spec.rb
127
+ - spec/getopt_long_spec.rb
metadata.gz.sig ADDED
Binary file
data/README DELETED
@@ -1,191 +0,0 @@
1
- = Description
2
- Implements a simple Getopt::Std class for command line parsing, as well
3
- as a Getopt::Long class for more advanced command line parsing.
4
-
5
- = Installation
6
-
7
- gem install getopt
8
-
9
- = Synopsis
10
- == Getopt::Std
11
-
12
- require 'getopt/std'
13
-
14
- # Look for -o with argument, and -I and -D boolean arguments
15
- opt = Getopt::Std.getopts("o:ID")
16
-
17
- if opt["I"]
18
- # Do something if -I passed
19
-
20
- if opt["D"]
21
- # Do something if -D passed
22
-
23
- if opt["o"]
24
- case opt["o"]
25
- # blah, blah, blah
26
- end
27
- end
28
-
29
- == Getopt::Long
30
-
31
- require 'getopt/long'
32
-
33
- opt = Getopt::Long.getopts(
34
- ["--foo", "-f", Getopt::BOOLEAN],
35
- ["--bar", "-b", Getopt::REQUIRED]
36
- )
37
-
38
- # Or, to save your fingers some typing:
39
- #
40
- # require "getopt/long"
41
- # include Getopt
42
- # opt = Long.getopts(
43
- # ["--foo", "-f", BOOLEAN],
44
- # ["--bar", "-b", REQUIRED]
45
- # )
46
-
47
- if opt["foo"]
48
- # Do something if --foo or -f passed
49
-
50
- if opt["b"]
51
- # Do something if --bar or -b passed
52
-
53
- = Class Methods
54
-
55
- Std.getopts(switches)
56
-
57
- Takes a series of single character switches that can be accepted on the
58
- command line. Those characters followed by a ':' require an argument. The
59
- rest are considered boolean switches. Returns a hash, with the switches
60
- as the key (sans the leading '-'). For boolean switches, the value is
61
- either true or false. Switches that were not passed on the command line
62
- do not appear in the hash.
63
-
64
- In the event that a switch that accepts an argument appears multiple times
65
- the value for that key becomes an array of values.
66
-
67
- Long.getopts(switches)
68
-
69
- Takes an array of switches beginning with "--" followed by one or more
70
- alphanumeric or hyphen characters, or "-" followed by a single character.
71
- The type of argument, if any, can be specified as BOOLEAN, OPTIONAL,
72
- REQUIRED or INCREMENT.
73
-
74
- The array should be in the form:
75
-
76
- # long form, short form (alias), option type
77
- ["--long", "-l", Getopt::OPTION]
78
-
79
- Note that only the long form is required. If the short form is not
80
- specified, it will automatically be set to the first letter of the long
81
- switch. If multiple long switches with the same first character are
82
- listed without short switches, only the first long switch gets the short
83
- switch alias.
84
-
85
- If the argument type is not specified, the default is BOOLEAN.
86
-
87
- For the truly lazy, you can also pass a string of long switches (with
88
- no short switches or argument types).
89
-
90
- See the 'examples' directory for more examples.
91
-
92
- = Getopt::Long argument types
93
-
94
- REQUIRED
95
- If the option is specified on the command line, it must be followed by
96
- a non-blank argument. This argument cannot be another switch. If this
97
- switch appears multiple times, the values are collected into an array.
98
-
99
- BOOLEAN
100
- If the option is specified on the command line, its value is set to true.
101
- It must not be followed by a non-blank argument, excluding other switches.
102
- Attempting to pass a boolean switch more than once will raise an error.
103
-
104
- OPTIONAL
105
- If the option is specified on the command line, it may or may not accept
106
- an argument, excluding other valid switches. If an argument is present,
107
- it's value is set to that argument. If an argument is not present, it's
108
- value is set to nil.
109
-
110
- INCREMENT
111
- If the option is specified on the command line, its value is incremented
112
- by one for each appearance on the command line, or set to 1 if it appears
113
- only once.
114
-
115
- = Future Plans
116
- Add support for negatable options so that you can do "--no-foo", for
117
- example.
118
-
119
- Add support for numeric types, so that you don't have to manually convert
120
- strings to numbers.
121
-
122
- Allow shortcut characters for the option types, e.g. "?" for BOOLEAN, "+"
123
- for INCREMENT, etc.
124
-
125
- = Known Issues
126
-
127
- == Getopt::Std
128
-
129
- You cannot squish switches that require arguments with the argument itself.
130
- For example, if you do Getopt::Std.getopts("o:ID"), it will not parse
131
- "-IDohello" properly. Instead, you must do "-IDo hello". Or, you can just
132
- separate the argument, e.g. "-I -D -o hello".
133
-
134
- == Getopt::Long
135
-
136
- If you mix and match compressed switches with separate, optional switches
137
- the optional switch will be set to true instead of nil if it separated
138
- from the compressed switches.
139
-
140
- == Reporting Issues
141
-
142
- If you find any other issues, please log them on the project
143
- page at https://github.com/djberg96/getopt.
144
-
145
- = Other Stuff
146
-
147
- Neither class attempts to be POSIX compliant in any way, shape or form.
148
- And I don't care!
149
-
150
- = Notes From the Author
151
-
152
- My main gripe with the getoptlong library currently in the standard library
153
- is that it doesn't return a hash, yet gives you partial hash behavior. This
154
- was both confusing and annoying, since the first thing I do (along with
155
- everyone else) is collect the results into a hash for later processing.
156
-
157
- My main gripe with the optparse library (also in the standard library) is
158
- that it treats command line processing like event processing. It's too
159
- complex, when 90% of the time all you want to do is slurp the command line
160
- options into a hash.
161
-
162
- So, I did something utterly novel with this library. I collected the command
163
- line options ... (wait for it) ... into a hash! Then I give that hash to
164
- you, aliases and all. I did get some ideas from Perl's Getopt::Long library,
165
- but this is in no way a port of that module (which supports POSIX parsing, GNU
166
- parsing, more option types, etc). My goal was to provide the functionality
167
- that I felt would cover the vast majority of common cases, yet still provide
168
- a little extra spice with switch types (REQUIRED, OPTIONAL, etc).
169
-
170
- There are a few extra things I plan to add (see the 'Future Plans' above) but
171
- I do not plan on this library ever becoming as feature rich as, say, Perl's
172
- Getopt::Long module.
173
-
174
- If you plan to write a full fledged command line application, e.g. you plan
175
- on implementing a full help system, gobs of command line options and tons of
176
- switches, consider Jim Freeze's 'commandline' gem.
177
-
178
- = Warranty
179
- This package is provided "as is" and without any express or
180
- implied warranties, including, without limitation, the implied
181
- warranties of merchantability and fitness for a particular purpose.
182
-
183
- = License
184
- Artistic 2.0
185
-
186
- = Copyright
187
- (C) 2005-2014, Daniel J. Berger
188
- All Rights Reserved
189
-
190
- = Author
191
- Daniel J. Berger