clamp 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -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
@@ -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