shellopts 2.0.0.pre.8 → 2.0.0.pre.9
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.
- checksums.yaml +4 -4
- data/lib/shellopts.rb +1 -0
- data/lib/shellopts/args.rb +15 -10
- data/lib/shellopts/shellopts.rb +15 -8
- data/lib/shellopts/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: abf197d1a00cb875a6ac80ba5dd672bfdf035778f2e281baa151b61b4ffb4644
|
4
|
+
data.tar.gz: 2e4f6a91002acbdd0ad2f528305e559606accb5f9b6f2b6610f88851a0cbd981
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6297e1adbf0a1a6f9a6c3b1057ae2a9c904cc34f4dfad26ee9ae25f48400476ea128b3c834e23cbe0ed746a1c11a69001c74dcd422ec06eec04b3138b855cbf3
|
7
|
+
data.tar.gz: bc9b0549d18774cdfac16150693e30b707bae6eb718ee36f8964405a78794335aea39386cb67e5457b7652e276fd244e9f6ec88d73577756e87d7f9490fd80b4
|
data/lib/shellopts.rb
CHANGED
@@ -135,6 +135,7 @@ module ShellOpts
|
|
135
135
|
def self.process(spec, argv, name: ::ShellOpts.default_name, usage: ::ShellOpts.default_usage)
|
136
136
|
@shellopts.nil? or reset
|
137
137
|
@shellopts = ShellOpts.new(spec, argv, name: name, usage: usage)
|
138
|
+
@shellopts.process
|
138
139
|
end
|
139
140
|
|
140
141
|
# Process command line, set current shellopts object, and return a
|
data/lib/shellopts/args.rb
CHANGED
@@ -2,21 +2,23 @@
|
|
2
2
|
module ShellOpts
|
3
3
|
# Specialization of Array for arguments lists. Args extends Array with a
|
4
4
|
# #extract and an #expect method to extract elements from the array. The
|
5
|
-
# methods
|
5
|
+
# methods raise a ShellOpts::UserError exception in case of errors
|
6
6
|
class Args < Array
|
7
7
|
def initialize(shellopts, *args)
|
8
8
|
@shellopts = shellopts
|
9
9
|
super(*args)
|
10
10
|
end
|
11
11
|
|
12
|
-
# Remove and return elements from beginning of the array
|
13
|
-
#
|
14
|
-
# If
|
15
|
-
# the count is
|
16
|
-
# array.
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
12
|
+
# Remove and return elements from beginning of the array
|
13
|
+
#
|
14
|
+
# If +count_or_range+ is a number, that number of elements will be
|
15
|
+
# returned. If the count is one, a simple value is returned instead of an
|
16
|
+
# array. If the count is negative, the elements will be removed from the
|
17
|
+
# end of the array. If +count_or_range+ is a range, the number of elements
|
18
|
+
# returned will be in that range. The range can't contain negative numbers
|
19
|
+
#
|
20
|
+
# #extract raise a ShellOpts::UserError exception if there's is not enough
|
21
|
+
# elements in the array to satisfy the request
|
20
22
|
def extract(count_or_range, message = nil)
|
21
23
|
if count_or_range.is_a?(Range)
|
22
24
|
range = count_or_range
|
@@ -33,8 +35,11 @@ module ShellOpts
|
|
33
35
|
end
|
34
36
|
end
|
35
37
|
|
36
|
-
# As extract except it doesn't allow negative counts and that the array is
|
38
|
+
# As #extract except it doesn't allow negative counts and that the array is
|
37
39
|
# expect to be emptied by the operation
|
40
|
+
#
|
41
|
+
# #expect raise a ShellOpts::UserError exception if the array is not emptied
|
42
|
+
# by the operation
|
38
43
|
def expect(count_or_range, message = nil)
|
39
44
|
count_or_range === self.size or inoa(message)
|
40
45
|
extract(count_or_range) # Can't fail
|
data/lib/shellopts/shellopts.rb
CHANGED
@@ -39,17 +39,16 @@ module ShellOpts
|
|
39
39
|
# The IDR generated from the Ast
|
40
40
|
attr_reader :idr
|
41
41
|
|
42
|
-
# Compile a spec string into a grammar
|
43
|
-
# arguments
|
42
|
+
# Compile a spec string into a grammar
|
44
43
|
#
|
45
44
|
# +spec+ is the spec string, and +argv+ the command line (typically the
|
46
45
|
# global ARGV array). +name+ is the name of the program and defaults to the
|
47
46
|
# basename of the program
|
48
47
|
#
|
49
|
-
# Syntax errors in the spec string are caused by the developer and
|
50
|
-
# +ShellOpts::CompilerError+ exception. Errors in
|
51
|
-
# caused by the user and
|
52
|
-
#
|
48
|
+
# Syntax errors in the spec string are caused by the developer and cause
|
49
|
+
# #initialize to raise a +ShellOpts::CompilerError+ exception. Errors in
|
50
|
+
# the +argv+ arguments are caused by the user and cause #process to raise
|
51
|
+
# ShellOpts::UserError exception
|
53
52
|
#
|
54
53
|
# TODO: Change to (name, spec, argv, usage: nil) because
|
55
54
|
# ShellOpts::ShellOpts isn't a magician like the ShellOpts module
|
@@ -60,13 +59,21 @@ module ShellOpts
|
|
60
59
|
@argv = argv
|
61
60
|
begin
|
62
61
|
@grammar = Grammar.compile(@name, @spec)
|
63
|
-
@ast = Ast.parse(@grammar, @argv)
|
64
|
-
@idr = Idr.generate(self)
|
65
62
|
rescue Grammar::Compiler::Error => ex
|
66
63
|
raise CompilerError.new(5, ex.message)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Process command line arguments and return self. Raises a
|
68
|
+
# ShellOpts::UserError in case of an error
|
69
|
+
def process
|
70
|
+
begin
|
71
|
+
@ast = Ast.parse(@grammar, @argv)
|
72
|
+
@idr = Idr.generate(self)
|
67
73
|
rescue Ast::Parser::Error => ex
|
68
74
|
raise UserError.new(ex.message)
|
69
75
|
end
|
76
|
+
self
|
70
77
|
end
|
71
78
|
|
72
79
|
# Return an array representation of options and commands in the same order
|
data/lib/shellopts/version.rb
CHANGED