console_runner 0.1.31 → 0.1.32

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: becac25f066c347b5251cae62e828e6d680a7ff7
4
- data.tar.gz: 869470a975fd0887238a96ba2910a65ee664531e
3
+ metadata.gz: 0ad8a81a76873b3609f96f246f2fa9921bdfabb0
4
+ data.tar.gz: 510c5ee696731b32c5443fb1b9934e0b80761f08
5
5
  SHA512:
6
- metadata.gz: 411ce109baeec14e087e73827b6170c49940972182f716e544696790e09b1b16eab530d62f189b31a20af5333aee1f897c4fd65747d9dca8bf227b71fbe51ba1
7
- data.tar.gz: a89ed3c0453241a195255df922bad9066ab4cf562fed8767ce946ac74fe58a94a0a600bc35319c18b399cb09edc1ca7efa83d5b702749ae39412858f5ca2aa4c
6
+ metadata.gz: 7f0775041c7e1d50ac39df2a821ee98a2b6c53fe11cdcbdc0a97afe6a44ce998784220622f6687be33ad4bb74611bd6805a66adaf83758690ba779bb1fba8371
7
+ data.tar.gz: 6c35467b3d493e4f5ec1fac5513952d2a214b21da25fd207425af852cfe60b0c845f6cdbda51849283234e0b8729f42006da70c8b30559729bbbdca26dcee1b1
data/README.md CHANGED
@@ -67,9 +67,10 @@ Or install it yourself as:
67
67
  Yes. Any text placed after `@runnable` tag will be displayed on the help page. You can add any additional information about how to use your tool there.
68
68
  > **Tip**: You can use multi-line text as well
69
69
 
70
- **Example:**
70
+ ##### Example:
71
71
  ```ruby
72
72
  # @runnable This tool can talk to you. Run it when you are lonely.
73
+ # Written in Ruby.
73
74
  class MyClass
74
75
 
75
76
  def initialize
@@ -78,8 +79,9 @@ class MyClass
78
79
  end
79
80
 
80
81
  # @runnable Say 'Hello' to you.
81
- def say_hello
82
- puts @hello_msg
82
+ # @param [String] name Your name
83
+ def say_hello(name)
84
+ puts @hello_msg + ', ' + name
83
85
  end
84
86
 
85
87
  # @runnable Say 'Good Bye' to you.
@@ -91,11 +93,124 @@ end
91
93
  ```
92
94
 
93
95
  ```bash
94
- TODO example
96
+ ~> c_run /projects/example/my_class.rb --help
97
+ Options:
98
+ --debug Run in debug mode.
99
+
100
+ This tool can talk to you. Run it when you are lonely.
101
+ Written in Ruby.
102
+
103
+ Available actions:
104
+ - say_hello
105
+ Say 'Hello' to you.
106
+ - say_bye
107
+ Say 'Good Bye' to you.
95
108
  ```
96
109
 
