bbcloud 0.11.2 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (198) hide show
  1. data/.gitignore +3 -2
  2. data/Gemfile +3 -0
  3. data/README +11 -1
  4. data/README.rdoc +11 -1
  5. data/Rakefile +12 -0
  6. data/bbcloud.gemspec +10 -10
  7. data/bin/brightbox-accounts +7 -2
  8. data/bin/brightbox-cloudips +7 -2
  9. data/bin/brightbox-config +7 -2
  10. data/bin/brightbox-images +7 -2
  11. data/bin/brightbox-lbs +7 -2
  12. data/bin/brightbox-servers +7 -2
  13. data/bin/brightbox-types +7 -2
  14. data/bin/brightbox-users +7 -2
  15. data/bin/brightbox-zones +7 -2
  16. data/lib/bbcloud.rb +43 -0
  17. data/lib/bbcloud/accounts.rb +5 -3
  18. data/lib/bbcloud/command_generator.rb +50 -0
  19. data/lib/bbcloud/commands/accounts-list.rb +13 -11
  20. data/lib/bbcloud/commands/accounts-reset-ftp-password.rb +26 -23
  21. data/lib/bbcloud/commands/accounts-show.rb +20 -16
  22. data/lib/bbcloud/commands/cloudips-create.rb +16 -14
  23. data/lib/bbcloud/commands/cloudips-destroy.rb +29 -26
  24. data/lib/bbcloud/commands/cloudips-list.rb +13 -11
  25. data/lib/bbcloud/commands/cloudips-map.rb +48 -46
  26. data/lib/bbcloud/commands/cloudips-show.rb +15 -12
  27. data/lib/bbcloud/commands/cloudips-unmap.rb +28 -25
  28. data/lib/bbcloud/commands/config-client-add.rb +33 -31
  29. data/lib/bbcloud/commands/config-client-default.rb +18 -16
  30. data/lib/bbcloud/commands/config-client-list.rb +19 -16
  31. data/lib/bbcloud/commands/config-client-remove.rb +18 -16
  32. data/lib/bbcloud/commands/images-destroy.rb +18 -16
  33. data/lib/bbcloud/commands/images-list.rb +20 -18
  34. data/lib/bbcloud/commands/images-register.rb +21 -19
  35. data/lib/bbcloud/commands/images-show.rb +17 -15
  36. data/lib/bbcloud/commands/lbs-add-nodes.rb +15 -13
  37. data/lib/bbcloud/commands/lbs-create.rb +65 -63
  38. data/lib/bbcloud/commands/lbs-destroy.rb +13 -11
  39. data/lib/bbcloud/commands/lbs-list.rb +13 -11
  40. data/lib/bbcloud/commands/lbs-remove-nodes.rb +15 -13
  41. data/lib/bbcloud/commands/lbs-show.rb +15 -13
  42. data/lib/bbcloud/commands/lbs-update.rb +59 -57
  43. data/lib/bbcloud/commands/servers-activate-cloud.rb +18 -16
  44. data/lib/bbcloud/commands/servers-create.rb +80 -77
  45. data/lib/bbcloud/commands/servers-destroy.rb +17 -15
  46. data/lib/bbcloud/commands/servers-list.rb +14 -14
  47. data/lib/bbcloud/commands/servers-show.rb +39 -37
  48. data/lib/bbcloud/commands/servers-shutdown.rb +14 -12
  49. data/lib/bbcloud/commands/servers-snapshot.rb +14 -12
  50. data/lib/bbcloud/commands/servers-start.rb +15 -13
  51. data/lib/bbcloud/commands/servers-stop.rb +14 -12
  52. data/lib/bbcloud/commands/types-list.rb +13 -11
  53. data/lib/bbcloud/commands/types-show.rb +18 -16
  54. data/lib/bbcloud/commands/users-list.rb +13 -11
  55. data/lib/bbcloud/commands/users-show.rb +17 -15
  56. data/lib/bbcloud/commands/users-update.rb +27 -25
  57. data/lib/bbcloud/commands/zones-list.rb +13 -11
  58. data/lib/bbcloud/config.rb +125 -111
  59. data/lib/bbcloud/error_parser.rb +34 -0
  60. data/lib/bbcloud/fog_extensions.rb +19 -0
  61. data/lib/bbcloud/gli_global_hooks.rb +56 -0
  62. data/lib/bbcloud/logging.rb +40 -0
  63. data/lib/bbcloud/ruby_core_ext.rb +9 -0
  64. data/lib/bbcloud/servers.rb +7 -2
  65. data/lib/bbcloud/tables.rb +78 -73
  66. data/lib/bbcloud/vendor/gli/.gitignore +10 -0
  67. data/lib/bbcloud/vendor/gli/.rvmrc +1 -0
  68. data/lib/bbcloud/vendor/gli/Gemfile +5 -0
  69. data/lib/bbcloud/vendor/gli/LICENSE.txt +201 -0
  70. data/lib/bbcloud/vendor/gli/README.rdoc +54 -0
  71. data/lib/bbcloud/vendor/gli/Rakefile +84 -0
  72. data/lib/bbcloud/vendor/gli/bin/gli +72 -0
  73. data/lib/bbcloud/vendor/gli/bin/report_on_rake_results +10 -0
  74. data/lib/bbcloud/vendor/gli/bin/test_all_rubies.sh +2 -0
  75. data/lib/bbcloud/vendor/gli/features/gli_executable.feature +88 -0
  76. data/lib/bbcloud/vendor/gli/features/gli_init.feature +122 -0
  77. data/lib/bbcloud/vendor/gli/features/step_definitions/gli_executable_steps.rb +12 -0
  78. data/lib/bbcloud/vendor/gli/features/step_definitions/gli_init_steps.rb +4 -0
  79. data/lib/bbcloud/vendor/gli/features/support/env.rb +21 -0
  80. data/lib/bbcloud/vendor/gli/gli.cheat +80 -0
  81. data/lib/bbcloud/vendor/gli/gli.gemspec +46 -0
  82. data/lib/bbcloud/vendor/gli/gli.rdoc +51 -0
  83. data/lib/bbcloud/vendor/gli/lib/gli.rb +560 -0
  84. data/lib/bbcloud/vendor/gli/lib/gli/command.rb +124 -0
  85. data/lib/bbcloud/vendor/gli/lib/gli/command_line_token.rb +58 -0
  86. data/lib/bbcloud/vendor/gli/lib/gli/copy_options_to_aliases.rb +33 -0
  87. data/lib/bbcloud/vendor/gli/lib/gli/exceptions.rb +45 -0
  88. data/lib/bbcloud/vendor/gli/lib/gli/flag.rb +67 -0
  89. data/lib/bbcloud/vendor/gli/lib/gli/options.rb +19 -0
  90. data/lib/bbcloud/vendor/gli/lib/gli/switch.rb +63 -0
  91. data/lib/bbcloud/vendor/gli/lib/gli/terminal.rb +79 -0
  92. data/lib/bbcloud/vendor/gli/lib/gli_version.rb +3 -0
  93. data/lib/bbcloud/vendor/gli/lib/support/help.rb +180 -0
  94. data/lib/bbcloud/vendor/gli/lib/support/initconfig.rb +34 -0
  95. data/lib/bbcloud/vendor/gli/lib/support/rdoc.rb +119 -0
  96. data/lib/bbcloud/vendor/gli/lib/support/scaffold.rb +284 -0
  97. data/lib/bbcloud/vendor/gli/test/config.yaml +9 -0
  98. data/lib/bbcloud/vendor/gli/test/gli.reek +116 -0
  99. data/lib/bbcloud/vendor/gli/test/roodi.yaml +9 -0
  100. data/lib/bbcloud/vendor/gli/test/tc_command.rb +329 -0
  101. data/lib/bbcloud/vendor/gli/test/tc_flag.rb +67 -0
  102. data/lib/bbcloud/vendor/gli/test/tc_gli.rb +429 -0
  103. data/lib/bbcloud/vendor/gli/test/tc_options.rb +31 -0
  104. data/lib/bbcloud/vendor/gli/test/tc_parsing.rb +279 -0
  105. data/lib/bbcloud/vendor/gli/test/tc_switch.rb +80 -0
  106. data/lib/bbcloud/vendor/gli/test/tc_terminal.rb +91 -0
  107. data/lib/bbcloud/version.rb +1 -1
  108. data/pkg/bbcloud-0.12.0.gem +0 -0
  109. data/spec/fixtures/vcr_cassettes/server_list.yml +117 -0
  110. data/spec/servers_spec.rb +36 -0
  111. data/spec/spec_helper.rb +24 -0
  112. data/spec/support/common_helpers.rb +47 -0
  113. metadata +129 -135
  114. data/lib/bbcloud/cli.rb +0 -160
  115. data/lib/bbcloud/vendor/fog/lib/fog.rb +0 -14
  116. data/lib/bbcloud/vendor/fog/lib/fog/compute.rb +0 -43
  117. data/lib/bbcloud/vendor/fog/lib/fog/compute/brightbox.rb +0 -162
  118. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/account.rb +0 -51
  119. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/cloud_ip.rb +0 -47
  120. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/cloud_ips.rb +0 -34
  121. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/flavor.rb +0 -33
  122. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/flavors.rb +0 -28
  123. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/image.rb +0 -56
  124. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/images.rb +0 -28
  125. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/load_balancer.rb +0 -50
  126. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/load_balancers.rb +0 -28
  127. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/server.rb +0 -108
  128. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/servers.rb +0 -29
  129. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/user.rb +0 -39
  130. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/users.rb +0 -29
  131. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/zone.rb +0 -21
  132. data/lib/bbcloud/vendor/fog/lib/fog/compute/models/brightbox/zones.rb +0 -29
  133. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/activate_console_server.rb +0 -20
  134. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/add_listeners_load_balancer.rb +0 -20
  135. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/add_nodes_load_balancer.rb +0 -20
  136. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/create_api_client.rb +0 -19
  137. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/create_cloud_ip.rb +0 -19
  138. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/create_image.rb +0 -19
  139. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/create_load_balancer.rb +0 -19
  140. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/create_server.rb +0 -19
  141. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/destroy_api_client.rb +0 -20
  142. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/destroy_cloud_ip.rb +0 -20
  143. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/destroy_image.rb +0 -20
  144. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/destroy_load_balancer.rb +0 -20
  145. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/destroy_server.rb +0 -20
  146. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/get_account.rb +0 -19
  147. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/get_api_client.rb +0 -20
  148. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/get_cloud_ip.rb +0 -20
  149. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/get_image.rb +0 -20
  150. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/get_interface.rb +0 -20
  151. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/get_load_balancer.rb +0 -20
  152. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/get_server.rb +0 -20
  153. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/get_server_type.rb +0 -20
  154. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/get_user.rb +0 -20
  155. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/get_zone.rb +0 -20
  156. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/list_api_clients.rb +0 -19
  157. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/list_cloud_ips.rb +0 -19
  158. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/list_images.rb +0 -19
  159. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/list_load_balancers.rb +0 -19
  160. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/list_server_types.rb +0 -19
  161. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/list_servers.rb +0 -19
  162. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/list_users.rb +0 -19
  163. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/list_zones.rb +0 -19
  164. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/map_cloud_ip.rb +0 -20
  165. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/remove_listeners_load_balancer.rb +0 -20
  166. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/remove_nodes_load_balancer.rb +0 -20
  167. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/reset_ftp_password_account.rb +0 -19
  168. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/resize_server.rb +0 -19
  169. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/shutdown_server.rb +0 -20
  170. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/snapshot_server.rb +0 -20
  171. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/start_server.rb +0 -20
  172. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/stop_server.rb +0 -20
  173. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/unmap_cloud_ip.rb +0 -20
  174. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/update_account.rb +0 -20
  175. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/update_api_client.rb +0 -21
  176. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/update_image.rb +0 -21
  177. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/update_load_balancer.rb +0 -21
  178. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/update_server.rb +0 -21
  179. data/lib/bbcloud/vendor/fog/lib/fog/compute/requests/brightbox/update_user.rb +0 -21
  180. data/lib/bbcloud/vendor/fog/lib/fog/core.rb +0 -32
  181. data/lib/bbcloud/vendor/fog/lib/fog/core/attributes.rb +0 -178
  182. data/lib/bbcloud/vendor/fog/lib/fog/core/collection.rb +0 -127
  183. data/lib/bbcloud/vendor/fog/lib/fog/core/connection.rb +0 -35
  184. data/lib/bbcloud/vendor/fog/lib/fog/core/credentials.rb +0 -92
  185. data/lib/bbcloud/vendor/fog/lib/fog/core/deprecation.rb +0 -23
  186. data/lib/bbcloud/vendor/fog/lib/fog/core/errors.rb +0 -20
  187. data/lib/bbcloud/vendor/fog/lib/fog/core/hmac.rb +0 -48
  188. data/lib/bbcloud/vendor/fog/lib/fog/core/mock.rb +0 -68
  189. data/lib/bbcloud/vendor/fog/lib/fog/core/model.rb +0 -57
  190. data/lib/bbcloud/vendor/fog/lib/fog/core/parser.rb +0 -99
  191. data/lib/bbcloud/vendor/fog/lib/fog/core/provider.rb +0 -18
  192. data/lib/bbcloud/vendor/fog/lib/fog/core/scp.rb +0 -67
  193. data/lib/bbcloud/vendor/fog/lib/fog/core/service.rb +0 -175
  194. data/lib/bbcloud/vendor/fog/lib/fog/core/ssh.rb +0 -120
  195. data/lib/bbcloud/vendor/fog/lib/fog/core/time.rb +0 -27
  196. data/lib/bbcloud/vendor/fog/lib/fog/core/wait_for.rb +0 -17
  197. data/lib/bbcloud/vendor/fog/lib/fog/providers.rb +0 -9
  198. data/lib/bbcloud/vendor/fog/lib/fog/providers/brightbox.rb +0 -9
