optionscrapper 0.0.9 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 55227d37e4e22dc9ff1625a0cc6257ed76326b6a
4
- data.tar.gz: 714240abbc84f62c81e0f9e2178ba1b815709ed4
3
+ metadata.gz: f4fced71128425f8f42697fa6c0a1be092ec0665
4
+ data.tar.gz: 5dc0955472e719f439d1caf38337530744ae0cdf
5
5
  SHA512:
6
- metadata.gz: f22b3bdf77e3b01bda61d634d509aa0eec9a3e46bbe08c0010a00975293b4e47571f76e7ee84b762ec8f3a73cc6430db5d327e78cc50a73d0aaaab05e2e277a7
7
- data.tar.gz: 7e54275583b4aeba65daeeba88cec9fd342192151ba29c9243292710cf132cd50389d0fd17286ab12dedb2157257b60c0d2c8134fff35c6829b2b6518e31b3d4
6
+ metadata.gz: 072e59961dca7ed332c2477ef7b24ba6ed8ef0ed5e5f558f62334caf9bb4c0751c3060968b26cc9c63011e670b30801664774d782e850985e0c443f26b2d5569
7
+ data.tar.gz: 7e4d0597e829212d933d6f94b020290fc2431a129d41b5c3dadba8af145c68c13c9bf11e61e6a64d89a30cec13238adbfe993f25d327a29fbb83752f5aeea898
data/LICENSE ADDED
@@ -0,0 +1,339 @@
1
+ GNU GENERAL PUBLIC LICENSE
2
+ Version 2, June 1991
3
+
4
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc., <http://fsf.org/>
5
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6
+ Everyone is permitted to copy and distribute verbatim copies
7
+ of this license document, but changing it is not allowed.
8
+
9
+ Preamble
10
+
11
+ The licenses for most software are designed to take away your
12
+ freedom to share and change it. By contrast, the GNU General Public
13
+ License is intended to guarantee your freedom to share and change free
14
+ software--to make sure the software is free for all its users. This
15
+ General Public License applies to most of the Free Software
16
+ Foundation's software and to any other program whose authors commit to
17
+ using it. (Some other Free Software Foundation software is covered by
18
+ the GNU Lesser General Public License instead.) You can apply it to
19
+ your programs, too.
20
+
21
+ When we speak of free software, we are referring to freedom, not
22
+ price. Our General Public Licenses are designed to make sure that you
23
+ have the freedom to distribute copies of free software (and charge for
24
+ this service if you wish), that you receive source code or can get it
25
+ if you want it, that you can change the software or use pieces of it
26
+ in new free programs; and that you know you can do these things.
27
+
28
+ To protect your rights, we need to make restrictions that forbid
29
+ anyone to deny you these rights or to ask you to surrender the rights.
30
+ These restrictions translate to certain responsibilities for you if you
31
+ distribute copies of the software, or if you modify it.
32
+
33
+ For example, if you distribute copies of such a program, whether
34
+ gratis or for a fee, you must give the recipients all the rights that
35
+ you have. You must make sure that they, too, receive or can get the
36
+ source code. And you must show them these terms so they know their
37
+ rights.
38
+
39
+ We protect your rights with two steps: (1) copyright the software, and
40
+ (2) offer you this license which gives you legal permission to copy,
41
+ distribute and/or modify the software.
42
+
43
+ Also, for each author's protection and ours, we want to make certain
44
+ that everyone understands that there is no warranty for this free
45
+ software. If the software is modified by someone else and passed on, we
46
+ want its recipients to know that what they have is not the original, so
47
+ that any problems introduced by others will not reflect on the original
48
+ authors' reputations.
49
+
50
+ Finally, any free program is threatened constantly by software
51
+ patents. We wish to avoid the danger that redistributors of a free
52
+ program will individually obtain patent licenses, in effect making the
53
+ program proprietary. To prevent this, we have made it clear that any
54
+ patent must be licensed for everyone's free use or not licensed at all.
55
+
56
+ The precise terms and conditions for copying, distribution and
57
+ modification follow.
58
+
59
+ GNU GENERAL PUBLIC LICENSE
60
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61
+
62
+ 0. This License applies to any program or other work which contains
63
+ a notice placed by the copyright holder saying it may be distributed
64
+ under the terms of this General Public License. The "Program", below,
65
+ refers to any such program or work, and a "work based on the Program"
66
+ means either the Program or any derivative work under copyright law:
67
+ that is to say, a work containing the Program or a portion of it,
68
+ either verbatim or with modifications and/or translated into another
69
+ language. (Hereinafter, translation is included without limitation in
70
+ the term "modification".) Each licensee is addressed as "you".
71
+
72
+ Activities other than copying, distribution and modification are not
73
+ covered by this License; they are outside its scope. The act of
74
+ running the Program is not restricted, and the output from the Program
75
+ is covered only if its contents constitute a work based on the
76
+ Program (independent of having been made by running the Program).
77
+ Whether that is true depends on what the Program does.
78
+
79
+ 1. You may copy and distribute verbatim copies of the Program's
80
+ source code as you receive it, in any medium, provided that you
81
+ conspicuously and appropriately publish on each copy an appropriate
82
+ copyright notice and disclaimer of warranty; keep intact all the
83
+ notices that refer to this License and to the absence of any warranty;
84
+ and give any other recipients of the Program a copy of this License
85
+ along with the Program.
86
+
87
+ You may charge a fee for the physical act of transferring a copy, and
88
+ you may at your option offer warranty protection in exchange for a fee.
89
+
90
+ 2. You may modify your copy or copies of the Program or any portion
91
+ of it, thus forming a work based on the Program, and copy and
92
+ distribute such modifications or work under the terms of Section 1
93
+ above, provided that you also meet all of these conditions:
94
+
95
+ a) You must cause the modified files to carry prominent notices
96
+ stating that you changed the files and the date of any change.
97
+
98
+ b) You must cause any work that you distribute or publish, that in
99
+ whole or in part contains or is derived from the Program or any
100
+ part thereof, to be licensed as a whole at no charge to all third
101
+ parties under the terms of this License.
102
+
103
+ c) If the modified program normally reads commands interactively
104
+ when run, you must cause it, when started running for such
105
+ interactive use in the most ordinary way, to print or display an
106
+ announcement including an appropriate copyright notice and a
107
+ notice that there is no warranty (or else, saying that you provide
108
+ a warranty) and that users may redistribute the program under
109
+ these conditions, and telling the user how to view a copy of this
110
+ License. (Exception: if the Program itself is interactive but
111
+ does not normally print such an announcement, your work based on
112
+ the Program is not required to print an announcement.)
113
+
114
+ These requirements apply to the modified work as a whole. If
115
+ identifiable sections of that work are not derived from the Program,
116
+ and can be reasonably considered independent and separate works in
117
+ themselves, then this License, and its terms, do not apply to those
118
+ sections when you distribute them as separate works. But when you
119
+ distribute the same sections as part of a whole which is a work based
120
+ on the Program, the distribution of the whole must be on the terms of
121
+ this License, whose permissions for other licensees extend to the
122
+ entire whole, and thus to each and every part regardless of who wrote it.
123
+
124
+ Thus, it is not the intent of this section to claim rights or contest
125
+ your rights to work written entirely by you; rather, the intent is to
126
+ exercise the right to control the distribution of derivative or
127
+ collective works based on the Program.
128
+
129
+ In addition, mere aggregation of another work not based on the Program
130
+ with the Program (or with a work based on the Program) on a volume of
131
+ a storage or distribution medium does not bring the other work under
132
+ the scope of this License.
133
+
134
+ 3. You may copy and distribute the Program (or a work based on it,
135
+ under Section 2) in object code or executable form under the terms of
136
+ Sections 1 and 2 above provided that you also do one of the following:
137
+
138
+ a) Accompany it with the complete corresponding machine-readable
139
+ source code, which must be distributed under the terms of Sections
140
+ 1 and 2 above on a medium customarily used for software interchange; or,
141
+
142
+ b) Accompany it with a written offer, valid for at least three
143
+ years, to give any third party, for a charge no more than your
144
+ cost of physically performing source distribution, a complete
145
+ machine-readable copy of the corresponding source code, to be
146
+ distributed under the terms of Sections 1 and 2 above on a medium
147
+ customarily used for software interchange; or,
148
+
149
+ c) Accompany it with the information you received as to the offer
150
+ to distribute corresponding source code. (This alternative is
151
+ allowed only for noncommercial distribution and only if you
152
+ received the program in object code or executable form with such
153
+ an offer, in accord with Subsection b above.)
154
+
155
+ The source code for a work means the preferred form of the work for
156
+ making modifications to it. For an executable work, complete source
157
+ code means all the source code for all modules it contains, plus any
158
+ associated interface definition files, plus the scripts used to
159
+ control compilation and installation of the executable. However, as a
160
+ special exception, the source code distributed need not include
161
+ anything that is normally distributed (in either source or binary
162
+ form) with the major components (compiler, kernel, and so on) of the
163
+ operating system on which the executable runs, unless that component
164
+ itself accompanies the executable.
165
+
166
+ If distribution of executable or object code is made by offering
167
+ access to copy from a designated place, then offering equivalent
168
+ access to copy the source code from the same place counts as
169
+ distribution of the source code, even though third parties are not
170
+ compelled to copy the source along with the object code.
171
+
172
+ 4. You may not copy, modify, sublicense, or distribute the Program
173
+ except as expressly provided under this License. Any attempt
174
+ otherwise to copy, modify, sublicense or distribute the Program is
175
+ void, and will automatically terminate your rights under this License.
176
+ However, parties who have received copies, or rights, from you under
177
+ this License will not have their licenses terminated so long as such
178
+ parties remain in full compliance.
179
+
180
+ 5. You are not required to accept this License, since you have not
181
+ signed it. However, nothing else grants you permission to modify or
182
+ distribute the Program or its derivative works. These actions are
183
+ prohibited by law if you do not accept this License. Therefore, by
184
+ modifying or distributing the Program (or any work based on the
185
+ Program), you indicate your acceptance of this License to do so, and
186
+ all its terms and conditions for copying, distributing or modifying
187
+ the Program or works based on it.
188
+
189
+ 6. Each time you redistribute the Program (or any work based on the
190
+ Program), the recipient automatically receives a license from the
191
+ original licensor to copy, distribute or modify the Program subject to
192
+ these terms and conditions. You may not impose any further
193
+ restrictions on the recipients' exercise of the rights granted herein.
194
+ You are not responsible for enforcing compliance by third parties to
195
+ this License.
196
+
197
+ 7. If, as a consequence of a court judgment or allegation of patent
198
+ infringement or for any other reason (not limited to patent issues),
199
+ conditions are imposed on you (whether by court order, agreement or
200
+ otherwise) that contradict the conditions of this License, they do not
201
+ excuse you from the conditions of this License. If you cannot
202
+ distribute so as to satisfy simultaneously your obligations under this
203
+ License and any other pertinent obligations, then as a consequence you
204
+ may not distribute the Program at all. For example, if a patent
205
+ license would not permit royalty-free redistribution of the Program by
206
+ all those who receive copies directly or indirectly through you, then
207
+ the only way you could satisfy both it and this License would be to
208
+ refrain entirely from distribution of the Program.
209
+
210
+ If any portion of this section is held invalid or unenforceable under
211
+ any particular circumstance, the balance of the section is intended to
212
+ apply and the section as a whole is intended to apply in other
213
+ circumstances.
214
+
215
+ It is not the purpose of this section to induce you to infringe any
216
+ patents or other property right claims or to contest validity of any
217
+ such claims; this section has the sole purpose of protecting the
218
+ integrity of the free software distribution system, which is
219
+ implemented by public license practices. Many people have made
220
+ generous contributions to the wide range of software distributed
221
+ through that system in reliance on consistent application of that
222
+ system; it is up to the author/donor to decide if he or she is willing
223
+ to distribute software through any other system and a licensee cannot
224
+ impose that choice.
225
+
226
+ This section is intended to make thoroughly clear what is believed to
227
+ be a consequence of the rest of this License.
228
+
229
+ 8. If the distribution and/or use of the Program is restricted in
230
+ certain countries either by patents or by copyrighted interfaces, the
231
+ original copyright holder who places the Program under this License
232
+ may add an explicit geographical distribution limitation excluding
233
+ those countries, so that distribution is permitted only in or among
234
+ countries not thus excluded. In such case, this License incorporates
235
+ the limitation as if written in the body of this License.
236
+
237
+ 9. The Free Software Foundation may publish revised and/or new versions
238
+ of the General Public License from time to time. Such new versions will
239
+ be similar in spirit to the present version, but may differ in detail to
240
+ address new problems or concerns.
241
+
242
+ Each version is given a distinguishing version number. If the Program
243
+ specifies a version number of this License which applies to it and "any
244
+ later version", you have the option of following the terms and conditions
245
+ either of that version or of any later version published by the Free
246
+ Software Foundation. If the Program does not specify a version number of
247
+ this License, you may choose any version ever published by the Free Software
248
+ Foundation.
249
+
250
+ 10. If you wish to incorporate parts of the Program into other free
251
+ programs whose distribution conditions are different, write to the author
252
+ to ask for permission. For software which is copyrighted by the Free
253
+ Software Foundation, write to the Free Software Foundation; we sometimes
254
+ make exceptions for this. Our decision will be guided by the two goals
255
+ of preserving the free status of all derivatives of our free software and
256
+ of promoting the sharing and reuse of software generally.
257
+
258
+ NO WARRANTY
259
+
260
+ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261
+ FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262
+ OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263
+ PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264
+ OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265
+ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266
+ TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267
+ PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268
+ REPAIR OR CORRECTION.
269
+
270
+ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271
+ WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272
+ REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273
+ INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274
+ OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275
+ TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276
+ YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277
+ PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278
+ POSSIBILITY OF SUCH DAMAGES.
279
+
280
+ END OF TERMS AND CONDITIONS
281
+
282
+ How to Apply These Terms to Your New Programs
283
+
284
+ If you develop a new program, and you want it to be of the greatest
285
+ possible use to the public, the best way to achieve this is to make it
286
+ free software which everyone can redistribute and change under these terms.
287
+
288
+ To do so, attach the following notices to the program. It is safest
289
+ to attach them to the start of each source file to most effectively
290
+ convey the exclusion of warranty; and each file should have at least
291
+ the "copyright" line and a pointer to where the full notice is found.
292
+
293
+ {description}
294
+ Copyright (C) {year} {fullname}
295
+
296
+ This program is free software; you can redistribute it and/or modify
297
+ it under the terms of the GNU General Public License as published by
298
+ the Free Software Foundation; either version 2 of the License, or
299
+ (at your option) any later version.
300
+
301
+ This program is distributed in the hope that it will be useful,
302
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
303
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304
+ GNU General Public License for more details.
305
+
306
+ You should have received a copy of the GNU General Public License along
307
+ with this program; if not, write to the Free Software Foundation, Inc.,
308
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
309
+
310
+ Also add information on how to contact you by electronic and paper mail.
311
+
312
+ If the program is interactive, make it output a short notice like this
313
+ when it starts in an interactive mode:
314
+
315
+ Gnomovision version 69, Copyright (C) year name of author
316
+ Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317
+ This is free software, and you are welcome to redistribute it
318
+ under certain conditions; type `show c' for details.
319
+
320
+ The hypothetical commands `show w' and `show c' should show the appropriate
321
+ parts of the General Public License. Of course, the commands you use may
322
+ be called something other than `show w' and `show c'; they could even be
323
+ mouse-clicks or menu items--whatever suits your program.
324
+
325
+ You should also get your employer (if you work as a programmer) or your
326
+ school, if any, to sign a "copyright disclaimer" for the program, if
327
+ necessary. Here is a sample; alter the names:
328
+
329
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330
+ `Gnomovision' (which makes passes at compilers) written by James Hacker.
331
+
332
+ {signature of Ty Coon}, 1 April 1989
333
+ Ty Coon, President of Vice
334
+
335
+ This General Public License does not permit incorporating your program into
336
+ proprietary programs. If your program is a subroutine library, you may
337
+ consider it more useful to permit linking proprietary applications with the
338
+ library. If this is what you want to do, use the GNU Lesser General
339
+ Public License instead of this License.
data/README.md CHANGED
@@ -1,11 +1,11 @@
1
- Options Scrapper!
1
+ OptionScrapper!
2
2
  ----------------
3
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
-
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;
5
+
6
6
  require 'optionscrapper'
7
7
  require 'pp'
8
-
8
+
9
9
  @options = {
10
10
  :config => '../config/openstack.yaml',
11
11
  :flavor => '2cores-4096mem-10gb',
@@ -17,47 +17,116 @@ Is a wrapper for the OptionsParser (optparse) gem which makes using subcommand l
17
17
  :verbose => true,
18
18
  :force => false
19
19
  }
20
-
20
+
21
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 }
22
+
23
+ def parser
24
+ @parser ||= OptionScrapper::new do |o|
25
+ o.on( '-S stack', '--stack NAME', 'the name of the openstack you wish to connect' ) { |x| @options[:stack] = x }
26
+ o.on( '-c CONFIG', '--config CONFIG', 'the configuration file to read credentials' ) { |x| @options[:config] = x }
27
+ o.on( nil, '--dry-run', 'perform a dry run' ) { @options[:dry_run] = true }
28
+ o.on( '-v', '--verbose', 'switch on verbose mode' ) { @options[:verbose] = true }
29
+ o.command :launch, 'launch a instance in to openstack cluster' do
30
+ o.command_alias :ln
31
+ o.on( '-H HOSTNAME', '--hostname HOSTNAME', 'the hostname of instance you are creating' ) { |x| @options[:hostname] = x }
32
+ o.on( '-i IMAGE', '--image IMAGE', 'the image you wish to boot from' ) { |x| @options[:image] = x }
33
+ o.on( '-f FLAVOR', '--flavor FLAVOR', 'the flavor the instance should work from' ) { |x| @options[:flavor] = x }
34
+ o.on( '-k KEYPAIR', '--keypair KEYPAIR', 'the keypair the instance should use' ) { |x| @options[:keypair] = x }
35
+ o.on( '-n NETWORK', '--network NETWORK', 'the network the instance should be connected' ) { |x| @options[:networks] << x }
36
+ o.on( '-s SECURITY', '--secgroups SECURITY', 'the security group assigned to the instance' ) { |x| @options[:security_group] << x }
37
+ o.on( '-u USER_DATA', '--user-data USER_DATA', 'the user data template' ) { |x| @options[:user_data] = x }
38
+ o.on( nil, '--hypervisor HOST', 'the compute node you want the instance to run' ) { |x| @options[:availability_zone] = x }
39
+ o.on( '-e', '--error', 'cause an error' ) { o.usage }
37
40
  o.on_command { @options[:action] = :launch }
38
41
  end
39
- o.command :destroy, 'destroy and delete an instance in openstack' do
42
+ o.command :destroy, 'destroy and delete an instance in openstack' do
43
+ o.command_alias :des
40
44
  o.on( '-H HOSTNAME', '--hostname HOSTNAME', 'the hostname of instance you are creating' ) { |x| @options[:hostname] = x }
41
45
  o.on_command { @options[:action] = :destroy }
42
46
  end
43
- o.command :snapshot, 'snapshot a instance within openstack' do
47
+ o.command :snapshot, 'snapshot a instance within openstack' do
48
+ o.command_alias :sp
44
49
  o.on( '-H HOSTNAME', '--hostname HOSTNAME', 'the hostname of the instance being snapshot' ) { |x| @options[:hostname] = x }
45
50
  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 }
51
+ o.on( nil, '--wait', 'wait on the snapshot to complete' ) { |x| @options[:wait] = true }
47
52
  o.on( '-f', '--force', 'if the snapshot image exists, delete it' ) { |x| @options[:force] = true }
48
53
  o.on_command { @options[:action] = :snapshot }
49
- end
54
+ end
50
55
  end
51
- Parser.parse!
52
- PP.pp @options
53
- rescue SystemExit => e
54
- exit e.status
55
56
  end
56
57
 
57
- Global Options
58
+ Will produce the following usage menu. Note, the -h|--help is injected in the default parser by default
59
+
60
+ [jest@starfury tests]$ ./test.rb --help
61
+
62
+ Usage: test.rb [options]
63
+ -h, --help display this usage menu
64
+ -S, --stack NAME the name of the openstack you wish to connect
65
+ -c, --config CONFIG the configuration file to read credentials
66
+ --dry-run perform a dry run
67
+ -v, --verbose switch on verbose mode
68
+
69
+ commands : -------------------------------------------------------------
70
+ launch (ln) launch a instance in to openstack cluster
71
+ destroy (des) destroy and delete an instance in openstack
72
+ snapshot (sp) snapshot a instance within openstack
73
+ ------------------------------------------------------------------------
74
+
75
+ launch : description: launch a instance in to openstack cluster
76
+ ------------------------------------------------------------------------
77
+
78
+ -H, --hostname HOSTNAME the hostname of instance you are creating
79
+ -i, --image IMAGE the image you wish to boot from
80
+ -f, --flavor FLAVOR the flavor the instance should work from
81
+ -k, --keypair KEYPAIR the keypair the instance should use
82
+ -n, --network NETWORK the network the instance should be connected
83
+ -s, --secgroups SECURITY the security group assigned to the instance
84
+ -u, --user-data USER_DATA the user data template
85
+ --hypervisor HOST the compute node you want the instance to run
86
+ -e, --error cause an error
87
+
88
+ destroy : description: destroy and delete an instance in openstack
89
+ ------------------------------------------------------------------------
90
+
91
+ -H, --hostname HOSTNAME the hostname of instance you are creating
92
+
93
+ snapshot : description: snapshot a instance within openstack
94
+ ------------------------------------------------------------------------
95
+
96
+ -H, --hostname HOSTNAME the hostname of the instance being snapshot
97
+ -s, --snapshot NAME the name of the snapshot you are creating
98
+ --wait wait on the snapshot to complete
99
+ -f, --force if the snapshot image exists, delete it
100
+
101
+ Or to print just the menu of the subcommand
102
+
103
+ [jest@starfury tests]$ ./test.rb ln --help
104
+ launch : description: launch a instance in to openstack cluster
105
+ ------------------------------------------------------------------------
106
+
107
+ -H, --hostname HOSTNAME the hostname of instance you are creating
108
+ -i, --image IMAGE the image you wish to boot from
109
+ -f, --flavor FLAVOR the flavor the instance should work from
110
+ -k, --keypair KEYPAIR the keypair the instance should use
111
+ -n, --network NETWORK the network the instance should be connected
112
+ -s, --secgroups SECURITY the security group assigned to the instance
113
+ -u, --user-data USER_DATA the user data template
114
+ --hypervisor HOST the compute node you want the instance to run
115
+ -e, --error cause an error
116
+
117
+ ----------
118
+
119
+ Mixing Options
58
120
  --------------
59
121
 
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
122
+ The order of the options is not enforced, thus global options can be placed anywhere on the command line. If however, there is a conflict, i.e. the subcommand has the same flag/s as a global option, the subcommand takes precedence.
61
123
 
62
- ./test launch -H hostname ; -H exists in global and launch, but launch take priority and it's his to process
124
+ Contributing
125
+ ------------
63
126
 
127
+ - Fork it
128
+ - Create your feature branch (git checkout -b my-new-feature)
129
+ - Commit your changes (git commit -am 'Add some feature')
130
+ - Push to the branch (git push origin my-new-feature)
131
+ - Create new Pull Request
132
+ - If applicable, update the README.md
@@ -4,29 +4,22 @@
4
4
  #
5
5
  # vim:ts=2:sw=2:et
6
6
  #
7
- $:.unshift File.join(File.dirname(__FILE__),'.','./')
7
+ require 'rubygems' if RUBY_VERSION < '1.9.0'
8
8
  require 'optparse'
9
- require 'optionscrapper/ext/optparse'
10
- require 'optionscrapper/version'
11
- require 'optionscrapper/optparser'
12
9
 
13
10
  module OptionScrapper
14
11
  ROOT = File.expand_path File.dirname __FILE__
15
12
 
16
- require "#{ROOT}/optionscrapper/version"
17
-
18
- autoload :Version, "#{ROOT}/optionscrapper/version"
19
- autoload :Parser, "#{ROOT}/optionscrapper/optparser"
20
-
21
- @version = OptionScrapper::VERSION
13
+ autoload :Version, "#{ROOT}/optionscrapper/version"
14
+ autoload :OptionsParser, "#{ROOT}/optionscrapper/optionsparser"
22
15
 
23
16
  def self.version
24
17
  OptionScrapper::VERSION
25
18
  end
26
19
 
27
- def self.new &block
28
- OptionScrapper::OptParser::new do |o|
29
- yield o if block_given?
20
+ def self.new
21
+ OptionScrapper::OptionsParser::new do |x|
22
+ yield x if block_given?
30
23
  end
31
24
  end
32
25
  end
@@ -0,0 +1,35 @@
1
+ #
2
+ # Author: Rohith
3
+ # Date: 2014-11-10 21:50:43 +0000 (Mon, 10 Nov 2014)
4
+ #
5
+ # vim:ts=2:sw=2:et
6
+ #
7
+ module OptionScrapper
8
+ class Batch
9
+ attr_accessor :cursor, :previous
10
+
11
+ def initialize
12
+ @cursor = OptionScrapper::OptionsParser::GLOBAL_PARSER
13
+ @batches = { OptionScrapper::OptionsParser::GLOBAL_PARSER => [] }
14
+ @previous = nil
15
+ yield self if block_given?
16
+ end
17
+
18
+ def batches
19
+ raise StandardError, 'batches: you have not supplied a block to call' unless block_given?
20
+ @batches.each_pair do |name,arguments|
21
+ yield name,arguments
22
+ end
23
+ end
24
+
25
+ def add(argument)
26
+ @previous = nil
27
+ @batches[@cursor] ||= []
28
+ @batches[@cursor] << argument
29
+ end
30
+
31
+ def global(argument)
32
+ @batches[OptionScrapper::OptionsParser::GLOBAL_PARSER] << argument
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,26 @@
1
+ #
2
+ # Author: Rohith (gambol99@gmail.com)
3
+ # Date: 2014-11-11 11:58:05 +0000 (Tue, 11 Nov 2014)
4
+ #
5
+ # vim:ts=2:sw=2:et
6
+ #
7
+ module OptionScrapper
8
+ module Utils
9
+ alias_method :newline, :puts
10
+
11
+ def offset(length = 4, spacer = "")
12
+ length.times.each { spacer << " " }
13
+ spacer
14
+ end
15
+
16
+ def fail(message)
17
+ puts "[error]: " << message
18
+ exit 1
19
+ end
20
+
21
+ def horizontal_line(length, symbol = '-', line = "")
22
+ length.times.each { line << "#{symbol}" }
23
+ line
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,200 @@
1
+ #
2
+ # Author: Rohith Jayawardene
3
+ # Email: (gambol99@gmail.com)
4
+ # Date: 2014-05-22 23:55:29 +0100 (Thu, 22 May 2014)
5
+ #
6
+ # vim:ts=2:sw=2:et
7
+ #
8
+ $:.unshift File.join(File.dirname(__FILE__),'./')
9
+ require 'optionscrapper/ext/optparse'
10
+ require 'optionscrapper/misc/utils'
11
+ require 'optionscrapper/parser'
12
+ require 'optionscrapper/batch'
13
+ require 'forwardable'
14
+
15
+ module OptionScrapper
16
+ class OptionsParser
17
+ extend Forwardable
18
+ include OptionScrapper::Utils
19
+ GLOBAL_PARSER = :global
20
+ GLOBAL_OPTION_REGEX = /^(-[-]?[[:alpha:]-]+)/
21
+
22
+ attr_accessor :parsers
23
+ def_delegators :@cursor, :command_alias, :alias, :on
24
+
25
+ def initialize
26
+ @cursor = nil
27
+ @parsers = {}
28
+ # step: create the global parser
29
+ create_parser(GLOBAL_PARSER,'the global parser')
30
+ @cursor.parser.program_name = prog_name
31
+ # step: inject a default help options for global
32
+ @cursor.parser.on_tail( '-h', '--help', 'display this usage menu' ) do
33
+ puts print_usage
34
+ exit 0
35
+ end
36
+ yield self if block_given?
37
+ end
38
+
39
+ def command(name, description)
40
+ parser = create_parser(name,description)
41
+ yield parser if block_given?
42
+ parser
43
+ end
44
+
45
+ def banner=(value)
46
+ @cursor.parser.banner = offset << value
47
+ end
48
+
49
+ def parse!(arguments = ARGV)
50
+ # step: we need to separate into subcommand arguments
51
+ batch_arguments(arguments) do |batch|
52
+ # step: iterate the batches and fire off the parsers for each subcommand
53
+ batch.batches do |name,options|
54
+ parsers[name].parse! options
55
+ end
56
+ end
57
+ end
58
+
59
+ def on_command &block
60
+ @cursor.on_command = block
61
+ end
62
+
63
+ def usage(message = nil, name = GLOBAL_PARSER)
64
+ # step: if the parser is specified, print only that one
65
+ unless name == GLOBAL_PARSER
66
+ puts parsers[parser_name].print_usage
67
+ else
68
+ # step: else we generate the full parse usage menu
69
+ newline
70
+ puts global_parser.parser
71
+ newline
72
+ # step: we don't need to do this if there are no sub commands
73
+ if parsers.size > 1
74
+ puts offset << "commands : #{horizontal_line(62,'-')}"
75
+ parsers.values.each do |parser|
76
+ next if parser.name == GLOBAL_PARSER
77
+ command_line = parser.name.to_s
78
+ command_line << '(%s)' % [ parser.aliases.join(',') ] unless parser.aliases.empty?
79
+ puts offset << '%-32s %s' % [ command_line, parser.description ]
80
+ end
81
+ puts offset << horizontal_line(72,'-')
82
+ newline
83
+ parsers.values.each do |parser|
84
+ # step: skip the global, we have already displayed it
85
+ next if parser.name == GLOBAL_PARSER
86
+ # step: we don't need to show this if the subcommand has no options / switches
87
+ next if parser.switches.empty?
88
+ # step: else we can show the parser usage
89
+ puts parser.parser
90
+ newline
91
+ end
92
+ end
93
+ end
94
+ fail message if message
95
+ newline
96
+ exit 0
97
+ end
98
+ alias_method :print_usage, :usage
99
+
100
+ def method_missing(method, *args, &block)
101
+ if @cursor.parser.respond_to? method
102
+ @cursor.parser.send method, args, &block if args and !args.empty?
103
+ @cursor.parser.send method, &block if !args or args.empty?
104
+ else
105
+ super(method, args, block)
106
+ end
107
+ end
108
+
109
+ private
110
+ def create_parser(name,description)
111
+ # step: create a new parser and add to hash collection
112
+ parsers[name] = Parser.new(name,description)
113
+ # step: update the cursor to the newly created parser
114
+ @cursor = parsers[name]
115
+ # step: create a usage for this command
116
+ @cursor.parser.banner = " #{name} : description: #{description}"
117
+ @cursor.parser.separator " #{horizontal_line(72)}"
118
+ @cursor.parser.separator ''
119
+ # step: return the parser
120
+ parsers[name]
121
+ end
122
+
123
+ # batch: takes the command line options, iterates the options and places them into
124
+ # the correct batch
125
+ def batch_arguments(arguments)
126
+ batch = Batch.new do |x|
127
+ arguments.each do |argument|
128
+ # step: is the argument a subcommand?
129
+ if subcommand?(argument)
130
+ parser = subcommand(argument)
131
+ # step: get the command and set the cursor to the parser
132
+ x.cursor = parser.name
133
+ # step: call the on_command block
134
+ parser.on_command.call
135
+ else
136
+ # step: is this argument a parser argument
137
+ parser = subcommand(x.cursor)
138
+ if !is_switch?(argument)
139
+ if x.previous
140
+ x.global(argument)
141
+ else
142
+ x.add argument
143
+ end
144
+ elsif !parser.switch?(argument) and global_parser.switch?(argument)
145
+ x.global(argument)
146
+ x.previous = x.cursor
147
+ else
148
+ x.add argument
149
+ end
150
+ end
151
+ end
152
+ end
153
+ yield batch if block_given?
154
+ batch
155
+ end
156
+
157
+ def subcommand?(name)
158
+ name = symbolize!(name)
159
+ return ( parsers.has_key?(name) or alias?(name) ) ? true : false
160
+ end
161
+
162
+ def subcommand(name)
163
+ name = symbolize!(name)
164
+ raise StandardError, "subcommand: parser: #{name} does not exists" unless subcommand? name
165
+ unless parsers[name]
166
+ # step: it must be an alias
167
+ name = parsers.values.select { |parser|
168
+ parser.aliases.include? name
169
+ }.first.name
170
+ end
171
+ parsers[name]
172
+ end
173
+
174
+ def alias?(name)
175
+ name = symbolize!(name)
176
+ parsers.values.each do |parser|
177
+ next if parser.name == GLOBAL_PARSER
178
+ next if parser.aliases.empty?
179
+ return true if parser.aliases.include? name
180
+ end
181
+ false
182
+ end
183
+
184
+ def is_switch?(argument)
185
+ argument =~ GLOBAL_OPTION_REGEX
186
+ end
187
+
188
+ def global_parser
189
+ subcommand(GLOBAL_PARSER)
190
+ end
191
+
192
+ def prog_name
193
+ File.basename($0)
194
+ end
195
+
196
+ def symbolize!(name)
197
+ ( name.is_a? Symbol ) ? name : name.to_sym
198
+ end
199
+ end
200
+ end
@@ -0,0 +1,81 @@
1
+ #
2
+ # Author: Rohith (gambol99@gmail.com)
3
+ # Date: 2014-11-10 17:30:21 +0000 (Mon, 10 Nov 2014)
4
+ #
5
+ # vim:ts=2:sw=2:et
6
+ #
7
+ module OptionScrapper
8
+ class Parser
9
+ include OptionScrapper::Utils
10
+ attr_reader :name,:description,:parser,:switches,:aliases
11
+ attr_accessor :on_command
12
+
13
+ def initialize(name,description)
14
+ @name = name.to_sym
15
+ @description = description
16
+ @switches = {}
17
+ @aliases = []
18
+ @parser = ::OptionParser.new
19
+ @on_command = Proc::new {}
20
+ end
21
+
22
+ # alias: a subcommand alias
23
+ def alias(name); aliases << name; end
24
+
25
+ #
26
+ # on: is the standard means of adding a command line option to the parser. The method
27
+ # excepts OptParser format extracts some information for meta data reasons and passes the
28
+ # call down the OptParser lib
29
+ #
30
+ def on(*args)
31
+ # step: we use this entry point to build of a list of switches
32
+ parse_option_switches(*args) do |option_name|
33
+ switches[option_name] = true
34
+ end
35
+ # step: pass the request to the underlining gem
36
+ parser.on(*args) do |x|
37
+ yield x if block_given?
38
+ end
39
+ end
40
+
41
+ def parse!(arguments)
42
+ parser.parse!(arguments)
43
+ end
44
+
45
+ def switch? argument;
46
+ switches.has_key? argument
47
+ end
48
+
49
+ def alias? argument
50
+ aliases.include? argument
51
+ end
52
+
53
+ # --- Method Aliases ---
54
+ alias_method :command_alias, :alias
55
+
56
+ private
57
+ def method_missing(method, *args, &block)
58
+ if parser.respond_to? method
59
+ case method
60
+ when :banner=
61
+ parser.send method, args.first, &block
62
+ else
63
+ parser.send method, args, &block if args and !args.empty?
64
+ parser.send method, &block if !args or args.empty?
65
+ end
66
+ else
67
+ super(method, args, block)
68
+ end
69
+ end
70
+
71
+ def parse_option_switches(*args)
72
+ if args and args.size >= 2
73
+ args[0..1].each do |a|
74
+ if a =~ OptionScrapper::OptionsParser::GLOBAL_OPTION_REGEX
75
+ yield $1
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -5,5 +5,5 @@
5
5
  # vim:ts=2:sw=2:et
6
6
  #
7
7
  module OptionScrapper
8
- VERSION = "0.0.9"
8
+ VERSION = '1.0.0'
9
9
  end
@@ -9,17 +9,17 @@ $:.unshift File.join(File.dirname(__FILE__),'.','lib/optionscrapper' )
9
9
  require 'version'
10
10
 
11
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) }
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 = 'GPL'
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
25
  end
@@ -12,6 +12,7 @@ require 'pp'
12
12
 
13
13
  def parser
14
14
  @parser ||= OptionScrapper::new do |o|
15
+ o.banner = "Usage: #{File.basename($0)} [subcommand]"
15
16
  o.on( '-S stack', '--stack NAME', 'the name of the openstack you wish to connect' ) { |x| @options[:stack] = x }
16
17
  o.on( '-c CONFIG', '--config CONFIG', 'the configuration file to read credentials' ) { |x| @options[:config] = x }
17
18
  o.on( nil, '--dry-run', 'perform a dry run' ) { @options[:dry_run] = true }
@@ -23,7 +24,7 @@ def parser
23
24
  o.on( '-f FLAVOR', '--flavor FLAVOR', 'the flavor the instance should work from' ) { |x| @options[:flavor] = x }
24
25
  o.on( '-k KEYPAIR', '--keypair KEYPAIR', 'the keypair the instance should use' ) { |x| @options[:keypair] = x }
25
26
  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( '-s SECURITY', '--secgroups SECURITY', 'the security group assigned to the instance' ) { |x| @options[:security] << x }
27
28
  o.on( '-u USER_DATA', '--user-data USER_DATA', 'the user data template' ) { |x| @options[:user_data] = x }
28
29
  o.on( nil, '--hypervisor HOST', 'the compute node you want the instance to run' ) { |x| @options[:availability_zone] = x }
29
30
  o.on( '-e', '--error', 'cause an error' ) { o.usage }
@@ -38,8 +39,8 @@ def parser
38
39
  o.command_alias :sp
39
40
  o.on( '-H HOSTNAME', '--hostname HOSTNAME', 'the hostname of the instance being snapshot' ) { |x| @options[:hostname] = x }
40
41
  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 }
42
+ o.on( '-w', '--wait', 'wait on the snapshot to complete' ) { @options[:wait] = true }
43
+ o.on( '-f', '--force', 'if the snapshot image exists, delete it' ) { @options[:force] = true }
43
44
  o.on_command { @options[:action] = :snapshot }
44
45
  end
45
46
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: optionscrapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rohith Jayawardene
@@ -17,18 +17,20 @@ extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - .gitignore
20
+ - LICENSE
20
21
  - README.md
21
22
  - lib/optionscrapper.rb
23
+ - lib/optionscrapper/batch.rb
22
24
  - lib/optionscrapper/ext/optparse.rb
23
- - lib/optionscrapper/optparser.rb
24
- - lib/optionscrapper/parsing.rb
25
- - lib/optionscrapper/usage.rb
25
+ - lib/optionscrapper/misc/utils.rb
26
+ - lib/optionscrapper/optionsparser.rb
27
+ - lib/optionscrapper/parser.rb
26
28
  - lib/optionscrapper/version.rb
27
29
  - optionscrapper.gemspec
28
30
  - tests/test.rb
29
31
  homepage: https://github.com/gambol99/optionscrapper
30
32
  licenses:
31
- - MIT
33
+ - GPL
32
34
  metadata: {}
33
35
  post_install_message:
34
36
  rdoc_options: []
@@ -1,95 +0,0 @@
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
@@ -1,123 +0,0 @@
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
@@ -1,65 +0,0 @@
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
-