moses 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,5 +1,7 @@
1
1
  source "http://rubygems.org"
2
2
 
3
+ gem 'activesupport'
4
+
3
5
  group :development do
4
6
  gem "rspec"
5
7
  gem "bundler"
data/Gemfile.lock CHANGED
@@ -1,9 +1,13 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
+ activesupport (3.2.13)
5
+ i18n (= 0.6.1)
6
+ multi_json (~> 1.0)
4
7
  coderay (1.0.9)
5
8
  diff-lcs (1.2.4)
6
9
  git (1.2.5)
10
+ i18n (0.6.1)
7
11
  jeweler (1.8.4)
8
12
  bundler (~> 1.0)
9
13
  git (>= 1.2.5)
@@ -11,6 +15,7 @@ GEM
11
15
  rdoc
12
16
  json (1.7.7)
13
17
  method_source (0.8.1)
18
+ multi_json (1.7.3)
14
19
  nyan-cat-formatter (0.5.0)
15
20
  rspec (>= 2.13)
16
21
  pry (0.9.12)
@@ -38,6 +43,7 @@ PLATFORMS
38
43
  ruby
39
44
 
40
45
  DEPENDENCIES
46
+ activesupport
41
47
  bundler
42
48
  jeweler
43
49
  nyan-cat-formatter
