coglius 0.0.1

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.
Files changed (38) hide show
  1. data/Cogfile +15 -0
  2. data/LICENSE +201 -0
  3. data/lib/coglius.rb +29 -0
  4. data/lib/coglius/app.rb +250 -0
  5. data/lib/coglius/app_support.rb +284 -0
  6. data/lib/coglius/command.rb +149 -0
  7. data/lib/coglius/command_line_option.rb +34 -0
  8. data/lib/coglius/command_line_token.rb +62 -0
  9. data/lib/coglius/command_support.rb +214 -0
  10. data/lib/coglius/commands/compound_command.rb +42 -0
  11. data/lib/coglius/commands/doc.rb +215 -0
  12. data/lib/coglius/commands/help.rb +73 -0
  13. data/lib/coglius/commands/help_modules/arg_name_formatter.rb +20 -0
  14. data/lib/coglius/commands/help_modules/command_finder.rb +60 -0
  15. data/lib/coglius/commands/help_modules/command_help_format.rb +138 -0
  16. data/lib/coglius/commands/help_modules/global_help_format.rb +70 -0
  17. data/lib/coglius/commands/help_modules/help_completion_format.rb +31 -0
  18. data/lib/coglius/commands/help_modules/list_formatter.rb +23 -0
  19. data/lib/coglius/commands/help_modules/one_line_wrapper.rb +18 -0
  20. data/lib/coglius/commands/help_modules/options_formatter.rb +49 -0
  21. data/lib/coglius/commands/help_modules/text_wrapper.rb +53 -0
  22. data/lib/coglius/commands/help_modules/tty_only_wrapper.rb +23 -0
  23. data/lib/coglius/commands/help_modules/verbatim_wrapper.rb +16 -0
  24. data/lib/coglius/commands/initconfig.rb +69 -0
  25. data/lib/coglius/commands/rdoc_document_listener.rb +116 -0
  26. data/lib/coglius/commands/scaffold.rb +401 -0
  27. data/lib/coglius/copy_options_to_aliases.rb +33 -0
  28. data/lib/coglius/dsl.rb +221 -0
  29. data/lib/coglius/exceptions.rb +54 -0
  30. data/lib/coglius/flag.rb +68 -0
  31. data/lib/coglius/gli_option_parser.rb +124 -0
  32. data/lib/coglius/option_parser_factory.rb +45 -0
  33. data/lib/coglius/options.rb +23 -0
  34. data/lib/coglius/switch.rb +35 -0
  35. data/lib/coglius/terminal.rb +94 -0
  36. data/lib/coglius/version.rb +5 -0
  37. data/templates/coglius/generator.rb.erb +26 -0
  38. metadata +208 -0
