console_runner 0.1.31 → 0.1.32
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +121 -6
- data/lib/command_line_parser.rb +3 -1
- data/lib/console_runner/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0ad8a81a76873b3609f96f246f2fa9921bdfabb0
|
4
|
+
data.tar.gz: 510c5ee696731b32c5443fb1b9934e0b80761f08
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
-
|
82
|
-
|
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
|
-
|
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
|
-
|
98
|
-
|
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
|
|
data/lib/command_line_parser.rb
CHANGED
@@ -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
|
-
|
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
|