@@ -0,0 +1,124 @@
1
+ require 'gli/command_line_token.rb'
2
+ require 'gli/copy_options_to_aliases.rb'
3
+
4
+ module GLI
5
+ # A command to be run, in context of global flags and switches. You are given an instance of this class
6
+ # to the block you use for GLI#command. You then use the methods described here to describe the
7
+ # command-specific command-line arguments, much as you use the methods in GLI to describe the global
8
+ # command-line interface
9
+ class Command < CommandLineToken
10
+ include CopyOptionsToAliases
11
+
12
+ # Create a new command
13
+ #
14
+ # +names+:: A String, Symbol, or Array of String or Symbol that represents the name(s) of this command.
15
+ # +description+:: short description of this command as a Strign
16
+ # +arguments_name+:: description of the arguments as a String, or nil if this command doesn't take arguments
17
+ # +long_desc+:: a longer description of the command, possibly with multiple lines and text formatting
18
+ # +skips_pre+:: if true, this command advertises that it doesn't want the pre block called first
19
+ # +skips_post+:: if true, this command advertises that it doesn't want the post block called after it
20
+ def initialize(names,description,arguments_name=nil,long_desc=nil,skips_pre=false,skips_post=false) # :nodoc:
21
+ super(names,description,long_desc)
22
+ @arguments_description = arguments_name || ''
23
+ @skips_pre = skips_pre
24
+ @skips_post = skips_post
25
+ clear_nexts
26
+ end
27
+
28
+ # Return the arguments description
29
+ def arguments_description #:nodoc:
30
+ @arguments_description
31
+ end
32
+
33
+ # If true, this command doesn't want the pre block run before it executes
34
+ def skips_pre #:nodoc:
35
+ @skips_pre
36
+ end
37
+
38
+ # If true, this command doesn't want the post block run before it executes
39
+ def skips_post #:nodoc:
40
+ @skips_post
41
+ end
42
+
43
+ # Return the Array of the command's names
44
+ def names #:nodoc:
45
+ all_forms
46
+ end
47
+
48
+ # Get the usage string
49
+ # CR: This should probably not be here
50
+ def usage #:nodoc:
51
+ usage = name.to_s
52
+ usage += ' [command options]' if !flags.empty? || !switches.empty?
53
+ usage += ' ' + @arguments_description if @arguments_description
54
+ usage
55
+ end
56
+
57
+ # Return the flags as a Hash
58
+ def flags #:nodoc:
59
+ @flags ||= {}
60
+ end
61
+ # Return the switches as a Hash
62
+ def switches #:nodoc:
63
+ @switches ||= {}
64
+ end
65
+
66
+ # describe the next switch or flag just as GLI#desc does.
67
+ def desc(description); @next_desc = description; end
68
+ # set the long description of this flag/switch, just as GLI#long_desc does.
69
+ def long_desc(long_desc); @next_long_desc = long_desc; end
70
+ # describe the argument name of the next flag, just as GLI#arg_name does.
71
+ def arg_name(name); @next_arg_name = name; end
72
+ # set the default value of the next flag, just as GLI#default_value does.
73
+ def default_value(val); @next_default_value = val; end
74
+
75
+ # Create a command-specific flag, similar to GLI#flag
76
+ def flag(*names)
77
+ names = [names].flatten
78
+ GLI.verify_unused(names,flags,switches,"in command #{name}")
79
+ flag = Flag.new(names,@next_desc,@next_arg_name,@next_default_value,@next_long_desc)
80
+ flags[flag.name] = flag
81
+ clear_nexts
82
+ end
83
+
84
+ # Create a command-specific switch, similar to GLI#switch
85
+ def switch(*names)
86
+ names = [names].flatten
87
+ GLI.verify_unused(names,flags,switches,"in command #{name}")
88
+ switch = Switch.new(names,@next_desc,@next_long_desc)
89
+ switches[switch.name] = switch
90
+ clear_nexts
91
+ end
92
+
93
+ # Define the action to take when the user executes this command
94
+ #
95
+ # +block+:: A block of code to execute. The block will be given 3 arguments:
96
+ # +global_options+:: A Hash (or Options, see GLI#use_openstruct) of the _global_ options specified
97
+ # by the user, with defaults set and config file values used (if using a config file, see
98
+ # GLI#config_file)
99
+ # +options+:: A Hash (or Options, see GLI#use_openstruct) of the command-specific options specified by the
100
+ # user, with defaults set and config file values used (if using a config file, see GLI#config_file)
101
+ # +arguments+:: An Array of Strings representing the unparsed command line arguments
102
+ # The block's result value is not used; raise an exception or use GLI#exit_now! if you need an early exit based
103
+ # on an error condition
104
+ def action(&block)
105
+ @action = block
106
+ end
107
+
108
+ def self.name_as_string(name) #:nodoc:
109
+ name.to_s
110
+ end
111
+
112
+ def clear_nexts #:nodoc:
113
+ @next_desc = nil
114
+ @next_arg_name = nil
115
+ @next_default_value = nil
116
+ @next_long_desc = nil
117
+ end
118
+
119
+ # Executes the command
120
+ def execute(global_options,options,arguments) #:nodoc:
121
+ @action.call(global_options,options,arguments)
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,58 @@
1
+ module GLI
2
+ # Abstract base class for a logical element of a command line, mostly so that subclasses can have similar
3
+ # initialization and interface
4
+ class CommandLineToken
5
+ attr_reader :name #:ndoc:
6
+ attr_reader :aliases #:ndoc:
7
+ attr_reader :description #:ndoc:
8
+ attr_reader :long_description #:ndoc:
9
+
10
+ def initialize(names,description,long_description=nil) #:ndoc:
11
+ @description = description
12
+ @long_description = long_description
13
+ @name,@aliases,@names = parse_names(names)
14
+ end
15
+
16
+ def usage #:nodoc:
17
+ all_forms
18
+ end
19
+
20
+ # Sort based on name
21
+ def <=>(other)
22
+ self.name.to_s <=> other.name.to_s
23
+ end
24
+
25
+ private
26
+ # Returns a string of all possible forms
27
+ # of this flag. Mostly intended for printing
28
+ # to the user.
29
+ def all_forms(joiner=', ')
30
+ forms = all_forms_a
31
+ forms.join(joiner)
32
+ end
33
+
34
+
35
+ # Handles dealing with the "names" param, parsing
36
+ # it into the primary name and aliases list
37
+ def parse_names(names)
38
+ # Allow strings; convert to symbols
39
+ names = [names].flatten.map { |name| name.to_sym }
40
+ names_hash = Hash.new
41
+ names.each do |name|
42
+ raise ArgumentError.new("#{name} has spaces; they are not allowed") if name.to_s =~ /\s/
43
+ names_hash[self.class.name_as_string(name)] = true
44
+ end
45
+ name = names.shift
46
+ aliases = names.length > 0 ? names : nil
47
+ [name,aliases,names_hash]
48
+ end
49
+
50
+ def all_forms_a
51
+ forms = [self.class.name_as_string(name)]
52
+ if aliases
53
+ forms |= aliases.collect { |one_alias| self.class.name_as_string(one_alias) }.sort { |one,two| two.length <=> one.length }
54
+ end
55
+ forms
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,33 @@
1
+ module GLI
2
+
3
+ # Mixin that both GLI and Command can use to copy command-line options to the aliased versions
4
+ # of flags and switches
5
+ #
6
+ # includers must provide the methods +flags+ and +switches+ that return an Array of Flag or Switch,
7
+ # respectively
8
+ module CopyOptionsToAliases # :nodoc:
9
+ # For each option in options, copies its value to keys for the aliases of the flags or
10
+ # switches in gli_like
11
+ #
12
+ # options - Hash of options parsed from command line; this is an I/O param
13
+ def copy_options_to_aliases(options) # :nodoc:
14
+ new_options = {}
15
+ options.each do |key,value|
16
+ if flags[key] && flags[key].aliases
17
+ copy_aliases(flags[key].aliases,new_options,value)
18
+ elsif switches[key] && switches[key].aliases
19
+ copy_aliases(switches[key].aliases,new_options,value)
20
+ end
21
+ end
22
+ options.merge!(new_options)
23
+ end
24
+
25
+ private
26
+
27
+ def copy_aliases(aliases,new_options,value)
28
+ aliases.each do |alias_name|
29
+ new_options[alias_name] = value
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,45 @@
1
+ module GLI
2
+ # Indicates that the command line invocation was bad
3
+ class BadCommandLine < Exception
4
+ def exit_code; -1; end
5
+ end
6
+
7
+ # Indicates the bad command line was an unknown command
8
+ class UnknownCommand < BadCommandLine
9
+ end
10
+
11
+ # Indicates the bad command line was an unknown global argument
12
+ class UnknownGlobalArgument < BadCommandLine
13
+ end
14
+
15
+ # Indicates the bad command line was an unknown command argument
16
+ class UnknownCommandArgument < BadCommandLine
17
+ attr_reader :command
18
+ # +message+:: the error message to show the user
19
+ # +command+:: the command we were using to parse command-specific options
20
+ def initialize(message,command)
21
+ super(message)
22
+ @command = command
23
+ end
24
+ end
25
+
26
+ # Raise this if you want to use an exit status that isn't the default
27
+ # provided by GLI. Note that GLI#exit_now! might be a bit more to your liking.
28
+ #
29
+ # Example:
30
+ #
31
+ # raise CustomExit.new("Not connected to DB",-5) unless connected?
32
+ # raise CustomExit.new("Bad SQL",-6) unless valid_sql?(args[0])
33
+ #
34
+ class CustomExit < Exception
35
+ attr_reader :exit_code #:nodoc:
36
+ # Create a custom exit exception
37
+ #
38
+ # +message+:: String containing error message to show the user
39
+ # +exit_code+:: the exit code to use (as an Int), overridding GLI's default
40
+ def initialize(message,exit_code)
41
+ super(message)
42
+ @exit_code = exit_code
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,67 @@
1
+ require 'gli/command_line_token.rb'
2
+ require 'gli/switch.rb'
3
+
4
+ module GLI
5
+ # Defines a flag, which is to say a switch that takes an argument
6
+ class Flag < Switch # :nodoc:
7
+
8
+ attr_accessor :default_value
9
+
10
+ def initialize(names,description,argument_name=nil,default=nil,long_desc=nil)
11
+ super(names,description,long_desc)
12
+ @argument_name = argument_name || "arg"
13
+ @default_value = default
14
+ end
15
+
16
+ def get_value!(args)
17
+ args.each_index() do |index|
18
+ arg = args[index]
19
+ present,matched,value = find_me(arg)
20
+ if present
21
+ args.delete_at index
22
+ if !value || value == ''
23
+ if args[index]
24
+ value = args[index]
25
+ args.delete_at index
26
+ return value
27
+ else
28
+ raise BadCommandLine.new("#{matched} requires an argument")
29
+ end
30
+ else
31
+ return value
32
+ end
33
+ end
34
+ end
35
+ return @default_value
36
+ end
37
+
38
+ def find_me(arg)
39
+ if @names[arg]
40
+ return [true,arg,nil] if arg.length == 2
41
+ # This means we matched the long-form, but there's no argument
42
+ raise BadCommandLine.new("#{arg} requires an argument via #{arg}=argument")
43
+ end
44
+ @names.keys.each() do |name|
45
+ match_string = "^#{name}=(.*)$"
46
+ match_data = arg.match(match_string)
47
+ return [true,name,$1] if match_data;
48
+ end
49
+ [false,nil,nil]
50
+ end
51
+
52
+ # Returns a string of all possible forms
53
+ # of this flag. Mostly intended for printing
54
+ # to the user.
55
+ def all_forms(joiner=', ')
56
+ forms = all_forms_a
57
+ string = forms.join(joiner)
58
+ if forms[-1] =~ /^\-\-/
59
+ string += '='
60
+ else
61
+ string += ' '
62
+ end
63
+ string += @argument_name
64
+ return string
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,19 @@
1
+ require 'ostruct'
2
+
3
+ module GLI
4
+ # Subclass of OpenStruct that provides hash-like methods for #[] and #[]=. Note that is is *not* a Hash.
5
+ class Options < OpenStruct
6
+
7
+ # Return the value of an attribute
8
+ def[](k)
9
+ @table[k.to_sym]
10
+ end
11
+
12
+ # Set the value of an attribute
13
+ def[]=(k, v)
14
+ @table[k.to_sym] = v
15
+ end
16
+
17
+ end
18
+ end
19
+
@@ -0,0 +1,63 @@
1
+ require 'gli/command_line_token.rb'
2
+
3
+ module GLI
4
+ # Defines a command line switch
5
+ class Switch < CommandLineToken #:nodoc:
6
+
7
+ def initialize(names,description,long_desc=nil)
8
+ super(names,description,long_desc)
9
+ @default_value = false
10
+ end
11
+
12
+ # Given the argument list, scans it looking for this switch
13
+ # returning true if it's in the argumennt list (and removing it from the argument list)
14
+ def get_value!(args)
15
+ idx = -1
16
+ args.each_index do |index|
17
+ result = find_me(args[index])
18
+ if result[0]
19
+ if result[1]
20
+ args[index] = result[1]
21
+ else
22
+ args.delete_at index
23
+ end
24
+ return result[0]
25
+ end
26
+ end
27
+ @default_value
28
+ end
29
+
30
+ # Used only to configure what's returned if we do not detect this switch on the command line
31
+ # This allows the configuration file to set a switch as always on
32
+ def default_value=(default)
33
+ @default_value = default
34
+ end
35
+
36
+ # Finds the switch in the given arg, returning the arg to keep.
37
+ # Returns an array of size 2:
38
+ # index 0:: true or false if the arg was found
39
+ # index 1:: the remaining arg to keep in the command line or nil to remove it
40
+ def find_me(arg)
41
+ if @names[arg]
42
+ return [true,nil]
43
+ end
44
+ @names.keys.each() do |name|
45
+ if name =~ /^-(\w)$/
46
+ match_string = "^\\-(\\w*)#{$1}(\\w*)$"
47
+ match_data = arg.match(match_string)
48
+ if match_data
49
+ # Note that if [1] and [2] were both empty
50
+ # we'd have returned above
51
+ return [true, "-" + match_data[1] + match_data[2]]
52
+ end
53
+ end
54
+ end
55
+ [false]
56
+ end
57
+
58
+ def self.name_as_string(name)
59
+ string = name.to_s
60
+ string.length == 1 ? "-#{string}" : "--#{string}"
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,79 @@
1
+ module GLI
2
+ # Class to encapsulate stuff about the terminal. This is useful to application developers
3
+ # as a canonical means to get information about the user's current terminal configuraiton.
4
+ # GLI uses this to determine the number of columns to use when printing to the screen.
5
+ #
6
+ # To access it, use Terminal#instance. This is a singleton mostly to facilitate testing, but
7
+ # it seems reasonable enough, since there's only one terminal in effect
8
+ #
9
+ # Example:
10
+ #
11
+ # Terminal.instance.size[0] # => columns in the terminal
12
+ # Terminal.default_size = [128,24] # => change default when we can't figure it out
13
+ # raise "no ls?!?!?" unless Terminal.instance.command_exists?("ls")
14
+ #
15
+ class Terminal
16
+
17
+ @@default_size = [80,24]
18
+
19
+ # Get the default size of the terminal when we can't figure it out
20
+ #
21
+ # Returns an array of int [cols,rows]
22
+ def self.default_size
23
+ @@default_size
24
+ end
25
+
26
+ # Set the default size of the terminal to use when we can't figure it out
27
+ #
28
+ # +size+:: array of two int [cols,rows]
29
+ def self.default_size=(size)
30
+ @@default_size = size
31
+ end
32
+
33
+ # Provide access to the shared instance.
34
+ def self.instance; @@instance ||= Terminal.new; end
35
+
36
+ # Call this to cause methods to throw exceptions rather than return a sane default. You
37
+ # probably don't want to call this unless you are writing tests
38
+ def make_unsafe!
39
+ @unsafe = true
40
+ end
41
+
42
+ # Returns true if the given command exists on this system
43
+ #
44
+ # +command+:: The command, as a String, to check for, without any path information.
45
+ def command_exists?(command)
46
+ ENV['PATH'].split(File::PATH_SEPARATOR).any? {|dir| File.exists? File.join(dir, command) }
47
+ end
48
+
49
+ # Get the size of the current terminal.
50
+ # Ripped from hirb[https://github.com/cldwalker/hirb/blob/master/lib/hirb/util.rb]
51
+ #
52
+ # Returns an Array of size two Ints representing the terminal width and height
53
+ def size
54
+ if (ENV['COLUMNS'] =~ /^\d+$/) && (ENV['LINES'] =~ /^\d+$/)
55
+ [ENV['COLUMNS'].to_i, ENV['LINES'].to_i]
56
+ elsif (jruby? || (!STDIN.tty? && ENV['TERM'])) && command_exists?('tput')
57
+ [run_command('tput cols').to_i, run_command('tput lines').to_i]
58
+ elsif STDIN.tty? && command_exists?('stty')
59
+ run_command('stty size').scan(/\d+/).map { |size_element| size_element.to_i }.reverse
60
+ else
61
+ Terminal.default_size
62
+ end
63
+ rescue Exception => ex
64
+ raise ex if @unsafe
65
+ Terminal.default_size
66
+ end
67
+
68
+ private
69
+
70
+ # Runs a command using backticks. Extracted to allow for testing
71
+ def run_command(command)
72
+ `#{command}`
73
+ end
74
+
75
+ # True if we are JRuby; exposed to allow for testing
76
+ def jruby?; RUBY_PLATFORM =~ /java/; end
77
+
78
+ end
79
+ end