optionscrapper 0.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 55227d37e4e22dc9ff1625a0cc6257ed76326b6a
4
+ data.tar.gz: 714240abbc84f62c81e0f9e2178ba1b815709ed4
5
+ SHA512:
6
+ metadata.gz: f22b3bdf77e3b01bda61d634d509aa0eec9a3e46bbe08c0010a00975293b4e47571f76e7ee84b762ec8f3a73cc6430db5d327e78cc50a73d0aaaab05e2e277a7
7
+ data.tar.gz: 7e54275583b4aeba65daeeba88cec9fd342192151ba29c9243292710cf132cd50389d0fd17286ab12dedb2157257b60c0d2c8134fff35c6829b2b6518e31b3d4
data/.gitignore ADDED
@@ -0,0 +1,42 @@
1
+ #
2
+ # Author: Rohith
3
+ # Date: 2014-05-22 16:52:19 +0100 (Thu, 22 May 2014)
4
+ #
5
+ # vim:ts=4:sw=4:et
6
+ #
7
+
8
+ *.swp
9
+ *.gem
10
+ *.rbc
11
+ /.config
12
+ /coverage/
13
+ /InstalledFiles
14
+ /pkg/
15
+ /spec/reports/
16
+ /test/tmp/
17
+ /test/version_tmp/
18
+ /tmp/
19
+
20
+ ## Specific to RubyMotion:
21
+ .dat*
22
+ .repl_history
23
+ build/
24
+
25
+ ## Documentation cache and generated files:
26
+ /.yardoc/
27
+ /_yardoc/
28
+ /doc/
29
+ /rdoc/
30
+
31
+ ## Environment normalisation:
32
+ /.bundle/
33
+ /lib/bundler/man/
34
+
35
+ # for a library or gem, you might want to ignore these files since the code is
36
+ # intended to run in multiple environments; otherwise, check them in:
37
+ # Gemfile.lock
38
+ # .ruby-version
39
+ # .ruby-gemset
40
+
41
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
42
+ .rvmrc
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ Options Scrapper!
2
+ ----------------
3
+
4
+ Is a wrapper for the OptionsParser (optparse) gem which makes using subcommand like cli easier to define. Note, all the options configuration is passed directly to optparse; so anything it supports, this will support; any methods missing is also passed down to optpasr i.e. banner, separator etc
5
+
6
+ require 'optionscrapper'
7
+ require 'pp'
8
+
9
+ @options = {
10
+ :config => '../config/openstack.yaml',
11
+ :flavor => '2cores-4096mem-10gb',
12
+ :image => 'centos-base-6.5-min-stable',
13
+ :user_data => '../config/user_data.erb',
14
+ :keypair => 'default',
15
+ :networks => [],
16
+ :security_group => [ 'default' ],
17
+ :verbose => true,
18
+ :force => false
19
+ }
20
+
21
+ begin
22
+
23
+ Parser = OptionScrapper::new do |o|
24
+ o.on( '-S stack', '--stack NAME', 'the name of the openstack you wish to connect' ) { |x| @options[:stack] = x }
25
+ o.on( '-c CONFIG', '--config CONFIG', 'the configuration file to read credentials' ) { |x| @options[:config] = x }
26
+ o.on( '-H', '--hostname HOSTNAME', 'switch on verbose mode' ) { @options[:verbose] = true }
27
+ o.on( '-v', '--verbose', 'switch on verbose mode' ) { @options[:verbose] = true }
28
+ o.command :launch, 'launch a instance in to openstack cluster' do
29
+ o.on( '-H HOSTNAME', '--hostname HOSTNAME', 'the hostname of instance you are creating' ) { |x| @options[:hostname] = x }
30
+ o.on( '-i IMAGE', '--image IMAGE', 'the image you wish to boot from' ) { |x| @options[:image] = x }
31
+ o.on( '-f FLAVOR', '--flavor FLAVOR', 'the flavor the instance should work from' ) { |x| @options[:flavor] = x }
32
+ o.on( '-k KEYPAIR', '--keypair KEYPAIR', 'the keypair the instance should use' ) { |x| @options[:keypair] = x }
33
+ o.on( '-n NETWORK', '--network NETWORK', 'the network the instance should be connected' ) { |x| @options[:networks] << x }
34
+ o.on( '-s SECURITY', '--secgroups SECURITY', 'the security group assigned to the instance' ) { |x| @options[:security_group] << x }
35
+ o.on( '-u USER_DATA', '--user-data USER_DATA', 'the user data template' ) { |x| @options[:user_data] = x }
36
+ o.on( nil, '--hypervisor HOST', 'the compute node you want the instance to run' ) { |x| @options[:availability_zone] = x }
37
+ o.on_command { @options[:action] = :launch }
38
+ end
39
+ o.command :destroy, 'destroy and delete an instance in openstack' do
40
+ o.on( '-H HOSTNAME', '--hostname HOSTNAME', 'the hostname of instance you are creating' ) { |x| @options[:hostname] = x }
41
+ o.on_command { @options[:action] = :destroy }
42
+ end
43
+ o.command :snapshot, 'snapshot a instance within openstack' do
44
+ o.on( '-H HOSTNAME', '--hostname HOSTNAME', 'the hostname of the instance being snapshot' ) { |x| @options[:hostname] = x }
45
+ o.on( '-s NAME', '--snapshot NAME', 'the name of the snapshot you are creating' ) { |x| @options[:snapshot] = x }
46
+ o.on( nil, '--wait' 'wait on the snapshot to complete' ) { |x| @options[:wait] = true }
47
+ o.on( '-f', '--force', 'if the snapshot image exists, delete it' ) { |x| @options[:force] = true }
48
+ o.on_command { @options[:action] = :snapshot }
49
+ end
50
+ end
51
+ Parser.parse!
52
+ PP.pp @options
53
+ rescue SystemExit => e
54
+ exit e.status
55
+ end
56
+
57
+ Global Options
58
+ --------------
59
+
60
+ Assuming you are using subcommands, global options may be intermixed amougst the subcommands - i.e. if you have an option --dry-run in global, it can be placed anywhere within the command line options. If however, the subcommand which it is seated has the same option, the subcommand takes priority. Using the above definition as an example
61
+
62
+ ./test launch -H hostname ; -H exists in global and launch, but launch take priority and it's his to process
63
+
@@ -0,0 +1,10 @@
1
+ #
2
+ #
3
+ # Author: Rohith
4
+ # Date: 2014-07-22 23:30:04 +0100 (Tue, 22 Jul 2014)
5
+ #
6
+ # vim:ts=4:sw=4:et
7
+ #
8
+ class OptionParser
9
+ attr_reader :stack
10
+ end
@@ -0,0 +1,95 @@
1
+ #
2
+ # Author: Rohith
3
+ # Date: 2014-05-22 23:55:29 +0100 (Thu, 22 May 2014)
4
+ #
5
+ # vim:ts=2:sw=2:et
6
+ #
7
+ $:.unshift File.join(File.dirname(__FILE__),'.')
8
+ require 'parsing'
9
+ require 'usage'
10
+
11
+ module OptionScrapper
12
+ class OptParser
13
+ include OptionScrapper::Parsing
14
+ include OptionScrapper::Usage
15
+
16
+ alias_method :newline, :puts
17
+
18
+ def initialize &block
19
+ initialize_parsers
20
+ yield self if block_given?
21
+ end
22
+
23
+ def parse! arguments = ARGV
24
+ # step: we need to separate into subcommand arguments
25
+ batches = batch_arguments arguments, parsers
26
+ # step: iterate the batches and fire off the parsers for each subcommand
27
+ batches.each_pair { |cmd,args| parsers[cmd][:parser].parse! args }
28
+ end
29
+
30
+ def command name, description, &block
31
+ # step: create a new command parser
32
+ command_name = name.to_sym
33
+ # step: create a new command parser
34
+ p = parser( command_name, description )
35
+ # step: add a spacer to the current one
36
+ @cursor[:parser].separator ""
37
+ # step: add the new parser to the @parsers
38
+ parsers[command_name] = p
39
+ # step: update the cursor to the new parser
40
+ @cursor = p
41
+ # step: create a useage for this command
42
+ @cursor[:parser].banner = " %s : description: %s" % [ name, description ]
43
+ @cursor[:parser].separator " %s" % [ horizontal_line( 72 ) ]
44
+ @cursor[:parser].separator ""
45
+ yield @cursor[:parser] if block_given?
46
+ end
47
+
48
+ def command_alias name
49
+ @cursor[:aliases] << name
50
+ end
51
+
52
+ def on_command &block
53
+ @cursor[:on_command] = block if block_given?
54
+ end
55
+
56
+ def on *args, &block
57
+ # step: we are creating an array of all the
58
+ parse_option_switches *args do |x|
59
+ @cursor[:switches][x] = true
60
+ end
61
+ @cursor[:parser].on *args do |x|
62
+ # step: build up a list of switches for this command
63
+ yield x if block_given?
64
+ end
65
+ end
66
+
67
+ private
68
+ def method_missing method, *args, &block
69
+ if @cursor[:parser].respond_to? method
70
+ case method
71
+ when :banner=
72
+ @cursor[:parser].send method, args.first, &block
73
+ else
74
+ @cursor[:parser].send method, args, &block if args and !args.empty?
75
+ @cursor[:parser].send method, &block if !args or args.empty?
76
+ end
77
+ else
78
+ super( method, args, block )
79
+ end
80
+ end
81
+
82
+ def initialize_parsers
83
+ # step: we create the global and inject the global parser into the parser hash
84
+ parsers[:global] = parser( 'global' )
85
+ parsers[:global][:parser].program_name = program_name
86
+ # step: set the cursor to global - i.e. all options are initially global
87
+ @cursor = parsers[:global]
88
+ # step: inject a default help options for global
89
+ @cursor[:parser].on( '-h', '--help', 'display this usage menu' ) do
90
+ puts print_usage
91
+ exit 0
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,123 @@
1
+ #
2
+ # Author: Rohith (gambol99@gmail.com)
3
+ # Date: 2014-07-11 14:26:25 +0100 (Fri, 11 Jul 2014)
4
+ #
5
+ # vim:ts=4:sw=4:et
6
+ #
7
+ module OptionScrapper
8
+ module Parsing
9
+ OptionRegex = /^(-[-]?[[:alpha:]-]+)/
10
+
11
+ private
12
+ # [ -c config launch -H rohith -i djskdjs -n 2 -f dksldkslkdsldksl --stack hq --dry-run -f mine ]
13
+ # [ -c config launch -H rohith -i djskdjs -n 2 -f dksldkslkdsldksl --dry-run -S hq ]
14
+ def batch_arguments arguments = ARGV, commands = parsers
15
+ # step: create the batches
16
+ batches = { :global => [] }
17
+ current = :global
18
+ previous = nil
19
+
20
+ arguments.each do |argument|
21
+ # step: is the argument a subcommand?
22
+ subcommand = has_command argument
23
+ if subcommand
24
+ current = subcommand
25
+ previous = nil
26
+ # step: create the new batch, reset the cursor and iterate
27
+ batches[current] = []
28
+ # step: call the block if the on_command block is set
29
+ parsers[current][:on_command].call if parsers[current].has_key? :on_command
30
+ else
31
+ unless option? argument
32
+ batches[current] << argument; next
33
+ end
34
+ # else we are processing a command line option and we are in global
35
+ if previous
36
+ current = previous
37
+ previous = nil
38
+ end
39
+
40
+ if !parser_option?( current, argument ) and global_option? argument
41
+ previous = current
42
+ current = :global
43
+ batches[current] << argument
44
+ else
45
+ # step: otherwise we inject into the current batch
46
+ batches[current] << argument
47
+ end
48
+ end
49
+ end
50
+ batches
51
+ end
52
+
53
+ def parser name, description = nil
54
+ p = {
55
+ :name => name.to_sym,
56
+ :parser => ::OptionParser::new,
57
+ :switches => {},
58
+ :aliases => [],
59
+ }
60
+ p[:description] = description if description
61
+ p
62
+ end
63
+
64
+ def has_command argument
65
+ command_name = argument.to_sym
66
+ return command_name if command? command_name
67
+ alias?( command_name )
68
+ end
69
+
70
+ def command? argument
71
+ parsers.has_key? argument
72
+ end
73
+
74
+ def alias? argument
75
+ parsers.each_pair do |name,config|
76
+ next if name == :global
77
+ next if aliases( name ).empty?
78
+ return name if aliases( name ).include? argument
79
+ end
80
+ nil
81
+ end
82
+
83
+ def aliases parser_name
84
+ parsers[parser_name][:aliases]
85
+ end
86
+
87
+ def parsers
88
+ @parsers ||= {}
89
+ end
90
+
91
+ def option? argument
92
+ argument =~ OptionRegex
93
+ end
94
+
95
+ def parser_option? parser_name, option
96
+ parsers[parser_name][:switches].has_key? option
97
+ end
98
+
99
+ def global_parser
100
+ parsers[:global][:parser]
101
+ end
102
+
103
+ def global_switches
104
+ parsers[:global][:switches]
105
+ end
106
+
107
+ def global_option? option
108
+ global_switches.has_key? option
109
+ end
110
+
111
+ def parse_option_switches *args, &block
112
+ if args and args.size >= 2
113
+ args[0..1].each do |a|
114
+ yield $1 if a =~ OptionRegex and block_given?
115
+ end
116
+ end
117
+ end
118
+
119
+ def program_name
120
+ File.basename( $0 )
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,65 @@
1
+ #
2
+ # Author: Rohith (gambol99@gmail.com)
3
+ # Date: 2014-07-11 14:39:16 +0100 (Fri, 11 Jul 2014)
4
+ #
5
+ # vim:ts=4:sw=4:et
6
+ #
7
+ module OptionScrapper
8
+ module Usage
9
+ def usage message = nil, parser_name = :global
10
+ # step: if the parser is specified, print only that one
11
+ if !parser_name == :global
12
+ puts parsers[parser_name][:parser]
13
+ else
14
+ # step: else we generate the full parse usage menu
15
+ puts "\n%s" % [ parsers[:global][:parser] ]
16
+ # step: we don't need to do this if there are no sub commands
17
+ subcommand_usage
18
+ end
19
+ fail message if message
20
+ newline
21
+ exit 0
22
+ end
23
+ alias_method :print_usage, :usage
24
+ alias_method :to_s, :usage
25
+
26
+ def subcommand_usage
27
+ if parsers.size > 1
28
+ puts offset << "commands : %s" % [ horizontal_line( 61, "-" ) ]
29
+ parsers.each_pair do |name,par|
30
+ next if name == :global
31
+ subcommand_name = name.to_s
32
+ subcommand_name << " (%s)" % [ aliases( name ).join(',') ] unless par[:aliases].empty?
33
+ puts offset << "%-32s %s" % [ subcommand_name, par[:description] ]
34
+ end
35
+ puts offset << "%s" % [ horizontal_line( 72, "-" ) ]
36
+ newline
37
+ parsers.each_pair do |parser_name,p|
38
+ # step: skip the global, we have already displayed it
39
+ next if parser_name == :global
40
+ # step: we don't need to show this if the subcommand has no options / switches
41
+ next if p[:switches].empty?
42
+ # step: else we can show the parser usage
43
+ puts p[:parser]
44
+ end
45
+ end
46
+ end
47
+
48
+ def offset length = 4, spacer = ""
49
+ length.times.each { spacer << " " }
50
+ spacer
51
+ end
52
+
53
+ def fail message
54
+ puts "[error]: " << message
55
+ exit 1
56
+ end
57
+
58
+ def horizontal_line length, symbol = '-', line = ""
59
+ length.times.each { line << "#{symbol}" }
60
+ line
61
+ end
62
+ end
63
+ end
64
+
65
+
@@ -0,0 +1,9 @@
1
+ #
2
+ # Author: Rohith
3
+ # Date: 2014-05-22 10:58:44 +0100 (Thu, 22 May 2014)
4
+ #
5
+ # vim:ts=2:sw=2:et
6
+ #
7
+ module OptionScrapper
8
+ VERSION = "0.0.9"
9
+ end
@@ -0,0 +1,32 @@
1
+ #
2
+ # Author: Rohith
3
+ # Date: 2014-05-22 23:55:29 +0100 (Thu, 22 May 2014)
4
+ #
5
+ # vim:ts=2:sw=2:et
6
+ #
7
+ $:.unshift File.join(File.dirname(__FILE__),'.','./')
8
+ require 'optparse'
9
+ require 'optionscrapper/ext/optparse'
10
+ require 'optionscrapper/version'
11
+ require 'optionscrapper/optparser'
12
+
13
+ module OptionScrapper
14
+ ROOT = File.expand_path File.dirname __FILE__
15
+
16
+ require "#{ROOT}/optionscrapper/version"
17
+
18
+ autoload :Version, "#{ROOT}/optionscrapper/version"
19
+ autoload :Parser, "#{ROOT}/optionscrapper/optparser"
20
+
21
+ @version = OptionScrapper::VERSION
22
+
23
+ def self.version
24
+ OptionScrapper::VERSION
25
+ end
26
+
27
+ def self.new &block
28
+ OptionScrapper::OptParser::new do |o|
29
+ yield o if block_given?
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Author: Rohith
4
+ # Date: 2014-06-06 16:48:00 +0100 (Thu, 06 Jun 2014)
5
+ #
6
+ # vim:ts=2:sw=2:et
7
+ #
8
+ $:.unshift File.join(File.dirname(__FILE__),'.','lib/optionscrapper' )
9
+ require 'version'
10
+
11
+ Gem::Specification.new do |s|
12
+ s.name = "optionscrapper"
13
+ s.version = OptionScrapper::VERSION
14
+ s.platform = Gem::Platform::RUBY
15
+ s.date = '2014-05-22'
16
+ s.authors = ["Rohith Jayawardene"]
17
+ s.email = 'gambol99@gmail.com'
18
+ s.homepage = 'https://github.com/gambol99/optionscrapper'
19
+ s.summary = %q{Options Parser with subcommand supports}
20
+ s.description = %q{Is a wrapper for optparse which allows for using subcommand more easily}
21
+ s.license = 'MIT'
22
+ s.files = `git ls-files`.split("\n")
23
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
24
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
25
+ end
data/tests/test.rb ADDED
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift File.join(File.dirname(__FILE__),'.','../lib')
3
+
4
+ require 'optionscrapper'
5
+ require 'pp'
6
+
7
+ @options = {
8
+ :verbose => true,
9
+ :networks => [],
10
+ :security => []
11
+ }
12
+
13
+ def parser
14
+ @parser ||= OptionScrapper::new do |o|
15
+ o.on( '-S stack', '--stack NAME', 'the name of the openstack you wish to connect' ) { |x| @options[:stack] = x }
16
+ o.on( '-c CONFIG', '--config CONFIG', 'the configuration file to read credentials' ) { |x| @options[:config] = x }
17
+ o.on( nil, '--dry-run', 'perform a dry run' ) { @options[:dry_run] = true }
18
+ o.on( '-v', '--verbose', 'switch on verbose mode' ) { @options[:verbose] = true }
19
+ o.command :launch, 'launch a instance in to openstack cluster' do
20
+ o.command_alias :ln
21
+ o.on( '-H HOSTNAME', '--hostname HOSTNAME', 'the hostname of instance you are creating' ) { |x| @options[:hostname] = x }
22
+ o.on( '-i IMAGE', '--image IMAGE', 'the image you wish to boot from' ) { |x| @options[:image] = x }
23
+ o.on( '-f FLAVOR', '--flavor FLAVOR', 'the flavor the instance should work from' ) { |x| @options[:flavor] = x }
24
+ o.on( '-k KEYPAIR', '--keypair KEYPAIR', 'the keypair the instance should use' ) { |x| @options[:keypair] = x }
25
+ o.on( '-n NETWORK', '--network NETWORK', 'the network the instance should be connected' ) { |x| @options[:networks] << x }
26
+ o.on( '-s SECURITY', '--secgroups SECURITY', 'the security group assigned to the instance' ) { |x| @options[:security_group] << x }
27
+ o.on( '-u USER_DATA', '--user-data USER_DATA', 'the user data template' ) { |x| @options[:user_data] = x }
28
+ o.on( nil, '--hypervisor HOST', 'the compute node you want the instance to run' ) { |x| @options[:availability_zone] = x }
29
+ o.on( '-e', '--error', 'cause an error' ) { o.usage }
30
+ o.on_command { @options[:action] = :launch }
31
+ end
32
+ o.command :destroy, 'destroy and delete an instance in openstack' do
33
+ o.command_alias :des
34
+ o.on( '-H HOSTNAME', '--hostname HOSTNAME', 'the hostname of instance you are creating' ) { |x| @options[:hostname] = x }
35
+ o.on_command { @options[:action] = :destroy }
36
+ end
37
+ o.command :snapshot, 'snapshot a instance within openstack' do
38
+ o.command_alias :sp
39
+ o.on( '-H HOSTNAME', '--hostname HOSTNAME', 'the hostname of the instance being snapshot' ) { |x| @options[:hostname] = x }
40
+ o.on( '-s NAME', '--snapshot NAME', 'the name of the snapshot you are creating' ) { |x| @options[:snapshot] = x }
41
+ o.on( nil, '--wait', 'wait on the snapshot to complete' ) { |x| @options[:wait] = true }
42
+ o.on( '-f', '--force', 'if the snapshot image exists, delete it' ) { |x| @options[:force] = true }
43
+ o.on_command { @options[:action] = :snapshot }
44
+ end
45
+ end
46
+ end
47
+
48
+ begin
49
+ parser.parse!
50
+ PP.pp @options
51
+ rescue SystemExit => e
52
+ exit e.status
53
+ rescue Exception => e
54
+ parser.usage e.message
55
+ end
56
+
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: optionscrapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.9
5
+ platform: ruby
6
+ authors:
7
+ - Rohith Jayawardene
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-22 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Is a wrapper for optparse which allows for using subcommand more easily
14
+ email: gambol99@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - .gitignore
20
+ - README.md
21
+ - lib/optionscrapper.rb
22
+ - lib/optionscrapper/ext/optparse.rb
23
+ - lib/optionscrapper/optparser.rb
24
+ - lib/optionscrapper/parsing.rb
25
+ - lib/optionscrapper/usage.rb
26
+ - lib/optionscrapper/version.rb
27
+ - optionscrapper.gemspec
28
+ - tests/test.rb
29
+ homepage: https://github.com/gambol99/optionscrapper
30
+ licenses:
31
+ - MIT
32
+ metadata: {}
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 2.1.11
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: Options Parser with subcommand supports
53
+ test_files: []