command_mapper 0.1.0.pre1
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 +7 -0
- data/.github/workflows/ruby.yml +27 -0
- data/.gitignore +10 -0
- data/.rspec +1 -0
- data/.yardopts +1 -0
- data/ChangeLog.md +25 -0
- data/Gemfile +15 -0
- data/LICENSE.txt +20 -0
- data/README.md +369 -0
- data/Rakefile +12 -0
- data/commnad_mapper.gemspec +61 -0
- data/gemspec.yml +23 -0
- data/lib/command_mapper/arg.rb +75 -0
- data/lib/command_mapper/argument.rb +142 -0
- data/lib/command_mapper/command.rb +606 -0
- data/lib/command_mapper/exceptions.rb +19 -0
- data/lib/command_mapper/option.rb +282 -0
- data/lib/command_mapper/option_value.rb +21 -0
- data/lib/command_mapper/sudo.rb +73 -0
- data/lib/command_mapper/types/enum.rb +35 -0
- data/lib/command_mapper/types/hex.rb +82 -0
- data/lib/command_mapper/types/input_dir.rb +35 -0
- data/lib/command_mapper/types/input_file.rb +35 -0
- data/lib/command_mapper/types/input_path.rb +29 -0
- data/lib/command_mapper/types/key_value.rb +131 -0
- data/lib/command_mapper/types/key_value_list.rb +45 -0
- data/lib/command_mapper/types/list.rb +90 -0
- data/lib/command_mapper/types/map.rb +64 -0
- data/lib/command_mapper/types/num.rb +50 -0
- data/lib/command_mapper/types/str.rb +85 -0
- data/lib/command_mapper/types/type.rb +102 -0
- data/lib/command_mapper/types.rb +6 -0
- data/lib/command_mapper/version.rb +4 -0
- data/lib/command_mapper.rb +2 -0
- data/spec/arg_spec.rb +137 -0
- data/spec/argument_spec.rb +513 -0
- data/spec/commnad_spec.rb +1175 -0
- data/spec/exceptions_spec.rb +14 -0
- data/spec/option_spec.rb +882 -0
- data/spec/option_value_spec.rb +17 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/sudo_spec.rb +24 -0
- data/spec/types/enum_spec.rb +31 -0
- data/spec/types/hex_spec.rb +158 -0
- data/spec/types/input_dir_spec.rb +30 -0
- data/spec/types/input_file_spec.rb +34 -0
- data/spec/types/input_path_spec.rb +32 -0
- data/spec/types/key_value_list_spec.rb +100 -0
- data/spec/types/key_value_spec.rb +272 -0
- data/spec/types/list_spec.rb +143 -0
- data/spec/types/map_spec.rb +62 -0
- data/spec/types/num_spec.rb +90 -0
- data/spec/types/str_spec.rb +232 -0
- data/spec/types/type_spec.rb +59 -0
- metadata +118 -0
@@ -0,0 +1,142 @@
|
|
1
|
+
require 'command_mapper/exceptions'
|
2
|
+
require 'command_mapper/arg'
|
3
|
+
|
4
|
+
module CommandMapper
|
5
|
+
#
|
6
|
+
# Represents an additional argument of a command.
|
7
|
+
#
|
8
|
+
class Argument < Arg
|
9
|
+
|
10
|
+
# The argument name.
|
11
|
+
#
|
12
|
+
# @return [Symbol]
|
13
|
+
attr_reader :name
|
14
|
+
|
15
|
+
#
|
16
|
+
# Initializes the argument.
|
17
|
+
#
|
18
|
+
# @param [Symbol] name
|
19
|
+
# The argument's name.
|
20
|
+
#
|
21
|
+
# @param [Boolean] required
|
22
|
+
# Specifies whether the argument is required or can be omitted.
|
23
|
+
#
|
24
|
+
# @param [Types::Type, Hash] type
|
25
|
+
# The value type of the argument.
|
26
|
+
#
|
27
|
+
# @param [Boolean] repeats
|
28
|
+
# Specifies whether the argument can be given multiple times.
|
29
|
+
#
|
30
|
+
# @raise [ArgumentError]
|
31
|
+
# The given `type:` must not be `false` or `nil`.
|
32
|
+
#
|
33
|
+
def initialize(name, required: true, type: Types::Str.new, repeats: false)
|
34
|
+
super(required: required, type: type)
|
35
|
+
|
36
|
+
@name = name
|
37
|
+
@repeats = repeats
|
38
|
+
end
|
39
|
+
|
40
|
+
#
|
41
|
+
# Indicates whether the arg can be repeated multiple times or not.
|
42
|
+
#
|
43
|
+
# @return [Boolean]
|
44
|
+
#
|
45
|
+
def repeats?
|
46
|
+
@repeats
|
47
|
+
end
|
48
|
+
|
49
|
+
#
|
50
|
+
# Validates whether a given value is compatible with the arg.
|
51
|
+
#
|
52
|
+
# @param [Array<Object>, Object] value
|
53
|
+
#
|
54
|
+
# @return [true, (false, String)]
|
55
|
+
# Returns true if the value is valid, or `false` and a validation error
|
56
|
+
# message if the value is not compatible.
|
57
|
+
#
|
58
|
+
def validate(value)
|
59
|
+
if repeats?
|
60
|
+
values = case value
|
61
|
+
when Array then value
|
62
|
+
else [value]
|
63
|
+
end
|
64
|
+
|
65
|
+
if required?
|
66
|
+
# argument requires atleast one value
|
67
|
+
if values.empty?
|
68
|
+
return [false, "requires at least one value"]
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# validate each element in the value
|
73
|
+
values.each do |element|
|
74
|
+
valid, message = @type.validate(element)
|
75
|
+
|
76
|
+
unless valid
|
77
|
+
return [valid, message]
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
return true
|
82
|
+
else
|
83
|
+
super(value)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
#
|
88
|
+
# Converts the given value into the command-line arguments for the
|
89
|
+
# argument's flag and value.
|
90
|
+
#
|
91
|
+
# @param [Array] argv
|
92
|
+
# The argv array.
|
93
|
+
#
|
94
|
+
# @param [Object] value
|
95
|
+
# The value for the argument.
|
96
|
+
#
|
97
|
+
# @return [Array<String>]
|
98
|
+
# The command-line arguments.
|
99
|
+
#
|
100
|
+
# @raise [ArgumentError]
|
101
|
+
# The given value was incompatible with the argument.
|
102
|
+
#
|
103
|
+
def argv(argv=[],value)
|
104
|
+
valid, message = validate(value)
|
105
|
+
|
106
|
+
unless valid
|
107
|
+
raise(ValidationError,"argument #{@name} was given an invalid value (#{value.inspect}): #{message}")
|
108
|
+
end
|
109
|
+
|
110
|
+
if repeats?
|
111
|
+
values = Array(value)
|
112
|
+
|
113
|
+
values.each do |element|
|
114
|
+
emit_arg_value(argv,element)
|
115
|
+
end
|
116
|
+
else
|
117
|
+
emit_arg_value(argv,value)
|
118
|
+
end
|
119
|
+
|
120
|
+
return argv
|
121
|
+
end
|
122
|
+
|
123
|
+
private
|
124
|
+
|
125
|
+
#
|
126
|
+
# Emits a single command-line arg value.
|
127
|
+
#
|
128
|
+
# @param [Array<String>] argv
|
129
|
+
# The argv array to append to.
|
130
|
+
#
|
131
|
+
# @param [Object] value
|
132
|
+
# The value for the argument.
|
133
|
+
#
|
134
|
+
def emit_arg_value(argv,value)
|
135
|
+
# explicitly ignore nil values
|
136
|
+
unless value.nil?
|
137
|
+
argv << @type.format(value)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
142
|
+
end
|