clamp 0.5.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/.autotest +9 -0
- data/.travis.yml +1 -0
- data/CHANGES.md +10 -0
- data/{README.markdown → README.md} +10 -4
- data/examples/admin +1 -3
- data/examples/flipflop +1 -3
- data/examples/fubar +1 -3
- data/examples/scoop +17 -0
- data/examples/speak +2 -4
- data/lib/clamp.rb +4 -0
- data/lib/clamp/attribute/declaration.rb +49 -0
- data/lib/clamp/attribute/definition.rb +82 -0
- data/lib/clamp/command.rb +4 -1
- data/lib/clamp/errors.rb +7 -4
- data/lib/clamp/help.rb +4 -4
- data/lib/clamp/option/declaration.rb +9 -7
- data/lib/clamp/option/definition.rb +94 -0
- data/lib/clamp/option/parsing.rb +3 -26
- data/lib/clamp/parameter/declaration.rb +8 -7
- data/lib/clamp/parameter/definition.rb +48 -0
- data/lib/clamp/parameter/parsing.rb +5 -4
- data/lib/clamp/subcommand/declaration.rb +25 -6
- data/lib/clamp/subcommand/definition.rb +25 -0
- data/lib/clamp/subcommand/execution.rb +23 -4
- data/lib/clamp/subcommand/parsing.rb +3 -21
- data/lib/clamp/truthy.rb +9 -0
- data/lib/clamp/version.rb +1 -1
- data/spec/clamp/command_group_spec.rb +50 -3
- data/spec/clamp/command_spec.rb +94 -65
- data/spec/clamp/{option_spec.rb → option/definition_spec.rb} +76 -13
- data/spec/clamp/{parameter_spec.rb → parameter/definition_spec.rb} +21 -13
- metadata +16 -12
- data/lib/clamp/attribute.rb +0 -44
- data/lib/clamp/attribute_declaration.rb +0 -40
- data/lib/clamp/option.rb +0 -94
- data/lib/clamp/parameter.rb +0 -77
- data/lib/clamp/subcommand.rb +0 -23
data/lib/clamp/option.rb
DELETED
@@ -1,94 +0,0 @@
|
|
1
|
-
require 'clamp/attribute'
|
2
|
-
|
3
|
-
module Clamp
|
4
|
-
|
5
|
-
class Option < Attribute
|
6
|
-
|
7
|
-
def initialize(switches, type, description, options = {})
|
8
|
-
@switches = Array(switches)
|
9
|
-
@type = type
|
10
|
-
@description = description
|
11
|
-
if options.has_key?(:attribute_name)
|
12
|
-
@attribute_name = options[:attribute_name].to_s
|
13
|
-
end
|
14
|
-
if options.has_key?(:default)
|
15
|
-
@default_value = options[:default]
|
16
|
-
end
|
17
|
-
if options.has_key?(:environment_variable)
|
18
|
-
@environment_variable = options[:environment_variable]
|
19
|
-
end
|
20
|
-
if options.has_key?(:required)
|
21
|
-
@required = options[:required]
|
22
|
-
# Do some light validation for conflicting settings.
|
23
|
-
if options.has_key?(:default)
|
24
|
-
raise ArgumentError, "Specifying a :default value also :required doesn't make sense"
|
25
|
-
end
|
26
|
-
if type == :flag
|
27
|
-
raise ArgumentError, "A required flag (boolean) doesn't make sense."
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
attr_reader :switches, :type
|
33
|
-
|
34
|
-
def attribute_name
|
35
|
-
@attribute_name ||= long_switch.sub(/^--(\[no-\])?/, '').tr('-', '_')
|
36
|
-
end
|
37
|
-
|
38
|
-
def long_switch
|
39
|
-
switches.find { |switch| switch =~ /^--/ }
|
40
|
-
end
|
41
|
-
|
42
|
-
def handles?(switch)
|
43
|
-
recognised_switches.member?(switch)
|
44
|
-
end
|
45
|
-
|
46
|
-
def required?
|
47
|
-
@required
|
48
|
-
end
|
49
|
-
|
50
|
-
def flag?
|
51
|
-
@type == :flag
|
52
|
-
end
|
53
|
-
|
54
|
-
def flag_value(switch)
|
55
|
-
!(switch =~ /^--no-(.*)/ && switches.member?("--\[no-\]#{$1}"))
|
56
|
-
end
|
57
|
-
|
58
|
-
def read_method
|
59
|
-
if flag?
|
60
|
-
super + "?"
|
61
|
-
else
|
62
|
-
super
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
def extract_value(switch, arguments)
|
67
|
-
if flag?
|
68
|
-
flag_value(switch)
|
69
|
-
else
|
70
|
-
arguments.shift
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
def help_lhs
|
75
|
-
lhs = switches.join(", ")
|
76
|
-
lhs += " " + type unless flag?
|
77
|
-
lhs
|
78
|
-
end
|
79
|
-
|
80
|
-
private
|
81
|
-
|
82
|
-
def recognised_switches
|
83
|
-
switches.map do |switch|
|
84
|
-
if switch =~ /^--\[no-\](.*)/
|
85
|
-
["--#{$1}", "--no-#{$1}"]
|
86
|
-
else
|
87
|
-
switch
|
88
|
-
end
|
89
|
-
end.flatten
|
90
|
-
end
|
91
|
-
|
92
|
-
end
|
93
|
-
|
94
|
-
end
|
data/lib/clamp/parameter.rb
DELETED
@@ -1,77 +0,0 @@
|
|
1
|
-
require 'clamp/attribute'
|
2
|
-
|
3
|
-
module Clamp
|
4
|
-
|
5
|
-
class Parameter < Attribute
|
6
|
-
|
7
|
-
def initialize(name, description, options = {})
|
8
|
-
@name = name
|
9
|
-
@description = description
|
10
|
-
@multivalued = (@name =~ ELLIPSIS_SUFFIX)
|
11
|
-
@required = (@name !~ OPTIONAL)
|
12
|
-
if options.has_key?(:attribute_name)
|
13
|
-
@attribute_name = options[:attribute_name].to_s
|
14
|
-
end
|
15
|
-
if options.has_key?(:default)
|
16
|
-
@default_value = options[:default]
|
17
|
-
end
|
18
|
-
if options.has_key?(:environment_variable)
|
19
|
-
@environment_variable = options[:environment_variable]
|
20
|
-
end
|
21
|
-
@attribute_name ||= infer_attribute_name
|
22
|
-
end
|
23
|
-
|
24
|
-
attr_reader :name, :attribute_name
|
25
|
-
|
26
|
-
def help_lhs
|
27
|
-
name
|
28
|
-
end
|
29
|
-
|
30
|
-
def consume(arguments)
|
31
|
-
if required? && arguments.empty?
|
32
|
-
raise ArgumentError, "no value provided"
|
33
|
-
end
|
34
|
-
if multivalued?
|
35
|
-
if arguments.length > 0
|
36
|
-
arguments.shift(arguments.length)
|
37
|
-
end
|
38
|
-
else
|
39
|
-
arguments.shift
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
def default_value
|
44
|
-
if defined?(@default_value)
|
45
|
-
@default_value
|
46
|
-
elsif multivalued?
|
47
|
-
[]
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
private
|
52
|
-
|
53
|
-
ELLIPSIS_SUFFIX = / \.\.\.$/
|
54
|
-
OPTIONAL = /^\[(.*)\]/
|
55
|
-
|
56
|
-
VALID_ATTRIBUTE_NAME = /^[a-z0-9_]+$/
|
57
|
-
|
58
|
-
def infer_attribute_name
|
59
|
-
inferred_name = name.downcase.tr('-', '_').sub(ELLIPSIS_SUFFIX, '').sub(OPTIONAL) { $1 }
|
60
|
-
unless inferred_name =~ VALID_ATTRIBUTE_NAME
|
61
|
-
raise "cannot infer attribute_name from #{name.inspect}"
|
62
|
-
end
|
63
|
-
inferred_name += "_list" if multivalued?
|
64
|
-
inferred_name
|
65
|
-
end
|
66
|
-
|
67
|
-
def multivalued?
|
68
|
-
@multivalued
|
69
|
-
end
|
70
|
-
|
71
|
-
def required?
|
72
|
-
@required
|
73
|
-
end
|
74
|
-
|
75
|
-
end
|
76
|
-
|
77
|
-
end
|
data/lib/clamp/subcommand.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
module Clamp
|
2
|
-
|
3
|
-
class Subcommand < Struct.new(:name, :description, :subcommand_class)
|
4
|
-
|
5
|
-
def initialize(names, description, subcommand_class)
|
6
|
-
@names = Array(names)
|
7
|
-
@description = description
|
8
|
-
@subcommand_class = subcommand_class
|
9
|
-
end
|
10
|
-
|
11
|
-
attr_reader :names, :description, :subcommand_class
|
12
|
-
|
13
|
-
def is_called?(name)
|
14
|
-
names.member?(name)
|
15
|
-
end
|
16
|
-
|
17
|
-
def help
|
18
|
-
[names.join(", "), description]
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
end
|