data/Cogfile ADDED
@@ -0,0 +1,15 @@
1
+ # All paths are relative to the directory containing this file.
2
+
3
+ stamp_generator do |name, dest|
4
+ @name = name
5
+ stamp 'coglius/generator.rb', dest, :absolute_destination => true
6
+ end
7
+
8
+ autoload_plugin :Coglius, 'lib/coglius'
9
+
10
+ # Define a directory in which to find generators
11
+ generator_path 'generators'
12
+
13
+ # Define a directory in which to find templates
14
+ template_path 'templates'
15
+
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/lib/coglius.rb ADDED
@@ -0,0 +1,29 @@
1
+ $LOAD_PATH << File.dirname(__FILE__)
2
+ require 'cog'
3
+ require 'coglius/option_parser_factory.rb'
4
+ require 'coglius/gli_option_parser.rb'
5
+ require 'coglius/app_support.rb'
6
+ require 'coglius/app.rb'
7
+ require 'coglius/command_support.rb'
8
+ require 'coglius/command.rb'
9
+ require 'coglius/command_line_token.rb'
10
+ require 'coglius/command_line_option.rb'
11
+ require 'coglius/copy_options_to_aliases.rb'
12
+ require 'coglius/exceptions.rb'
13
+ require 'coglius/flag.rb'
14
+ require 'coglius/options.rb'
15
+ require 'coglius/switch.rb'
16
+ require 'coglius/dsl.rb'
17
+ require 'coglius/version.rb'
18
+ require 'coglius/commands/help'
19
+ require 'coglius/commands/compound_command'
20
+ require 'coglius/commands/initconfig'
21
+ require 'coglius/commands/rdoc_document_listener'
22
+ require 'coglius/commands/doc'
23
+
24
+ module Coglius
25
+ include Coglius::App
26
+ def self.included(klass)
27
+ warn "You should include Coglius::App instead"
28
+ end
29
+ end
@@ -0,0 +1,250 @@
1
+ require 'etc'
2
+ require 'optparse'
3
+ require 'coglius/copy_options_to_aliases'
4
+ require 'coglius/dsl'
5
+ require 'pathname'
6
+
7
+ module Coglius
8
+ # A means to define and parse a command line interface that works as
9
+ # Git's does, in that you specify global options, a command name, command
10
+ # specific options, and then command arguments.
11
+ module App
12
+ include Cog::Generator
13
+ include CopyOptionsToAliases
14
+ include DSL
15
+ include AppSupport
16
+
17
+ # Generate the command-line interface in the target language
18
+ def stamp_app
19
+ # TODO: write this
20
+ end
21
+
22
+ # Describe the overall application/programm. This should be a one-sentence summary
23
+ # of what your program does that will appear in the help output.
24
+ #
25
+ # +description+:: A String of the short description of your program's purpose
26
+ def program_desc(description=nil)
27
+ @program_desc = description if description
28
+ @program_desc
29
+ end
30
+
31
+ # Provide a longer description of the program. This can be as long as needed, and use double-newlines
32
+ # for paragraphs. This will show up in the help output.
33
+ #
34
+ # description:: A String for the description
35
+ def program_long_desc(description=nil)
36
+ @program_long_desc = description if description
37
+ @program_long_desc
38
+ end
39
+
40
+ # Use this if the following command should not have the pre block executed.
41
+ # By default, the pre block is executed before each command and can result in
42
+ # aborting the call. Using this will avoid that behavior for the following command
43
+ def skips_pre
44
+ @skips_pre = true
45
+ end
46
+
47
+ # Use this if the following command should not have the post block executed.
48
+ # By default, the post block is executed after each command.
49
+ # Using this will avoid that behavior for the following command
50
+ def skips_post
51
+ @skips_post = true
52
+ end
53
+
54
+ # Use this if the following command should not have the around block executed.
55
+ # By default, the around block is executed, but for commands that might not want the
56
+ # setup to happen, this can be handy
57
+ def skips_around
58
+ @skips_around = true
59
+ end
60
+
61
+ # Sets that this app uses a config file as well as the name of the config file.
62
+ #
63
+ # +filename+:: A String representing the path to the file to use for the config file. If it's an absolute
64
+ # path, this is treated as the path to the file. If it's *not*, it's treated as relative to the user's home
65
+ # directory as produced by <code>File.expand_path('~')</code>.
66
+ def config_file(filename)
67
+ @config_file = if filename =~ /^\//
68
+ filename
69
+ else
70
+ File.join(File.expand_path(ENV['HOME']),filename)
71
+ end
72
+ commands[:initconfig] = InitConfig.new(@config_file,commands,flags,switches)
73
+ @commands_declaration_order << commands[:initconfig]
74
+ @config_file
75
+ end
76
+
77
+ # Define a block to run after command line arguments are parsed
78
+ # but before any command is run. If this block raises an exception
79
+ # the command specified will not be executed.
80
+ # The block will receive the global-options,command,options, and arguments
81
+ # If this block evaluates to true, the program will proceed; otherwise
82
+ # the program will end immediately
83
+ def pre(&a_proc)
84
+ @pre_block = a_proc
85
+ end
86
+
87
+ # Define a block to run after the command was executed, <b>only
88
+ # if there was not an error</b>.
89
+ # The block will receive the global-options,command,options, and arguments
90
+ def post(&a_proc)
91
+ @post_block = a_proc
92
+ end
93
+
94
+ # This inverts the pre/post concept. This is useful when you have a global shared resource that is governed by a block
95
+ # instead of separate open/close methods. The block you pass here will be given four parameters:
96
+ #
97
+ # global options:: the parsed global options
98
+ # command:: The Coglius::Command that the user is going to invoke
99
+ # options:: the command specific options
100
+ # args:: unparsed command-line args
101
+ # code:: a block that you must +call+ to execute the command.
102
+ #
103
+ # #help_now! and #exit_now! work as expected; you can abort the command call by simply not calling it.
104
+ #
105
+ # You can declare as many #around blocks as you want. They will be called in the order in which they are defined.
106
+ #
107
+ # Note that if you declare #around blocks, #pre and #post blocks will still work. The #pre is called first, followed by
108
+ # the around, followed by the #post.
109
+ #
110
+ # Call #skips_around before a command that should not have this hook fired
111
+ def around(&a_proc)
112
+ @around_blocks ||= []
113
+ @around_blocks << a_proc
114
+ end
115
+
116
+ # Define a block to run if an error occurs.
117
+ # The block will receive any Exception that was caught.
118
+ # It should evaluate to false to avoid the built-in error handling (which basically just
119
+ # prints out a message). Coglius uses a variety of exceptions that you can use to find out what
120
+ # errors might've occurred during command-line parsing:
121
+ # * Coglius::CustomExit
122
+ # * Coglius::UnknownCommandArgument
123
+ # * Coglius::UnknownGlobalArgument
124
+ # * Coglius::UnknownCommand
125
+ # * Coglius::BadCommandLine
126
+ def on_error(&a_proc)
127
+ @error_block = a_proc
128
+ end
129
+
130
+ # Indicate the version of your application
131
+ #
132
+ # +version+:: String containing the version of your application.
133
+ def version(version)
134
+ @version = version
135
+ switch :version, :negatable => false
136
+ end
137
+
138
+ # By default, Coglius mutates the argument passed to it. This is
139
+ # consistent with +OptionParser+, but be less than ideal. Since
140
+ # that value, for scaffolded apps, is +ARGV+, you might want to
141
+ # refer to the entire command-line via +ARGV+ and thus not want it mutated.
142
+ def preserve_argv(preserve=true)
143
+ @preserve_argv = preserve
144
+ end
145
+
146
+ # Call this with +true+ will cause the +global_options+ and
147
+ # +options+ passed to your code to be wrapped in
148
+ # Options, which is a subclass of +OpenStruct+ that adds
149
+ # <tt>[]</tt> and <tt>[]=</tt> methods.
150
+ #
151
+ # +use_openstruct+:: a Boolean indicating if we should use OpenStruct instead of Hashes
152
+ def use_openstruct(use_openstruct)
153
+ @use_openstruct = use_openstruct
154
+ end
155
+
156
+ # Configure a type conversion not already provided by the underlying OptionParser.
157
+ # This works more or less like the OptionParser version.
158
+ #
159
+ # object:: the class (or whatever) that triggers the type conversion
160
+ # block:: the block that will be given the string argument and is expected
161
+ # to return the converted value
162
+ #
163
+ # Example
164
+ #
165
+ # accept(Hash) do |value|
166
+ # result = {}
167
+ # value.split(/,/) do |pair|
168
+ # k,v = pair.split(/:/)
169
+ # result[k] = v
170
+ # end
171
+ # result
172
+ # end
173
+ #
174
+ # flag :properties, :type => Hash
175
+ def accept(object,&block)
176
+ accepts[object] = block
177
+ end
178
+
179
+ # Simpler means of exiting with a custom exit code. This will
180
+ # raise a CustomExit with the given message and exit code, which will ultimatley
181
+ # cause your application to exit with the given exit_code as its exit status
182
+ # Use #help_now! if you want to show the help in addition to the error message
183
+ #
184
+ # message:: message to show the user
185
+ # exit_code:: exit code to exit as, defaults to 1
186
+ def exit_now!(message,exit_code=1)
187
+ raise CustomExit.new(message,exit_code)
188
+ end
189
+
190
+ # Exit now, showing the user help for the command they executed. Use #exit_now! to just show the error message
191
+ #
192
+ # message:: message to indicate how the user has messed up the CLI invocation or nil to just simply show help
193
+ def help_now!(message=nil)
194
+ exception = OptionParser::ParseError.new(message)
195
+ class << exception
196
+ def exit_code; 64; end
197
+ end
198
+ raise exception
199
+ end
200
+
201
+ # Control how commands and options are sorted in help output. By default, they are sorted alphabetically.
202
+ #
203
+ # sort_type:: How you want help commands/options sorted:
204
+ # +:manually+:: help commands/options are ordered in the order declared.
205
+ # +:alpha+:: sort alphabetically (default)
206
+ def sort_help(sort_type)
207
+ @help_sort_type = sort_type
208
+ end
209
+
210
+ # Set how help text is wrapped.
211
+ #
212
+ # wrap_type:: Symbol indicating how you'd like text wrapped:
213
+ # +:to_terminal+:: Wrap text based on the width of the terminal (default)
214
+ # +:verbatim+:: Format text exactly as it was given to the various methods. This is useful if your output has
215
+ # formatted output, e.g. ascii tables and you don't want it messed with.
216
+ # +:one_line+:: Do not wrap text at all. This will bring all help content onto one line, removing any newlines
217
+ # +:tty_only+:: Wrap like +:to_terminal+ if this output is going to a TTY, otherwise don't wrap (like +:one_line+)
218
+ def wrap_help_text(wrap_type)
219
+ @help_text_wrap_type = wrap_type
220
+ end
221
+
222
+ def program_name(override=nil) #:nodoc:
223
+ warn "#program_name has been deprecated"
224
+ end
225
+
226
+ # Sets a default command to run when none is specified on the command line. Note that
227
+ # if you use this, you won't be able to pass arguments, flags, or switches
228
+ # to the command when run in default mode. All flags and switches are treated
229
+ # as global, and any argument will be interpretted as the command name and likely
230
+ # fail.
231
+ #
232
+ # +command+:: Command as a Symbol to run as default
233
+ def default_command(command)
234
+ @default_command = command.to_sym
235
+ end
236
+
237
+ private
238
+
239
+ def load_commands(path)
240
+ if File.exists? path
241
+ Dir.entries(path).sort.each do |entry|
242
+ file = File.join(path,entry)
243
+ if file =~ /\.rb$/
244
+ require file
245
+ end
246
+ end
247
+ end
248
+ end
249
+ end
250
+ end