97
- ## ToDo
98
- - fix help menu for action: action help text should be displayed, list of available actions should be displayed
110
+ ```bash
111
+ ~> c_run /projects/example/my_class.rb say_hello -h
112
+ Options:
113
+ -d, --debug Run in debug mode.
114
+ -h, --help Show this message
115
+ --name=<s> (Ruby class: String) Your name
116
+
117
+ Say 'Hello' to you.
118
+
119
+ ```
120
+ #### **Can I send parameters to my methods?**
121
+ Yes, `console_runner` parses YARD annotation (`@param` and `@option` tags) and check the list of parameters for your method.
122
+
123
+ > *Restriction*: You can use Hash parameters as well (for storing options). But you cannot use the same name for parameter and for option.
124
+ >
125
+ > For example, `def limit(number, options = {number: 5})...` - `number` name is not allowed. You should use another parameter name.
126
+
127
+ ##### Example:
128
+ ```ruby
129
+ # @runnable This tool can talk to you. Run it when you are lonely.
130
+ # Written in Ruby.
131
+ class MyClass
132
+
133
+ def initialize
134
+ @hello_msg = 'Hello'
135
+ @bye_msg = 'Good Bye'
136
+ end
137
+
138
+ # @runnable Say 'Hello' to you.
139
+ # @param [String] name Your name
140
+ # @param [Hash] options options
141
+ # @option options [Boolean] :second_meet Have you met before?
142
+ # @option options [String] :prefix Your custom prefix
143
+ def say_hello(name, options = {})
144
+ second_meet = nil
145
+ second_meet = 'Nice to see you again!' if options['second_meet']
146
+ prefix = options['prefix']
147
+ message = @hello_msg + ', '
148
+ message += "#{prefix} " if prefix
149
+ message += "#{name}. "
150
+ message += second_meet if second_meet
151
+ puts message
152
+ end
153
+
154
+ end
155
+ ```
156
+ ```bash
157
+ ~> c_run /projects/example/my_class.rb say_hello -h
158
+ Options:
159
+ -d, --debug Run in debug mode.
160
+ -h, --help Show this message
161
+ --name=<s> (Ruby class: String) Your name
162
+ --second-meet (Ruby class: Boolean) Have you met before?
163
+ --prefix=<s> (Ruby class: String) Your custom prefix
164
+
165
+ Say 'Hello' to you.
166
+ ```
167
+ ```bash
168
+ ~> c_run /projects/example/my_class.rb say_hello -n John --second-meet --prefix Mr.
169
+ Hello, Mr. John. Nice to see you again!
170
+ ```
171
+
172
+ #### **Can I use optional parameters?**
173
+ Yes. All parameters with `@option` YARD tag are optional.
174
+
175
+ `--second-meet` and `--prefix` parameters are optional in the following example:
176
+ ```ruby
177
+ # @runnable Say 'Hello' to you.
178
+ # @param [String] name Your name
179
+ # @param [Hash] options options
180
+ # @option options [Boolean] :second_meet Have you met before?
181
+ # @option options [String] :prefix Your custom prefix
182
+ ```
183
+ Another approach is to use *default values* for parameters.
184
+ Parameter `--name` in the following example is optional because it has the default value `Chuck`.
185
+ ```ruby
186
+ # @runnable Say 'Hello' to you.
187
+ # @param [String] name Your name
188
+ # @param [Hash] options options
189
+ # @option options [Boolean] :second_meet Have you met before?
190
+ # @option options [String] :prefix Your custom prefix
191
+ def say_hello(name = 'Chuck', options = {})
192
+ second_meet = nil
193
+ second_meet = 'Nice to see you again!' if options['second_meet']
194
+ prefix = options['prefix']
195
+ message = @hello_msg + ', '
196
+ message += "#{prefix} " if prefix
197
+ message += "#{name}. "
198
+ message += second_meet if second_meet
199
+ puts message
200
+ end
201
+ ```
202
+ ```bash
203
+ -> c_run /projects/example/my_class.rb say_hello
204
+ Hello, Chuck.
205
+ ```
206
+ #### **Is it works only for class methods?**
207
+ `console_runner` works with both methods - class and instance methods. It's clear how it works with class method - method is called without any preconditions.
208
+ Class method will be called in accordance with following logic:
209
+ 1. call `:initialize` method
210
+ 2. cal action method
211
+
212
+ #### **My `require` code doesn't work well. How can I fix it?**
213
+ Use `require_relative ` method instead.
99
214
 
100
215
  ## Development
101
216
 
@@ -76,7 +76,9 @@ class CommandLineParser
76
76
  given_attrs = cmd_opts.keys.select { |k| k.to_s.include? '_given' }.map { |k| k.to_s.gsub('_given', '').to_sym }
77
77
  method.cmd_opts = cmd_opts.select { |k, _| given_attrs.include? k }
78
78
  method.default_values.each do |k, v|
79
- method.cmd_opts[k.to_sym] ||= v
79
+ param_name = k.to_sym
80
+ next if method.option_tags.map(&:name).include?(param_name.to_s)
81
+ method.cmd_opts[param_name] ||= v
80
82
  end
81
83
  method.required_parameters.each do |required_param|
82
84
  next if method.options_group? required_param
@@ -1,3 +1,3 @@
1
1
  module ConsoleRunner
2
- VERSION = '0.1.31'
2
+ VERSION = '0.1.32'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: console_runner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.31
4
+ version: 0.1.32
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yury Karpovich