data/HELP.md ADDED
@@ -0,0 +1,10 @@
1
+ Moses
2
+ =====
3
+
4
+ The moses command line utility is a simple script to generate a Moses application scaffold.
5
+
6
+ Usage
7
+ -----
8
+ moses create app_name
9
+
10
+ This creates all the necessary files and folders to create a Moses-based command line application
data/README.md ADDED
@@ -0,0 +1,191 @@
1
+ Moses
2
+ =====
3
+ Moses is a simple option/command parser for building command line applications in Ruby.
4
+ ![image](moses.png)
5
+
6
+ Getting Started
7
+ ---------------
8
+
9
+ Installation with ruby-gems:
10
+
11
+ gem install moses
12
+
13
+ Installation with bundler:
14
+
15
+ gem 'moses', '0.1.5'
16
+
17
+ Creating a Moses application
18
+ ----------------------------
19
+
20
+ Moses is a module that you can include into any Ruby class to provide all the plumbing you need to write command line applications. Create the following directory structure for your application
21
+
22
+ - bin
23
+ - myapp
24
+ - lib
25
+ - myapp.rb
26
+ - VERSION
27
+ - HELP.md (or just HELP)
28
+
29
+ Create a class for your applications functionality:
30
+
31
+ ```rb
32
+ class MyApp
33
+ include Moses
34
+ commands :foo, :bar
35
+
36
+ def foo
37
+ # do stuff
38
+ end
39
+
40
+ def bar
41
+ # do stuff
42
+ end
43
+ end
44
+ ```
45
+
46
+ Then all you need in your executable file is the following:
47
+
48
+ ```rb
49
+ #!/usr/bin/env ruby
50
+ $: << File.expand_path(File.join(File.dirname(__FILE__), "../lib"))
51
+ require 'myapp'
52
+ MyApp.new.run
53
+ ```
54
+
55
+ That's all you need to create a CLI app. You can now call your application like so:
56
+
57
+ myapp foo
58
+ myapp bar
59
+
60
+ By default, Moses will read the contents of the HELP.md (or HELP) file in the root of your project. This will be used to display your application instructions when passed the flags `-h` and `--help`
61
+
62
+ myapp -h
63
+ myapp --help
64
+
65
+ Similarly, Moses will use the VERSION file with the `-v` and `--version` flags.
66
+
67
+ myapp -v
68
+ myapp --version
69
+
70
+ You may choose not to use `VERSION` or `HELP` files in your project and simply define corresponding methods that output what you want:
71
+
72
+ ```rb
73
+ def help
74
+ output.puts "This is my help text"
75
+ end
76
+
77
+ def version
78
+ output.puts "X.X.X"
79
+ end
80
+ ```
81
+
82
+ Commands
83
+ --------
84
+
85
+ Moses treats the first non-flag argument passed to it as a command (sometimes referred to as sub-commands). A command is simply a method defined in your application class. To define a command, simply add a command definition to your class and implement a method of the same name:
86
+
87
+ ```rb
88
+ class MyApp
89
+ include Moses
90
+ commands :foo
91
+
92
+ def foo
93
+ # do stuff
94
+ end
95
+ end
96
+ ```
97
+
98
+ This will create a `foo` command that can be called from the command line: `myapp foo`
99
+
100
+ ### Default command
101
+
102
+ Sometimes you will want to create an application that has no "sub-commands" and will simply be called from the command line with options. To run a command by default, add a default_command definition to your class:
103
+
104
+ ```rb
105
+ class MyApp
106
+ include Moses
107
+ default_command :main
108
+
109
+ def main
110
+ # do stuff
111
+ end
112
+ end
113
+ ```
114
+
115
+ In the above example, the `main` method will execute whenever your application is run. Simply calling `myapp` from the command line will execute the `main` method.
116
+
117
+
118
+ Options
119
+ -------
120
+
121
+ Moses will parse any flags passed to your application that begin with one or two dashes. There are two basic types of flags, boolean and variable flags.
122
+
123
+ ### Boolean flags
124
+
125
+ Flags which begin with one dash are treated as boolean flags. A boolean flag is basically an _ON_ switch:
126
+
127
+ myapp -x
128
+
129
+ The above will create an `:x` key in the `@options` hash that is true. Given the above switch, we could write code to handle that switch like so:
130
+
131
+ ```rb
132
+ if @options[:x]
133
+ # stuff here
134
+ end
135
+ ```
136
+
137
+ ### Variable flags
138
+
139
+ Flags which begin with two dashes are slightly trickier than boolean flags. Variable flags can be either boolean or be passed a value. Variable flags will be treated as a boolean if no arguments are passed after them:
140
+
141
+ myapp --foo
142
+
143
+ In the above example the value of `@options[:foo]` will be true. However if an argument is passed after the flag, it will automatically be assigned as the value:
144
+
145
+ myapp --foo bar
146
+
147
+ In this case the value of `@options[:foo]` will be the string `bar`. The flag can still be treated as a boolean but it's adjacent argument can also be accessed as it's value:
148
+
149
+ ```rb
150
+ if @options[:foo]
151
+ x = @options[:foo] # bar
152
+ end
153
+ ```
154
+
155
+ ### Option commands
156
+
157
+ It's common to run a command as the result of an option being passed (ie. `-h` or `--help`). To run a command automatically when an option is passed, simply add an `option_commands` definition to your class:
158
+
159
+ ```rb
160
+ class MyApp
161
+ include Moses
162
+ option_commands({ :auto => :run_automatic })
163
+
164
+ def run_automatic
165
+ # automatically runs when --auto is passed
166
+ end
167
+ end
168
+ ```
169
+
170
+ _Note: When using `option_commands`, no other command will be run. If you wish to execute multiple commands based on an option, simply use the boolean flag._
171
+
172
+ Output
173
+ ------
174
+
175
+ By default Moses exposes `$stdout` through an `output` method. To output to the terminal, simply use `output.puts`:
176
+
177
+ ```rb
178
+ output.puts "This will be output to STDOUT"
179
+ ```
180
+
181
+ To substitute your own output, define an initializer that overwrites the `@output` variable:
182
+
183
+ ```rb
184
+ class MyApp
185
+ def initialize(output)
186
+ @output = output
187
+ end
188
+ end
189
+
190
+ MyApp.new(File.open('output.txt'))
191
+ ```
data/Rakefile CHANGED
@@ -21,6 +21,7 @@ Jeweler::Tasks.new do |gem|
21
21
  gem.description = %Q{Moses is a simple command parser for writing command line applications}
