optplus 0.0.8

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.
@@ -0,0 +1,31 @@
1
+ #
2
+ # Author:: Robert Sharp
3
+ # Copyright:: Copyright (c) 2013 Robert Sharp
4
+ # License:: Open Software Licence v3.0
5
+ #
6
+ # This software is licensed for use under the Open Software Licence v. 3.0
7
+ # The terms of this licence can be found at http://www.opensource.org/licenses/osl-3.0.php
8
+ # and in the file copyright.txt. Under the terms of this licence, all derivative works
9
+ # must themselves be licensed under the Open Software Licence v. 3.0
10
+ #
11
+ #
12
+
13
+ # This file groups together all the errors for optplus.
14
+ # Preceed each class with a description of the error
15
+
16
+ module Optplus
17
+
18
+ # A general class for all errors created by this project. All specific exceptions
19
+ # should be children of this class
20
+ class OptplusError < RuntimeError; end
21
+
22
+ # add specific errors as required
23
+
24
+ # description
25
+ class ParseError < OptplusError; end
26
+
27
+ # force an exit
28
+ # @note Should only be used by the exit_on_error method
29
+ class ExitOnError < OptplusError; end
30
+
31
+ end
@@ -0,0 +1,159 @@
1
+ #
2
+ #
3
+ # = Next Parser
4
+ #
5
+ # == SubTitle
6
+ #
7
+ # Author:: Robert Sharp
8
+ # Copyright:: Copyright (c) 2013 Robert Sharp
9
+ # License:: Open Software Licence v3.0
10
+ #
11
+ # This software is licensed for use under the Open Software Licence v. 3.0
12
+ # The terms of this licence can be found at http://www.opensource.org/licenses/osl-3.0.php
13
+ # and in the file copyright.txt. Under the terms of this licence, all derivative works
14
+ # must themselves be licensed under the Open Software Licence v. 3.0
15
+ #
16
+ #
17
+ #
18
+ require 'optplus'
19
+
20
+ module Optplus
21
+
22
+ # define a nested parser to add sub-actions to an action
23
+ #
24
+ # Useful if you want to group actions together. For example,
25
+ # if you have a command that does things for services (for example)
26
+ # and other things for servers (for example) then you can define
27
+ # a nested parser for each and then define actions 'services' and 'servers'
28
+ # that use these parsers:
29
+ #
30
+ # my_command servers list --all
31
+ # my_command services list --all
32
+ #
33
+ # Optplus does not allow option switches to be specific to any one action -
34
+ # its an everything and everywhere approach.
35
+ #
36
+ # Note that NestedParser inherits everything from Parser and could therefore
37
+ # nest another parser within itself, ad infinitum. You could also define
38
+ # methods such as #options which would be a waste because they would never
39
+ # be called.
40
+ #
41
+ class NestedParser < Optplus::Parser
42
+
43
+ class << self
44
+
45
+ # @!visibility private
46
+ def run!(parent)
47
+ # set class attribute so that the instance
48
+ # can determine its parent class
49
+ @_parent = parent
50
+ super()
51
+ end
52
+
53
+ # @!visibility private
54
+ attr_reader :_parent
55
+
56
+ #@!visibility private
57
+ # def _help_me
58
+ # prog_name = File.basename($0, File.extname($0))
59
+ # puts "Usage: #{prog_name} #{self._banner}"
60
+ # puts ""
61
+ # self._description.each do |line|
62
+ # puts line
63
+ # end
64
+ # puts ""
65
+ # self._descriptions.each_pair do |action, description|
66
+ # puts " #{action} - #{description}"
67
+ # end
68
+ # puts ""
69
+ # puts "For full details of options etc:"
70
+ # puts " #{prog_name} -h"
71
+ # end
72
+
73
+ end # class << self
74
+
75
+ # @!visibility private
76
+ def initialize(parent=nil)
77
+ @klass = self.class
78
+ @_parent = parent || @klass._parent
79
+ self.before_all if self.respond_to?(:before_all)
80
+ self.before_actions if self.respond_to?(:before_actions)
81
+ end
82
+
83
+ # @!visibility private
84
+ def _parent
85
+ @_parent
86
+ end
87
+
88
+ # @!visibility private
89
+ def _args
90
+ _parent._args
91
+ end
92
+
93
+ # return the next argument, if there is one or nil otherwise
94
+ # @see Optplus::Parser#next_argument
95
+ def next_argument
96
+ _parent.next_argument
97
+ end
98
+
99
+ # return the next argument or the given default
100
+ # @see Optplus::Parser#next_argument
101
+ def next_argument_or(default)
102
+ _parent.next_argument_or(default)
103
+ end
104
+
105
+ # return the next argument or raise exception with the given message
106
+ # @see Optplus::Parser#next_argument
107
+ def next_argument_or_error(message)
108
+ _parent.next_argument_or_error(message)
109
+ end
110
+
111
+ # return all of the remaining args, or an empty array
112
+ # @see Optplus::Parser#next_argument
113
+ def all_arguments
114
+ _parent.all_arguments
115
+ end
116
+
117
+ # get the value of the option
118
+ # @see Optplus::Parser#next_argument
119
+ def get_option(key)
120
+ _parent.get_option(key)
121
+ end
122
+
123
+ # check if the option has been set
124
+ # @see Optplus::Parser#next_argument
125
+ def option?(key)
126
+ _parent.option?(key)
127
+ end
128
+
129
+ # @!visibility private
130
+ def _get_help
131
+ prog_name = File.basename($0, File.extname($0))
132
+ puts "Usage: #{prog_name} #{self.class._banner}"
133
+ puts ""
134
+ self.class._description.each do |line|
135
+ puts line
136
+ end
137
+ puts ""
138
+ flags = 0
139
+ self.class._descriptions.each_pair do |action, description|
140
+ flag = @klass._help.has_key?(action.to_sym) ? '(-h)' : ''
141
+ flags += 1 unless flag == ''
142
+ puts " #{action} - #{description} #{flag}"
143
+ end
144
+
145
+ if flags > 0 then
146
+ puts ""
147
+ puts " (-h indicates actions with additional help)"
148
+ puts ""
149
+ end
150
+
151
+ puts ""
152
+ puts "For full details of options etc:"
153
+ puts " #{prog_name} -h"
154
+ end
155
+
156
+
157
+ end
158
+
159
+ end
@@ -0,0 +1,13 @@
1
+ # Created by Jevoom
2
+ #
3
+ # 13-Sep-2013
4
+ # Release Candidate with nesting etc.
5
+
6
+ module Optplus
7
+ # version set to 0.0.8
8
+ Version = '0.0.8'
9
+ # date set to 13-Sep-2013
10
+ Version_Date = '13-Sep-2013'
11
+ #ident string set to: optplus-0.0.8 13-Sep-2013
12
+ Ident = 'optplus-0.0.8 13-Sep-2013'
13
+ end
@@ -0,0 +1,41 @@
1
+ #
2
+ # Author:: Robert Sharp
3
+ # Copyright:: Copyright (c) 2013 Robert Sharp
4
+ # License:: Open Software Licence v3.0
5
+ #
6
+ # This software is licensed for use under the Open Software Licence v. 3.0
7
+ # The terms of this licence can be found at http://www.opensource.org/licenses/osl-3.0.php
8
+ # and in the file copyright.txt. Under the terms of this licence, all derivative works
9
+ # must themselves be licensed under the Open Software Licence v. 3.0
10
+ #
11
+ #
12
+
13
+
14
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
15
+ require 'optplus/config'
16
+ require 'optplus/errors'
17
+ require 'jellog'
18
+
19
+ conf_file = File.expand_path(File.dirname(__FILE__) + '/../test/conf.d/optplus.rb')
20
+
21
+ describe Optplus do
22
+
23
+ before(:all) do
24
+ @params = Optplus::Config.new(conf_file)
25
+ Jellog::Logger.disable_syslog
26
+ end
27
+
28
+ before(:each) do
29
+ @optplus = Optplus::Service.new("Testkey", @params)
30
+ end
31
+
32
+ after(:each) do
33
+ @optplus.stop_callback("Testkey")
34
+ end
35
+
36
+
37
+ it "should do what you want" do
38
+ pending "Need to do something here"
39
+ end
40
+
41
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ $LOAD_PATH.delete('/usr/local/lib')
4
+ require 'optplus'
5
+ require 'rspec'
6
+ require 'rspec/autorun'
7
+
8
+ RSpec.configure do |config|
9
+ config.color_enabled = true
10
+ config.formatter = :doc
11
+
12
+ end
@@ -0,0 +1 @@
1
+ Error: could not load lib/optplus/config.rb
@@ -0,0 +1,130 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Author:: Robert Sharp
4
+ # Copyright:: Copyright (c) 2013 Robert Sharp
5
+ # License:: Open Software Licence v3.0
6
+ #
7
+ # This software is licensed for use under the Open Software Licence v. 3.0
8
+ # The terms of this licence can be found at http://www.opensource.org/licenses/osl-3.0.php
9
+ # and in the file copyright.txt. Under the terms of this licence, all derivative works
10
+ # must themselves be licensed under the Open Software Licence v. 3.0
11
+ #
12
+ #
13
+
14
+
15
+ require 'optplus'
16
+ require 'optplus/nested'
17
+
18
+
19
+ class MyParser < Optplus::Parser
20
+
21
+ usage "[options] action"
22
+
23
+ description "A simple test",
24
+ "that tells you little"
25
+
26
+ def before_all
27
+ set_option :env, :dev
28
+ end
29
+
30
+
31
+ # define the options for this command
32
+ def options(opts)
33
+ opts.on('-a', '--all', 'show me everything') do
34
+ set_option :all
35
+ end
36
+ opts.on('-A', '--altogether', 'show me everything, really') do
37
+ set_option :altogether
38
+ end
39
+ opts.on('-e', '--environment TYPE', [:dev, :dec, :test, :prod], 'specify the environment') do |env|
40
+ set_option :env, env
41
+ end
42
+ opts.on('-s', '--sob [String]', String, 'sob when telling truth') do |cry|
43
+ set_option :sob, cry
44
+ end
45
+
46
+ debug_option(opts)
47
+ end
48
+
49
+ # define things that need to be done before any actions
50
+ def before_actions
51
+ @secrets = ['The answer to the universe is 42',
52
+ 'The mystery man is Blake',
53
+ 'The beginning of the Universe is the wrong question'
54
+ ]
55
+ end
56
+
57
+ def after_actions
58
+ puts "Tidying and Cleaning..."
59
+ sleep 1.0
60
+ puts "All done"
61
+ end
62
+
63
+ describe :show, "Show me what you know"
64
+ def show
65
+ unless get_option :all
66
+ puts "I know nothing"
67
+ else
68
+ puts "OK I will tell you everything"
69
+ puts get_option :sob if option? :sob
70
+ puts "You're in: #{get_option :env}"
71
+ @secrets.each do |secret|
72
+ puts secret
73
+ end
74
+ puts get_option :sob if option? :sob
75
+ end
76
+ end
77
+ help :show, "You can use this to find out what I know",
78
+ "If you persist with options I may tell you more"
79
+
80
+ describe :about, "Tell me about something"
81
+ def about
82
+ subject = all_arguments.join(' ')
83
+ if subject then
84
+ puts "I know nothing about #{subject}"
85
+ else
86
+ puts "Sorry, what do you want to know about?"
87
+ end
88
+ end
89
+
90
+ describe :sheep, "All about sheep"
91
+ def sheep
92
+ puts "Baaaa"
93
+ end
94
+
95
+ class Question < Optplus::NestedParser
96
+
97
+ usage "question aspect"
98
+
99
+ description "ask a question about a particular aspect"
100
+
101
+ describe :work, "Ask about work-related things"
102
+ def work
103
+ puts "I am unemployed"
104
+ end
105
+
106
+ describe :home, "Ask about home related things"
107
+ def home
108
+ topic = next_argument_or('Cooking')
109
+ puts "I don't do #{topic} at home"
110
+ end
111
+ help :home, "suggest something I might do at home, such as ",
112
+ "cleaning or painting"
113
+
114
+ describe :errors, "Ask me about errors"
115
+ def errors
116
+ if get_option :altogether
117
+ exit_on_error("Error: I know nothing about errors")
118
+ end
119
+ exit_on_error
120
+ end
121
+
122
+ end
123
+
124
+ nest_parser :question, Question, "Interrogate in more detail"
125
+
126
+ end
127
+
128
+
129
+
130
+ MyParser.run!
data/test/my_parser.rb ADDED
@@ -0,0 +1,90 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Author:: Robert Sharp
4
+ # Copyright:: Copyright (c) 2013 Robert Sharp
5
+ # License:: Open Software Licence v3.0
6
+ #
7
+ # This software is licensed for use under the Open Software Licence v. 3.0
8
+ # The terms of this licence can be found at http://www.opensource.org/licenses/osl-3.0.php
9
+ # and in the file copyright.txt. Under the terms of this licence, all derivative works
10
+ # must themselves be licensed under the Open Software Licence v. 3.0
11
+ #
12
+ #
13
+
14
+
15
+ require 'optplus'
16
+
17
+
18
+ class MyParser < Optplus::Parser
19
+
20
+ usage "[options] action"
21
+
22
+ description "A simple test",
23
+ "that tells you little"
24
+
25
+ def before_all
26
+ set_option :env, :dev
27
+ end
28
+
29
+
30
+ # define the options for this command
31
+ def options(opts)
32
+ opts.on('-a', '--all', 'show me everything') do
33
+ set_option :all
34
+ end
35
+ opts.on('-A', '--altogether', 'show me everything, really') do
36
+ set_option :altogether
37
+ end
38
+ opts.on('-e', '--environment TYPE', [:dev, :dec, :test, :prod], 'specify the environment') do |env|
39
+ set_option :env, env
40
+ end
41
+ opts.on('-s', '--sob [String]', String, 'sob when telling truth') do |cry|
42
+ set_option :sob, cry
43
+ end
44
+
45
+ debug_option(opts)
46
+ end
47
+
48
+ # define things that need to be done before any actions
49
+ def before_actions
50
+ @secrets = ['The answer to the universe is 42',
51
+ 'The mystery man is Blake',
52
+ 'The beginning of the Universe is the wrong question'
53
+ ]
54
+ end
55
+
56
+ describe :show, "Show me what you know"
57
+ def show
58
+ unless get_option :all
59
+ puts "I know nothing"
60
+ else
61
+ puts "OK I will tell you everything"
62
+ puts get_option :sob if option? :sob
63
+ puts "You're in: #{get_option :env}"
64
+ @secrets.each do |secret|
65
+ puts secret
66
+ end
67
+ puts get_option :sob if option? :sob
68
+ end
69
+ end
70
+ help :show, "You can use this to find out what I know",
71
+ "If you persist with options I may tell you more"
72
+
73
+ describe :about, "Tell me about something"
74
+ def about
75
+ subject = all_arguments.join(' ')
76
+ if subject then
77
+ puts "I know nothing about #{subject}"
78
+ else
79
+ puts "Sorry, what do you want to know about?"
80
+ end
81
+ end
82
+
83
+ describe :sheep, "All about sheep"
84
+ def sheep
85
+ puts "Baaaa"
86
+ end
87
+
88
+ end
89
+
90
+ MyParser.run!