moses 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Binary file
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format progress --format UnicornFormatter
@@ -0,0 +1 @@
1
+ 1.9.3-p392
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem "rspec"
5
+ gem "bundler"
6
+ gem "jeweler"
7
+ gem "pry"
8
+ gem "pry-nav"
9
+ end
10
+
11
+ group :test do
12
+ gem 'nyan-cat-formatter'
13
+ gem 'unicorn-formatter'
14
+ end
@@ -0,0 +1,47 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ coderay (1.0.9)
5
+ diff-lcs (1.2.4)
6
+ git (1.2.5)
7
+ jeweler (1.8.4)
8
+ bundler (~> 1.0)
9
+ git (>= 1.2.5)
10
+ rake
11
+ rdoc
12
+ json (1.7.7)
13
+ method_source (0.8.1)
14
+ nyan-cat-formatter (0.5.0)
15
+ rspec (>= 2.13)
16
+ pry (0.9.12)
17
+ coderay (~> 1.0.5)
18
+ method_source (~> 0.8)
19
+ slop (~> 3.4)
20
+ pry-nav (0.2.2)
21
+ pry (~> 0.9.10)
22
+ rake (10.0.4)
23
+ rdoc (4.0.1)
24
+ json (~> 1.4)
25
+ rspec (2.13.0)
26
+ rspec-core (~> 2.13.0)
27
+ rspec-expectations (~> 2.13.0)
28
+ rspec-mocks (~> 2.13.0)
29
+ rspec-core (2.13.1)
30
+ rspec-expectations (2.13.0)
31
+ diff-lcs (>= 1.1.3, < 2.0)
32
+ rspec-mocks (2.13.1)
33
+ slop (3.4.4)
34
+ unicorn-formatter (0.1.3)
35
+ rspec
36
+
37
+ PLATFORMS
38
+ ruby
39
+
40
+ DEPENDENCIES
41
+ bundler
42
+ jeweler
43
+ nyan-cat-formatter
44
+ pry
45
+ pry-nav
46
+ rspec
47
+ unicorn-formatter
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2013 Dayton Nolan
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Dayton Nolan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ = parsely
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to parsely
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2013 Dayton Nolan. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "moses"
18
+ gem.homepage = "http://github.com/daytonn/moses"
19
+ gem.license = "Apache 2.0"
20
+ gem.summary = %Q{Moses is a simple command parser for writing command line applications}
21
+ gem.description = %Q{Moses is a simple command parser for writing command line applications}
22
+ gem.email = "daytonn@gmail.com"
23
+ gem.authors = ["Dayton Nolan"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "moses #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'autospec' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rspec-core', 'autospec')
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'coderay' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('coderay', 'coderay')
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'htmldiff' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('diff-lcs', 'htmldiff')
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'jeweler' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('jeweler', 'jeweler')
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'ldiff' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('diff-lcs', 'ldiff')
data/bin/pry ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'pry' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('pry', 'pry')
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rake' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rake', 'rake')
data/bin/ri ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'ri' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rdoc', 'ri')
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rspec' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rspec-core', 'rspec')
@@ -0,0 +1,150 @@
1
+ module Moses
2
+
3
+ VERSION_FILE = 'VERSION'
4
+ HELP_FILE = 'HELP'
5
+
6
+ attr_reader :args, :command, :options
7
+ attr :default_option_commands, :default_commands
8
+
9
+ module ClassMethods
10
+ def commands(*args)
11
+ class_eval %Q{
12
+ def commands
13
+ @commands ||= #{args}
14
+ end
15
+ }
16
+ end
17
+
18
+ def default_command(cmd)
19
+ class_eval %Q{
20
+ def default_command
21
+ @default_command ||= "#{cmd}".to_sym
22
+ end
23
+ }
24
+ end
25
+
26
+ def option_commands(option_cmds)
27
+ class_eval %Q{
28
+ def option_commands
29
+ @option_commands ||= default_option_commands.merge(#{option_cmds})
30
+ end
31
+ }
32
+ end
33
+ end
34
+
35
+ def self.included(base)
36
+ base.extend(ClassMethods)
37
+ class_eval %Q{
38
+ def output
39
+ @output ||= STDOUT
40
+ end
41
+ }
42
+ end
43
+
44
+ def default_commands
45
+ @default_commands ||= [:help, :version]
46
+ end
47
+
48
+ def default_option_commands
49
+ @default_option_commands ||= {
50
+ h: :help,
51
+ help: :help,
52
+ version: :version,
53
+ v: :version
54
+ }
55
+ end
56
+
57
+ def run
58
+ @options = {}
59
+ @args = Array.try_convert ARGV
60
+ parse_options
61
+ parse_command
62
+ set_option_command
63
+
64
+ self.send(@command) if valid_command?
65
+ self.send(@default_command || :help) if @command.nil?
66
+ end
67
+
68
+ private
69
+
70
+ def parse_options
71
+ @args.each_with_index do |arg, index|
72
+ if flag?(arg)
73
+ next_index = index + 1
74
+ create_variable_option(arg, @args[next_index]) if variable_option? index
75
+ create_boolean_option(arg) unless variable_option? index
76
+ end
77
+ end
78
+ @args.delete_if { |a| flag?(a) }
79
+ end
80
+
81
+ def parse_command
82
+ @command = @args.shift.to_sym if @args.first && @args.first.respond_to?(:to_sym)
83
+ end
84
+
85
+ def set_option_command
86
+ @options.each do |opt, v|
87
+ if self.class.method_defined?(:option_commands) && option_commands[opt]
88
+ @command = option_commands[opt]
89
+ return
90
+ end
91
+
92
+ if default_option_commands[opt]
93
+ @command = default_option_commands[opt]
94
+ return
95
+ end
96
+ end
97
+ end
98
+
99
+ def valid_command?
100
+ default_commands.include?(@command) || [*commands].include?(@command) && self.class.method_defined?(@command)
101
+ end
102
+
103
+ def create_variable_option(flag, value = nil)
104
+ key = flag.gsub(/^--/, '').to_sym
105
+ @options[key] = value ? value : true
106
+ end
107
+
108
+ def create_boolean_option(flag)
109
+ key = flag.gsub(/^-{1,2}/, '').to_sym
110
+ @options[key] = true
111
+ end
112
+
113
+ def flag?(arg)
114
+ arg =~ /^-/
115
+ end
116
+
117
+ def long_flag?(arg)
118
+ arg =~ /^-{2}/
119
+ end
120
+
121
+ def not_flag?(arg)
122
+ arg =~ /^[^-]/
123
+ end
124
+
125
+ def variable_option?(i)
126
+ arg = @args[i]
127
+ next_arg = @args[i+1]
128
+ long_flag?(arg) && next_arg && not_flag?(next_arg)
129
+ end
130
+
131
+ def help
132
+ @output.puts help_file_content || "Include a HELP file in your project or define a help method"
133
+ end
134
+
135
+ def help_file_content
136
+ content = File.read(File.expand_path(Moses::HELP_FILE)) if File.exist?(File.expand_path(Moses::HELP_FILE))
137
+ content = File.read(File.expand_path("#{Moses::HELP_FILE}.md")) if File.exist?(File.expand_path("#{Moses::HELP_FILE}.md"))
138
+ content
139
+ end
140
+
141
+ def version
142
+ @output.puts version_file_content || "Include a VERSION file in your project or define a version method"
143
+ end
144
+
145
+ def version_file_content
146
+ content = File.read(File.expand_path(Moses::VERSION_FILE)) if File.exist?(File.expand_path(Moses::VERSION_FILE))
147
+ content
148
+ end
149
+
150
+ end
@@ -0,0 +1,78 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "moses"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Dayton Nolan"]
12
+ s.date = "2013-05-13"
13
+ s.description = "Moses is a simple command parser for writing command line applications"
14
+ s.email = "daytonn@gmail.com"
15
+ s.executables = ["autospec", "coderay", "htmldiff", "jeweler", "ldiff", "pry", "rake", "ri", "rspec"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE",
18
+ "LICENSE.txt",
19
+ "README.rdoc"
20
+ ]
21
+ s.files = [
22
+ ".DS_Store",
23
+ ".document",
24
+ ".rspec",
25
+ ".ruby-version",
26
+ "Gemfile",
27
+ "Gemfile.lock",
28
+ "LICENSE",
29
+ "LICENSE.txt",
30
+ "README.rdoc",
31
+ "Rakefile",
32
+ "VERSION",
33
+ "bin/autospec",
34
+ "bin/coderay",
35
+ "bin/htmldiff",
36
+ "bin/jeweler",
37
+ "bin/ldiff",
38
+ "bin/pry",
39
+ "bin/rake",
40
+ "bin/ri",
41
+ "bin/rspec",
42
+ "lib/moses.rb",
43
+ "moses.gemspec",
44
+ "spec/moses_spec.rb",
45
+ "spec/spec_helper.rb",
46
+ "test.rb"
47
+ ]
48
+ s.homepage = "http://github.com/daytonn/moses"
49
+ s.licenses = ["Apache 2.0"]
50
+ s.require_paths = ["lib"]
51
+ s.rubygems_version = "1.8.23"
52
+ s.summary = "Moses is a simple command parser for writing command line applications"
53
+
54
+ if s.respond_to? :specification_version then
55
+ s.specification_version = 3
56
+
57
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
58
+ s.add_development_dependency(%q<rspec>, [">= 0"])
59
+ s.add_development_dependency(%q<bundler>, [">= 0"])
60
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
61
+ s.add_development_dependency(%q<pry>, [">= 0"])
62
+ s.add_development_dependency(%q<pry-nav>, [">= 0"])
63
+ else
64
+ s.add_dependency(%q<rspec>, [">= 0"])
65
+ s.add_dependency(%q<bundler>, [">= 0"])
66
+ s.add_dependency(%q<jeweler>, [">= 0"])
67
+ s.add_dependency(%q<pry>, [">= 0"])
68
+ s.add_dependency(%q<pry-nav>, [">= 0"])
69
+ end
70
+ else
71
+ s.add_dependency(%q<rspec>, [">= 0"])
72
+ s.add_dependency(%q<bundler>, [">= 0"])
73
+ s.add_dependency(%q<jeweler>, [">= 0"])
74
+ s.add_dependency(%q<pry>, [">= 0"])
75
+ s.add_dependency(%q<pry-nav>, [">= 0"])
76
+ end
77
+ end
78
+
@@ -0,0 +1,276 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Moses do
4
+ before(:all) do
5
+ @output = StringIO.new
6
+ class TestAppClass
7
+ include Moses
8
+ commands :foo, :bar
9
+
10
+ def initialize(output)
11
+ @output = output
12
+ end
13
+
14
+ def foo
15
+ end
16
+ end
17
+ @app = TestAppClass.new(@output)
18
+ end
19
+
20
+ after do
21
+ ARGV.clear
22
+ end
23
+
24
+ describe 'output' do
25
+ it "defaults to STDOUT" do
26
+ class TestOutputAppClass
27
+ include Moses
28
+ end
29
+ app = TestOutputAppClass.new
30
+ expect(app.output).to eq(STDOUT)
31
+ end
32
+
33
+ it "defers to class defined output" do
34
+ expect(@app.output).to eq(@output)
35
+ end
36
+ end
37
+
38
+ describe 'help' do
39
+ after do
40
+ FileUtils.rm_rf('HELP') if File.exist?('HELP')
41
+ FileUtils.rm_rf('HELP.md') if File.exist?('HELP.md')
42
+ end
43
+
44
+ it "has a HELP_FILE constant" do
45
+ expect(Moses::HELP_FILE).to eq('HELP')
46
+ end
47
+
48
+ describe "help_file_content" do
49
+ it "returns the contents of the HELP file" do
50
+ File.open('HELP', 'w+') do |f|
51
+ f << 'Content from the HELP file'
52
+ end
53
+ expect(@app.send(:help_file_content)).to eq("Content from the HELP file")
54
+ end
55
+
56
+ it "returns the contents of the HELP.md file" do
57
+ File.open('HELP.md', 'w+') do |f|
58
+ f << 'Content from the HELP.md file'
59
+ end
60
+ expect(@app.send(:help_file_content)).to eq("Content from the HELP.md file")
61
+ end
62
+ end
63
+
64
+ it "outputs an instruction message when no HELP file is present" do
65
+ @output.should_receive(:puts).with("Include a HELP file in your project or define a help method")
66
+ @app.send(:help)
67
+ end
68
+
69
+ it "outputs the contents of the HELP file if present" do
70
+ File.open('HELP', 'w+') do |f|
71
+ f << 'Content from the HELP file'
72
+ end
73
+ @output.should_receive(:puts).with("Content from the HELP file")
74
+ @app.send(:help)
75
+ end
76
+
77
+ it "outputs the contents of the HELP.md file if present" do
78
+ File.open('HELP.md', 'w+') do |f|
79
+ f << 'Content from the HELP.md file'
80
+ end
81
+ @output.should_receive(:puts).with("Content from the HELP.md file")
82
+ @app.send(:help)
83
+ end
84
+ end
85
+
86
+ describe 'version' do
87
+ after do
88
+ FileUtils.rm_rf('VERSION.test') if File.exist?('VERSION.test')
89
+ end
90
+
91
+ it "has a VERSION_FILE constant" do
92
+ expect(Moses::VERSION_FILE).to eq('VERSION')
93
+ end
94
+
95
+ describe "version_file_content" do
96
+ it "returns the contents of the VERSION file" do
97
+ module Moses VERSION_FILE = 'VERSION.test'; end
98
+ File.open('VERSION.test', 'w+') do |f|
99
+ f << 'x.x.x'
100
+ end
101
+ expect(@app.send(:version_file_content)).to eq("x.x.x")
102
+ end
103
+ end
104
+
105
+ it "outputs an instruction message when no VERSION file is present" do
106
+ module Moses VERSION_FILE = 'VERSION.test'; end
107
+ @output.should_receive(:puts).with("Include a VERSION file in your project or define a version method")
108
+ @app.send(:version)
109
+ end
110
+
111
+ it "returns the contents of the VERSION file" do
112
+ module Moses VERSION_FILE = 'VERSION.test'; end
113
+ File.open("VERSION.test", "w+") do |f|
114
+ f << 'x.x.x'
115
+ end
116
+ @output.should_receive(:puts).with("x.x.x")
117
+ @app.send(:version)
118
+ end
119
+ end
120
+
121
+ describe "default_command" do
122
+ it "can be set with the class method" do
123
+ class CustomDefaultCommandClass
124
+ include Moses
125
+ default_command :some_command
126
+ end
127
+ @dc_app = CustomDefaultCommandClass.new
128
+ expect(@dc_app.default_command).to eq(:some_command)
129
+ end
130
+ end
131
+
132
+ describe 'option commands' do
133
+ it "has default option commands" do
134
+ expect(@app.default_option_commands).to have_key(:h)
135
+ expect(@app.default_option_commands).to have_key(:help)
136
+ expect(@app.default_option_commands).to have_key(:v)
137
+ expect(@app.default_option_commands).to have_key(:version)
138
+
139
+ expect(@app.default_option_commands[:h]).to eq(:help)
140
+ expect(@app.default_option_commands[:help]).to eq(:help)
141
+ expect(@app.default_option_commands[:v]).to eq(:version)
142
+ expect(@app.default_option_commands[:version]).to eq(:version)
143
+ end
144
+
145
+ it "assigns option commands" do
146
+ class OptionCommandsClass
147
+ include Moses
148
+ option_commands({ foo: :foo, v: :verbose })
149
+ end
150
+ @opt_cmd = OptionCommandsClass.new
151
+
152
+ expect(@opt_cmd.option_commands).to have_key(:foo)
153
+ expect(@opt_cmd.option_commands).to have_key(:h)
154
+ expect(@opt_cmd.option_commands).to have_key(:help)
155
+ expect(@opt_cmd.option_commands).to have_key(:v)
156
+ expect(@opt_cmd.option_commands).to have_key(:version)
157
+
158
+ expect(@opt_cmd.option_commands[:foo]).to eq(:foo)
159
+ expect(@opt_cmd.option_commands[:h]).to eq(:help)
160
+ expect(@opt_cmd.option_commands[:help]).to eq(:help)
161
+ expect(@opt_cmd.option_commands[:v]).to eq(:verbose)
162
+ expect(@opt_cmd.option_commands[:version]).to eq(:version)
163
+ end
164
+
165
+ it "sets the command to the default option command if the default option is set" do
166
+ ARGV = ['-h']
167
+ @app.run
168
+ expect(@app.command).to eq(:help)
169
+ end
170
+
171
+ it "sets the command to the option command if the option is set" do
172
+ class OptionCommandsClass
173
+ include Moses
174
+ commands :verbose
175
+ option_commands({ foo: :foo, v: :verbose })
176
+ end
177
+ @opt_cmd = OptionCommandsClass.new
178
+ ARGV = ['-v']
179
+ @opt_cmd.run
180
+ expect(@opt_cmd.command).to eq(:verbose)
181
+ end
182
+ end
183
+
184
+ describe "commands" do
185
+ it "returns a an array of whitelisted commands" do
186
+ expect(@app.commands).to include(:foo)
187
+ expect(@app.commands).to include(:bar)
188
+ end
189
+ end
190
+
191
+ describe "run" do
192
+ it "creates a local array of the ARGV array" do
193
+ ARGV = ['one', 'two', 'three']
194
+ @app.stub(:parse_command)
195
+ @app.run
196
+ expect(@app.args).to eq(['one', 'two', 'three'])
197
+ end
198
+
199
+ describe "command parsing" do
200
+ before do
201
+ ARGV = ['foo']
202
+ @app.run
203
+ end
204
+
205
+ it "parses the command" do
206
+ expect(@app.command).to eq(:foo)
207
+ end
208
+
209
+ it "removes the command from the args array" do
210
+ expect(@app.args).to eq([])
211
+ end
212
+ end
213
+
214
+ describe "option parsing" do
215
+ describe "single dash flag" do
216
+ it "creates a boolean option with single dash flags -f" do
217
+ ARGV = ['foo', '-f']
218
+ @app.run
219
+ expect(@app.options[:f]).to be_true
220
+ end
221
+ end
222
+
223
+ describe 'double dash flag' do
224
+
225
+ it "removes the flags from the args array" do
226
+ ARGV = ['foo', '--flag', '-f']
227
+ @app.run
228
+ expect(@app.args).to eq([])
229
+ end
230
+
231
+ context 'with value' do
232
+ it "creates a variable option" do
233
+ ARGV = ['foo', '--flag', 'value']
234
+ @app.run
235
+ expect(@app.options[:flag]).to eq('value')
236
+ end
237
+ end
238
+
239
+ context 'without value' do
240
+ it "creates a boolean option" do
241
+ ARGV = ['foo', '--flag']
242
+ @app.run
243
+ expect(@app.options[:flag]).to be_true
244
+ end
245
+ end
246
+ end
247
+ end
248
+
249
+ describe "running commands" do
250
+ it "sends the command if it is in the commands white list" do
251
+ ARGV = ['foo']
252
+ @app.stub(:foo)
253
+ @app.should_receive(:foo)
254
+ @app.run
255
+ end
256
+
257
+ it "does not send a command if it is not in the commands white list" do
258
+ ARGV = ['baz']
259
+ @app.should_not_receive(:baz)
260
+ @app.run
261
+ end
262
+
263
+ it "does not send a command if the method is not defined" do
264
+ ARGV = ['bar']
265
+ @app.should_not_receive(:bar)
266
+ @app.run
267
+ end
268
+
269
+ it "runs any option commands if the option was passed" do
270
+ ARGV = ['-h']
271
+ @app.should_receive(:help)
272
+ @app.run
273
+ end
274
+ end
275
+ end
276
+ end
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'moses'
5
+ require 'pry'
6
+ require 'pry-nav'
7
+
8
+ # Requires supporting files with custom matchers and macros, etc,
9
+ # in ./support/ and its subdirectories.
10
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
11
+
12
+ RSpec.configure do |config|
13
+
14
+ end
data/test.rb ADDED
@@ -0,0 +1,27 @@
1
+ require 'pry'
2
+
3
+ module Foo
4
+
5
+ def self.included(base)
6
+ base.extend(ClassMethods)
7
+ end
8
+
9
+ module ClassMethods
10
+ def commands(*args)
11
+ class_eval %Q{
12
+ def commands
13
+ @commands ||= #{args}
14
+ end
15
+ }
16
+ end
17
+ end
18
+ end
19
+
20
+ class Bar
21
+ include Foo
22
+
23
+ commands :foo, :bar
24
+ end
25
+
26
+ b = Bar.new
27
+ puts b.commands
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: moses
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dayton Nolan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: jeweler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: pry
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: pry-nav
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Moses is a simple command parser for writing command line applications
95
+ email: daytonn@gmail.com
96
+ executables:
97
+ - autospec
98
+ - coderay
99
+ - htmldiff
100
+ - jeweler
101
+ - ldiff
102
+ - pry
103
+ - rake
104
+ - ri
105
+ - rspec
106
+ extensions: []
107
+ extra_rdoc_files:
108
+ - LICENSE
109
+ - LICENSE.txt
110
+ - README.rdoc
111
+ files:
112
+ - .DS_Store
113
+ - .document
114
+ - .rspec
115
+ - .ruby-version
116
+ - Gemfile
117
+ - Gemfile.lock
118
+ - LICENSE
119
+ - LICENSE.txt
120
+ - README.rdoc
121
+ - Rakefile
122
+ - VERSION
123
+ - bin/autospec
124
+ - bin/coderay
125
+ - bin/htmldiff
126
+ - bin/jeweler
127
+ - bin/ldiff
128
+ - bin/pry
129
+ - bin/rake
130
+ - bin/ri
131
+ - bin/rspec
132
+ - lib/moses.rb
133
+ - moses.gemspec
134
+ - spec/moses_spec.rb
135
+ - spec/spec_helper.rb
136
+ - test.rb
137
+ homepage: http://github.com/daytonn/moses
138
+ licenses:
139
+ - Apache 2.0
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ segments:
151
+ - 0
152
+ hash: 4489770416600735452
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
+ none: false
155
+ requirements:
156
+ - - ! '>='
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ requirements: []
160
+ rubyforge_project:
161
+ rubygems_version: 1.8.23
162
+ signing_key:
163
+ specification_version: 3
164
+ summary: Moses is a simple command parser for writing command line applications
165
+ test_files: []