22
22
  gem.email = "daytonn@gmail.com"
23
23
  gem.authors = ["Dayton Nolan"]
24
+ gem.executables = ["moses"]
24
25
  # dependencies defined in Gemfile
25
26
  end
26
27
  Jeweler::RubygemsDotOrgTasks.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.1.5
data/bin/moses ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ $: << File.expand_path(File.join(File.dirname(__FILE__), "../lib"))
3
+ require 'moses/application'
4
+ Moses::Application.new.run
@@ -0,0 +1,88 @@
1
+ require 'moses'
2
+ require 'fileutils'
3
+ require 'active_support/inflector'
4
+
5
+ class Moses::Application
6
+ include Moses
7
+ commands :create
8
+ attr_reader :root_path
9
+
10
+ def initialize(root_path = Dir.getwd)
11
+ @root_path = root_path
12
+ end
13
+
14
+ def create
15
+ if @args.first
16
+ @app_name = @args.first
17
+ create_app_dir
18
+ create_help_file
19
+ create_version_file
20
+ create_bin_dir
21
+ create_bin_file
22
+ create_lib_dir
23
+ create_application_class
24
+ else
25
+ output.puts "You need to name your application: moses create myapp"
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def create_app_dir
32
+ FileUtils.mkdir("#{@root_path}/#{@app_name}") unless File.directory?("#{@root_path}/#{@app_name}")
33
+ end
34
+
35
+ def create_help_file
36
+ unless File.file?("#{@root_path}/#{@app_name}/HELP.md")
37
+ FileUtils.touch("#{@root_path}/#{@app_name}/HELP.md")
38
+ File.open("#{@root_path}/#{@app_name}/HELP.md", 'w+') do |f|
39
+ f << "Todo: Add your own instructions"
40
+ end
41
+ end
42
+ end
43
+
44
+ def create_version_file
45
+ unless File.file?("#{@root_path}/#{@app_name}/VERSION")
46
+ FileUtils.touch("#{@root_path}/#{@app_name}/VERSION")
47
+ File.open("#{@root_path}/#{@app_name}/VERSION", 'w+') do |f|
48
+ f << "0.0.0"
49
+ end
50
+ end
51
+ end
52
+
53
+ def create_bin_dir
54
+ FileUtils.mkdir("#{@root_path}/#{@app_name}/bin") unless File.directory?("#{@root_path}/#{@app_name}/bin")
55
+ end
56
+
57
+ def create_bin_file
58
+ unless File.file?("#{@root_path}/#{@app_name}/bin/#{@app_name}")
59
+ FileUtils.touch("#{@root_path}/#{@app_name}/bin/#{@app_name}")
60
+ FileUtils.chmod("u+x", "#{@root_path}/#{@app_name}/bin/#{@app_name}")
61
+ File.open("#{@root_path}/#{@app_name}/bin/#{@app_name}", 'w+') do |f|
62
+ f << %Q{
63
+ #!/usr/bin/env ruby
64
+ $: << File.expand_path(File.join(File.dirname(__FILE__), "../lib"))
65
+ require '#{@app_name}'
66
+ #{@app_name.camelize}.new.run
67
+ }
68
+ end
69
+ end
70
+ end
71
+
72
+ def create_lib_dir
73
+ FileUtils.mkdir("#{@root_path}/#{@app_name}/lib") unless File.directory?("#{@root_path}/#{@app_name}/lib")
74
+ end
75
+
76
+ def create_application_class
77
+ unless File.file?("#{@root_path}/#{@app_name}/lib/#{@app_name}.rb")
78
+ FileUtils.touch("#{@root_path}/#{@app_name}/lib/#{@app_name}.rb")
79
+ File.open("#{@root_path}/#{@app_name}/lib/#{@app_name}.rb", 'w+') do |f|
80
+ f << %Q{
81
+ class #{@app_name.camelize}
82
+ include Moses
83
+ end
84
+ }
85
+ end
86
+ end
87
+ end
88
+ end
data/moses.gemspec CHANGED
@@ -5,15 +5,17 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "moses"
8
- s.version = "0.1.4"
8
+ s.version = "0.1.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Dayton Nolan"]
12
- s.date = "2013-05-13"
12
+ s.date = "2013-05-14"
13
13
  s.description = "Moses is a simple command parser for writing command line applications"
