wojtekmach-cli 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,3 +1,7 @@
1
- *.sw*
2
- pkg/
3
- *.gemspec
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ .yardoc
5
+ /pkg
6
+ /doc
7
+ *.*.sw*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format=doc
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ -
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,17 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2 do
5
+ watch(%r{^spec/.+_spec\.rb})
6
+ watch(%r{^lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ # Rails example
10
+ watch('spec/spec_helper.rb') { "spec" }
11
+ watch('config/routes.rb') { "spec/routing" }
12
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
13
+ watch(%r{^spec/.+_spec\.rb})
14
+ watch(%r{^app/(.+)\.rb}) { |m| "spec/#{m[1]}_spec.rb" }
15
+ watch(%r{^lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" }
16
+ watch(%r{^app/controllers/(.+)_(controller)\.rb}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
17
+ end
data/LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2011 Wojciech Mach
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to
5
+ deal in the Software without restriction, including without limitation the
6
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
+ sell copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
+ THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # CLI
2
+
3
+ A CLI app framework
4
+
5
+ ## Setup
6
+
7
+ git clone /path/to/cli.git
8
+ cd cli
9
+ rake install
10
+
11
+ ## Example
12
+
13
+ Let's say you have a file `hello.rb`:
14
+
15
+ #!/usr/bin/env ruby
16
+ require "rubygems"
17
+ require "cli"
18
+
19
+ CLI.app do
20
+ name "hello"
21
+ version "0.0.1"
22
+
23
+ option "-m", "--message MSG" do |msg|
24
+ options[:msg] = msg
25
+ end
26
+
27
+ action "hi" do
28
+ puts "Hi, World!"
29
+ end
30
+
31
+ default do
32
+ str = options[:msg] || "Hello"
33
+ puts "#{str}, World!"
34
+ end
35
+ end
36
+
37
+ You can now use it like this:
38
+
39
+ ruby hello.rb # => Hello, World!
40
+ ruby hello.rb hi # => Hi, World!
41
+ ruby hello.rb -m Bye # => Bye, World!
42
+
43
+ ## Licence
44
+
45
+ CLI is Copyright (c) 2011 Wojciech Mach and distributed under the MIT license. See the LICENCE file for more info.
data/Rakefile CHANGED
@@ -1,18 +1,3 @@
1
- require 'rubygems'
2
- require 'rake'
3
-
4
- begin
5
- require 'jeweler'
6
- Jeweler::Tasks.new do |gemspec|
7
- gemspec.name = "wojtekmach-cli"
8
- gemspec.summary = "Create simple CLI apps"
9
- gemspec.description = "Create simple CLI apps"
10
- gemspec.email = "wojtekmach1@gmail.com"
11
- gemspec.authors = ["Wojtek Mach"]
12
- end
13
- Jeweler::GemcutterTasks.new
14
- rescue LoadError
15
- puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
16
- end
17
-
18
- Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
1
+ require 'bundler'
2
+
3
+ Bundler::GemHelper.install_tasks
data/cli.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "cli/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "wojtekmach-cli"
7
+ s.version = CLI::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Wojciech Mach"]
10
+ s.email = ["wojtekmach1@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = "cli-#{s.version}"
13
+ s.description = %q{CLI apps framework.}
14
+
15
+ s.rubyforge_project = "cli"
16
+
17
+ s.add_development_dependency "rspec"
18
+ s.add_development_dependency "guard"
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
+ s.require_paths = ["lib"]
24
+ end
data/lib/cli/app.rb ADDED
@@ -0,0 +1,92 @@
1
+ require "optparse"
2
+
3
+ module CLI
4
+ # An App
5
+ class App
6
+ attr_accessor :binary, :name, :version
7
+ attr_reader :proxy, :args, :opts, :options
8
+
9
+ def initialize(binary, &block)
10
+ @binary = binary
11
+ @name = binary
12
+ @actions = {}
13
+ @proxy = Proxy.new(self)
14
+ @args = []
15
+ @opts = OptionParser.new
16
+ @options = {}
17
+ proxy.instance_eval &block
18
+ end
19
+
20
+ # Hash of action_name => block to be executed
21
+ def actions
22
+ @actions
23
+ end
24
+
25
+ # Defines an action
26
+ def action(name, &block)
27
+ @actions[name] = block
28
+ end
29
+
30
+ # Runs an action
31
+ def run(action_name)
32
+ block = @actions[action_name]
33
+
34
+ unless block
35
+ raise ArgumentError, "action '#{action_name}' not found"
36
+ end
37
+
38
+ instance_eval &block
39
+ end
40
+
41
+ # Parses the arguments (with OptionParser) and runs
42
+ # specified or default action.
43
+ def run!(args)
44
+ @opts.parse!(args)
45
+ @args = args
46
+
47
+ begin
48
+ if args == []
49
+ run "default"
50
+ else
51
+ run args[0]
52
+ end
53
+ rescue => e
54
+ puts e.message.capitalize
55
+ end
56
+ end
57
+ end
58
+
59
+ # Proxy class is responsible for cool DSL inside CLI.app do ... end block
60
+ class Proxy
61
+ attr_reader :app
62
+
63
+ def initialize(app)
64
+ @app = app
65
+ end
66
+
67
+ # Sets the App name
68
+ def name(val)
69
+ app.name = val
70
+ end
71
+
72
+ # Sets the App version
73
+ def version(val)
74
+ app.version = val
75
+ end
76
+
77
+ # Defines the default action
78
+ def default(&block)
79
+ app.action("default", &block)
80
+ end
81
+
82
+ # Define option for OptionParser
83
+ def option(*args, &block)
84
+ app.opts.on(*args, &block)
85
+ end
86
+
87
+ # Delegates to App
88
+ def method_missing(name, *attrs, &block)
89
+ app.send(name, *attrs, &block)
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,4 @@
1
+ module CLI
2
+ # Current CLI version
3
+ VERSION = "0.1.0"
4
+ end
data/lib/cli.rb CHANGED
@@ -1,29 +1,10 @@
1
- require 'optparse'
1
+ require "cli/app"
2
2
 
3
+ # A CLI app framework
3
4
  module CLI
5
+ # Create a new App object with $0 as the binary name and runs it with ARGV
4
6
  def self.app(&block)
5
- App.new(ARGV, &block)
6
- end
7
-
8
- class App
9
- attr_reader :argv, :opts
10
-
11
- def initialize(argv, &block)
12
- @argv = argv
13
- instance_eval(&block)
14
- end
15
-
16
- def options(&block)
17
- @opts = OptionParser.new
18
- instance_eval(&block)
19
- @opts.parse!
20
- end
21
-
22
- def run(&block)
23
- instance_eval(&block)
24
- end
25
-
26
- def name(value) @name = value end
27
- def version(value) @version = value end
7
+ @app = App.new($0, &block)
8
+ @app.run!(ARGV)
28
9
  end
29
10
  end
@@ -0,0 +1,82 @@
1
+ require "spec_helper"
2
+
3
+ module CLI
4
+ describe App do
5
+ before do
6
+ @app = App.new("hello") do
7
+ name "Hello"
8
+ version "0.0.1"
9
+
10
+ option "-m", "--message MESSAGE" do |msg|
11
+ options[:msg] = msg
12
+ end
13
+
14
+ action "hi" do
15
+ puts "Hi!"
16
+ end
17
+
18
+ default do
19
+ puts options[:msg] || "Hello, World!"
20
+ end
21
+ end
22
+ end
23
+
24
+ it "has a binary name" do
25
+ @app.binary.should eq("hello")
26
+ end
27
+
28
+ it "has a name" do
29
+ @app.name.should eq("Hello")
30
+ end
31
+
32
+ it "has a version" do
33
+ @app.version.should eq("0.0.1")
34
+ end
35
+
36
+ it "runs an action" do
37
+ $stdout.should_receive(:puts).with("Hi!")
38
+ @app.run("hi")
39
+ end
40
+
41
+ it "fails when action is not found" do
42
+ lambda do
43
+ @app.run("bad")
44
+ end.should raise_error(ArgumentError, "action 'bad' not found")
45
+ end
46
+
47
+ it "runs the default action" do
48
+ $stdout.should_receive(:puts).with("Hello, World!")
49
+ @app.run("default")
50
+ end
51
+
52
+ describe "#run!" do
53
+ describe "when arguments are empty" do
54
+ it "runs the default action" do
55
+ @app.should_receive(:run).with("default")
56
+ @app.run!([])
57
+ end
58
+ end
59
+
60
+ describe "when 1st argument is a valid action name" do
61
+ it "runs the action" do
62
+ @app.should_receive(:run).with("hi")
63
+ @app.run!(%w( hi ))
64
+ end
65
+ end
66
+
67
+ describe "when 1st argument is an invalid action name" do
68
+ it "prints error message" do
69
+ $stdout.should_receive(:puts).with("Action 'bad' not found")
70
+ @app.run!(%w( bad ))
71
+ end
72
+ end
73
+
74
+ describe "with a valid option" do
75
+ it "sets the option and runs the default action" do
76
+ $stdout.should_receive(:puts).with("Goodmorning!")
77
+ @app.run!(%w( -m Goodmorning! ))
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,2 @@
1
+ $:.unshift File.expand_path("../lib", __FILE__)
2
+ require "cli"
metadata CHANGED
@@ -1,25 +1,42 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wojtekmach-cli
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 1
9
- version: 0.0.1
4
+ prerelease:
5
+ version: 0.1.0
10
6
  platform: ruby
11
7
  authors:
12
- - Wojtek Mach
8
+ - Wojciech Mach
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
12
 
17
- date: 2010-09-15 00:00:00 +02:00
18
- default_executable:
19
- dependencies: []
20
-
21
- description: Create simple CLI apps
22
- email: wojtekmach1@gmail.com
13
+ date: 2011-06-11 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :development
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: guard
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :development
36
+ version_requirements: *id002
37
+ description: CLI apps framework.
38
+ email:
39
+ - wojtekmach1@gmail.com
23
40
  executables: []
24
41
 
25
42
  extensions: []
@@ -28,17 +45,25 @@ extra_rdoc_files: []
28
45
 
29
46
  files:
30
47
  - .gitignore
48
+ - .rspec
49
+ - .yardopts
50
+ - Gemfile
51
+ - Guardfile
52
+ - LICENSE
53
+ - README.md
31
54
  - Rakefile
32
- - VERSION
33
- - examples/rcat
55
+ - cli.gemspec
34
56
  - lib/cli.rb
35
- has_rdoc: true
36
- homepage:
57
+ - lib/cli/app.rb
58
+ - lib/cli/version.rb
59
+ - spec/cli/app_spec.rb
60
+ - spec/spec_helper.rb
61
+ homepage: ""
37
62
  licenses: []
38
63
 
39
64
  post_install_message:
40
- rdoc_options:
41
- - --charset=UTF-8
65
+ rdoc_options: []
66
+
42
67
  require_paths:
43
68
  - lib
44
69
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -46,23 +71,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
46
71
  requirements:
47
72
  - - ">="
48
73
  - !ruby/object:Gem::Version
49
- segments:
50
- - 0
51
74
  version: "0"
52
75
  required_rubygems_version: !ruby/object:Gem::Requirement
53
76
  none: false
54
77
  requirements:
55
78
  - - ">="
56
79
  - !ruby/object:Gem::Version
57
- segments:
58
- - 0
59
80
  version: "0"
60
81
  requirements: []
61
82
 
62
- rubyforge_project:
63
- rubygems_version: 1.3.7
83
+ rubyforge_project: cli
84
+ rubygems_version: 1.8.5
64
85
  signing_key:
65
86
  specification_version: 3
66
- summary: Create simple CLI apps
67
- test_files: []
68
-
87
+ summary: cli-0.1.0
88
+ test_files:
89
+ - spec/cli/app_spec.rb
90
+ - spec/spec_helper.rb
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.0.1
data/examples/rcat DELETED
@@ -1,20 +0,0 @@
1
- #!/usr/bin/env ruby
2
- $: << File.dirname(__FILE__) + "/../lib"
3
-
4
- require 'cli'
5
-
6
- CLI.app do
7
- name "rcat"
8
- version "0.0.1"
9
-
10
- options do
11
- opts.on("-h", "--help") do
12
- puts "rcat 0.0.1. See cat(1)"
13
- exit
14
- end
15
- end
16
-
17
- run do
18
- puts ARGF.read
19
- end
20
- end