14
14
  s.email = "daytonn@gmail.com"
15
+ s.executables = ["moses"]
15
16
  s.extra_rdoc_files = [
16
- "LICENSE"
17
+ "LICENSE",
18
+ "README.md"
17
19
  ]
18
20
  s.files = [
19
21
  ".DS_Store",
@@ -22,11 +24,17 @@ Gem::Specification.new do |s|
22
24
  ".ruby-version",
23
25
  "Gemfile",
24
26
  "Gemfile.lock",
27
+ "HELP.md",
25
28
  "LICENSE",
29
+ "README.md",
26
30
  "Rakefile",
27
31
  "VERSION",
32
+ "bin/moses",
28
33
  "lib/moses.rb",
34
+ "lib/moses/application.rb",
29
35
  "moses.gemspec",
36
+ "moses.png",
37
+ "spec/application_spec.rb",
30
38
  "spec/moses_spec.rb",
31
39
  "spec/spec_helper.rb",
32
40
  "test.rb"
@@ -41,12 +49,14 @@ Gem::Specification.new do |s|
41
49
  s.specification_version = 3
42
50
 
43
51
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
52
+ s.add_runtime_dependency(%q<activesupport>, [">= 0"])
44
53
  s.add_development_dependency(%q<rspec>, [">= 0"])
45
54
  s.add_development_dependency(%q<bundler>, [">= 0"])
46
55
  s.add_development_dependency(%q<jeweler>, [">= 0"])
47
56
  s.add_development_dependency(%q<pry>, [">= 0"])
48
57
  s.add_development_dependency(%q<pry-nav>, [">= 0"])
49
58
  else
59
+ s.add_dependency(%q<activesupport>, [">= 0"])
50
60
  s.add_dependency(%q<rspec>, [">= 0"])
51
61
  s.add_dependency(%q<bundler>, [">= 0"])
52
62
  s.add_dependency(%q<jeweler>, [">= 0"])
@@ -54,6 +64,7 @@ Gem::Specification.new do |s|
54
64
  s.add_dependency(%q<pry-nav>, [">= 0"])
55
65
  end
56
66
  else
67
+ s.add_dependency(%q<activesupport>, [">= 0"])
57
68
  s.add_dependency(%q<rspec>, [">= 0"])
58
69
  s.add_dependency(%q<bundler>, [">= 0"])
59
70
  s.add_dependency(%q<jeweler>, [">= 0"])
data/moses.png ADDED
Binary file
@@ -0,0 +1,91 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'moses/application'
3
+ require 'pry'
4
+
5
+ describe Moses::Application do
6
+
7
+ describe "create" do
8
+
9
+ before do
10
+ @bin = File.expand_path(File.join("bin", "moses"))
11
+ @tmp_dir = Dir.mktmpdir
12
+ @app = Moses::Application.new @tmp_dir
13
+ end
14
+
15
+ after do
16
+ FileUtils.rm_rf @tmp_dir
17
+ end
18
+
19
+ describe "initialization" do
20
+ it "has default root_path" do
21
+ app = Moses::Application.new
22
+ expect(app.root_path).to eq(File.dirname(File.expand_path('..', __FILE__)))
23
+ end
24
+
25
+ it "can be initialized with a root_path" do
26
+ expect(@app.root_path).to eq(@tmp_dir)
27
+ end
28
+ end
29
+
30
+ describe "create" do
31
+ before do
32
+ @app.instance_variable_set(:@args, ['test_app'])
33
+ @app.create
34
+ end
35
+
36
+ it "makes a directory for the application" do
37
+ expect(File.directory?("#{@tmp_dir}/test_app")).to be_true
38
+ end
39
+
40
+ it "makes a HELP file" do
41
+ expect(File.file?("#{@tmp_dir}/test_app/HELP.md")).to be_true
42
+ expect(File.read("#{@tmp_dir}/test_app/HELP.md")).to eq("Todo: Add your own instructions")
43
+ end
44
+
45
+ it "makes a version file" do
46
+ expect(File.file?("#{@tmp_dir}/test_app/VERSION")).to be_true
47
+ expect(File.read("#{@tmp_dir}/test_app/VERSION")).to eq("0.0.0")
48
+ end
49
+
50
+ it "makes a bin directory" do
51
+ expect(File.directory?("#{@tmp_dir}/test_app/bin")).to be_true
52
+ end
53
+
54
+ it "makes an executable file" do
55
+ expect(File.file?("#{@tmp_dir}/test_app/bin/test_app")).to be_true
56
+ expect(File.executable?("#{@tmp_dir}/test_app/bin/test_app")).to be_true
57
+ expected_content = %Q{
58
+ #!/usr/bin/env ruby
59
+ $: << File.expand_path(File.join(File.dirname(__FILE__), "../lib"))
60
+ require 'test_app'
61
+ TestApp.new.run
62
+ }
63
+ expect(File.read("#{@tmp_dir}/test_app/bin/test_app")).to eq(expected_content)
64
+ end
65
+
66
+ it "makes a lib directory" do
67
+ expect(File.directory?("#{@tmp_dir}/test_app/lib")).to be_true
68
+ end
69
+
70
+ it "makes an applcation class file" do
71
+ expect(File.file?("#{@tmp_dir}/test_app/lib/test_app.rb")).to be_true
72
+ expected_content = %Q{
73
+ class TestApp
74
+ include Moses
75
+ end
76
+ }
77
+ expect(File.read("#{@tmp_dir}/test_app/lib/test_app.rb")).to eq(expected_content)
78
+ end
79
+
80
+ it "warns you if you forget the app name" do
81
+ app = Moses::Application.new
82
+ app.instance_variable_set(:@args, [])
83
+ app.output.stub(:puts)
84
+ app.output.should_receive(:puts).with('You need to name your application: moses create myapp')
85
+ app.create
86
+ end
87
+ end
88
+
89
+ end
90
+
91
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moses
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-13 00:00:00.000000000 Z
12
+ date: 2013-05-14 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
14
30
  - !ruby/object:Gem::Dependency
15
31
  name: rspec
16
32
  requirement: !ruby/object:Gem::Requirement
@@ -93,10 +109,12 @@ dependencies:
93
109
  version: '0'
94
110
  description: Moses is a simple command parser for writing command line applications
95
111
  email: daytonn@gmail.com
96
- executables: []
112
+ executables:
113
+ - moses
97
114
  extensions: []
98
115
  extra_rdoc_files:
99
116
  - LICENSE
117
+ - README.md
100
118
  files:
101
119
  - .DS_Store
102
120
  - .document
@@ -104,11 +122,17 @@ files:
104
122
  - .ruby-version
105
123
  - Gemfile
106
124
  - Gemfile.lock
125
+ - HELP.md
107
126
  - LICENSE
127
+ - README.md
108
128
  - Rakefile
109
129
  - VERSION
130
+ - bin/moses
110
131
  - lib/moses.rb
132
+ - lib/moses/application.rb
111
133
  - moses.gemspec
134
+ - moses.png
135
+ - spec/application_spec.rb
112
136
  - spec/moses_spec.rb
113
137
  - spec/spec_helper.rb
114
138
  - test.rb
@@ -127,7 +151,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
127
151
  version: '0'
128
152
  segments:
129
153
  - 0
130
- hash: -1441504447573939489
154
+ hash: 4282325133328940449
131
155
  required_rubygems_version: !ruby/object:Gem::Requirement
132
156
  none: false
133
157
  requirements: