applix 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Add dependencies to develop your gem here.
4
+ # Include everything needed to run rake, tests, features, etc.
5
+ group :development do
6
+ gem "rspec", "~> 2.3.0"
7
+ gem "bundler", "~> 1.0.0"
8
+ gem "jeweler", "~> 1.5.2"
9
+ gem "rcov", ">= 0"
10
+ gem "ZenTest", ">= 4.4.2"
11
+ end
data/Rakefile CHANGED
@@ -1,4 +1,12 @@
1
1
  require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
2
10
  require 'rake'
3
11
 
4
12
  begin
@@ -42,8 +50,9 @@ begin
42
50
  rescue LoadError
43
51
  puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
44
52
  end
53
+ Jeweler::RubygemsDotOrgTasks.new
45
54
 
46
-
55
+ require 'rspec/core'
47
56
  require "rspec/core/rake_task"
48
57
  namespace :test do
49
58
  desc "Run all specs."
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.3.0
@@ -0,0 +1,51 @@
1
+ module ApplixHash
2
+
3
+ module ClassMethods
4
+ # #from_argv builds hash from ARGV like argument vector according to
5
+ # following examples:
6
+ #
7
+ # '-f' --> { :f => true }
8
+ # '--flag' --> { :flag => true }
9
+ # '--flag:false' --> { :flag => false }
10
+ # '--flag=false' --> { :flag => 'false' }
11
+ # '--option=value' --> { :option => "value" }
12
+ # '--int=1' --> { :int => "1" }
13
+ # '--float=2.3' --> { :float => "2.3" }
14
+ # '--float:2.3' --> { :float => 2.3 }
15
+ # '--txt="foo bar"' --> { :txt => "foo bar" }
16
+ # '--txt:\'"foo bar"\'' --> { :txt => "foo bar" }
17
+ # '--txt:%w{foo bar}' --> { :txt => ["foo", "bar"] }
18
+ # '--now:Time.now' --> { :now => #<Date: 3588595/2,0,2299161> }
19
+ #
20
+ # remaining arguments(non flag/options) are inserted as [:args]. eg:
21
+ # Hash.from_argv %w(--foo --bar=loo 123 now)
22
+ # becomes
23
+ # { :foo => true, :bar => 'loo', :args => ["123", "now"] }
24
+ #
25
+ def from_argv argv, opts = {}
26
+ args, h = argv.clone, {}
27
+ while arg = args.first
28
+ key, val = ApplixHash.parse(arg)
29
+ break unless key
30
+ h[key] = val
31
+ args.shift
32
+ end
33
+ #[args, h]
34
+ h[:args] = args
35
+ h
36
+ end
37
+ end # ClassMethods
38
+
39
+ # parse single flag/option into a [key, value] tuple. returns nil on non
40
+ # option/flag arguments.
41
+ def self.parse(arg)
42
+ m = /^(-(\w)|--(\w\w+))(([=:])(.+))?$/.match(arg)
43
+ return [nil, arg] unless m # neither option nor flag -> straight arg
44
+ key = (m[2] || m[3]).to_sym
45
+ value = m[6][/(['"]?)(.*)\1$/,2] rescue true
46
+ value = eval(value) if m[5] == ':'
47
+ [key, value]
48
+ end
49
+ end
50
+
51
+ Hash.extend ApplixHash::ClassMethods
data/lib/applix.rb CHANGED
@@ -1,51 +1,74 @@
1
- module ApplixHash
2
-
3
- module ClassMethods
4
- # #from_argv builds hash from ARGV like argument vector according to
5
- # following examples:
6
- #
7
- # '-f' --> { :f => true }
8
- # '--flag' --> { :flag => true }
9
- # '--flag:false' --> { :flag => false }
10
- # '--flag=false' --> { :flag => 'false' }
11
- # '--option=value' --> { :option => "value" }
12
- # '--int=1' --> { :int => "1" }
13
- # '--float=2.3' --> { :float => "2.3" }
14
- # '--float:2.3' --> { :float => 2.3 }
15
- # '--txt="foo bar"' --> { :txt => "foo bar" }
16
- # '--txt:\'"foo bar"\'' --> { :txt => "foo bar" }
17
- # '--txt:%w{foo bar}' --> { :txt => ["foo", "bar"] }
18
- # '--now:Time.now' --> { :now => #<Date: 3588595/2,0,2299161> }
19
- #
20
- # remaining arguments(non flag/options) are inserted as [:args]. eg:
21
- # Hash.from_argv %w(--foo --bar=loo 123 now)
22
- # becomes
23
- # { :foo => true, :bar => 'loo', :args => ["123", "now"] }
24
- #
25
- def from_argv argv, opts = {}
26
- args, h = argv.clone, {}
27
- while arg = args.first
28
- key, val = ApplixHash.parse(arg)
29
- break unless key
30
- h[key] = val
31
- args.shift
32
- end
33
- #[args, h]
34
- h[:args] = args
35
- h
36
- end
37
- end # ClassMethods
38
-
39
- # parse single flag/option into a [key, value] tuple. returns nil on non
40
- # option/flag arguments.
41
- def self.parse(arg)
42
- m = /^(-(\w)|--(\w\w+))(([=:])(.+))?$/.match(arg)
43
- return [nil, arg] unless m # neither option nor flag -> straight arg
44
- key = (m[2] || m[3]).to_sym
45
- value = m[6][/(['"]?)(.*)\1$/,2] rescue true
46
- value = eval(value) if m[5] == ':'
47
- [key, value]
1
+ require 'applix/hash'
2
+
3
+ class Applix
4
+ def self.main argv, defaults = {}, &blk
5
+ app = Applix.new
6
+ app.instance_eval(&blk)
7
+ app.run(argv, defaults)
8
+ rescue => e
9
+ puts <<-EOT
10
+
11
+ ## #{e}
12
+
13
+ usage: #{__FILE__} <args...>
14
+
15
+ --- #{e.backtrace.join "\n "}
16
+ EOT
17
+ end
18
+
19
+ def run argv, defaults
20
+ options = (Hash.from_argv argv)
21
+ options = (defaults.merge options)
22
+ args = (options.delete :args)
23
+
24
+ # which task to run depends on first line argument..
25
+ (name = args.shift) or (raise "no task")
26
+ (task = tasks[name.to_sym]) or (raise "no such task: '#{name}'")
27
+ task[:code].call(*args, options)
28
+ end
29
+
30
+ private
31
+
32
+ def handle name, &blk
33
+ tasks[name.to_sym] = { :code => blk }
34
+ end
35
+
36
+ def tasks
37
+ @tasks ||= {}
48
38
  end
49
39
  end
50
40
 
51
- Hash.extend ApplixHash::ClassMethods
41
+ __END__
42
+ #
43
+ def main args, options = {}
44
+ options = (Defaults.merge options)
45
+ options[:date] = Date.parse(options[:date]) # up-type string date
46
+
47
+ action = args.shift or raise "no action"
48
+
49
+ # account is an command line arg but password is prompted, never have that in
50
+ # a config or on the command line!
51
+ #
52
+ username = args.shift # or raise "no username"
53
+ password = prompt_for_password
54
+
55
+ # which method to run depend on first command line argument..
56
+ send action, username, password, options
57
+ end
58
+
59
+ params = Hash.from_argv ARGV
60
+ begin
61
+ main params[:args], params
62
+ rescue => e
63
+ puts <<-EOT
64
+
65
+ ## #{e}
66
+
67
+ usage: #{__FILE__} <task> <username>
68
+
69
+ --- #{e.backtrace.join "\n "}
70
+ EOT
71
+ end
72
+
73
+ __END__
74
+
data/spec/applix_spec.rb CHANGED
@@ -1,10 +1,35 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "Applix" do
4
- #it "fails" do
5
- # fail "hey buddy, you should probably rename this file and start specing for real"
6
- #end
7
-
4
+
5
+ it 'should call actions by first argument names' do
6
+ argv = ['func']
7
+ Applix.main(argv) do
8
+ handle(:func) { :func_return }
9
+ end.should == :func_return
10
+ end
11
+
12
+ it 'should pass arguments to function' do
13
+ argv = ['func', 'p1', 'p2']
14
+ Applix.main(argv) do
15
+ handle(:func) { |*args, options| args }
16
+ end.should == %w{p1 p2}
17
+ end
18
+
19
+ it 'should pass emtpy options to function on default' do
20
+ argv = %w(func)
21
+ Applix.main(argv) do
22
+ handle(:func) { |*_, options| options }
23
+ end.should == {}
24
+ end
25
+
26
+ it 'should pass a processed options hash' do
27
+ argv = %w(-a --bar func)
28
+ Applix.main(argv) do
29
+ handle(:func) { |*_, options| options }
30
+ end.should == {:a => true, :bar => true}
31
+ end
32
+
8
33
  it "should parse the old unit test..." do
9
34
  # -f becomes { :f => true }
10
35
  # --flag becomes { :flag => true }
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: applix
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
5
4
  prerelease:
6
- segments:
7
- - 0
8
- - 2
9
- - 2
10
- version: 0.2.2
5
+ version: 0.3.0
11
6
  platform: ruby
12
7
  authors:
13
8
  - dirk luesebrink
@@ -15,55 +10,97 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2011-02-07 00:00:00 +01:00
13
+ date: 2011-08-14 00:00:00 +02:00
19
14
  default_executable:
20
15
  dependencies:
21
16
  - !ruby/object:Gem::Dependency
22
17
  name: rspec
23
- prerelease: false
24
18
  requirement: &id001 !ruby/object:Gem::Requirement
25
19
  none: false
26
20
  requirements:
27
- - - ">="
21
+ - - ~>
28
22
  - !ruby/object:Gem::Version
29
- hash: 3
30
- segments:
31
- - 2
32
- - 3
33
- - 0
34
23
  version: 2.3.0
35
24
  type: :development
25
+ prerelease: false
36
26
  version_requirements: *id001
37
27
  - !ruby/object:Gem::Dependency
38
- name: rcov
39
- prerelease: false
28
+ name: bundler
40
29
  requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: 1.0.0
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: jeweler
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.5.2
46
+ type: :development
47
+ prerelease: false
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: rcov
51
+ requirement: &id004 !ruby/object:Gem::Requirement
41
52
  none: false
42
53
  requirements:
43
54
  - - ">="
44
55
  - !ruby/object:Gem::Version
45
- hash: 3
46
- segments:
47
- - 0
48
56
  version: "0"
49
57
  type: :development
50
- version_requirements: *id002
58
+ prerelease: false
59
+ version_requirements: *id004
51
60
  - !ruby/object:Gem::Dependency
52
61
  name: ZenTest
62
+ requirement: &id005 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 4.4.2
68
+ type: :development
53
69
  prerelease: false
54
- requirement: &id003 !ruby/object:Gem::Requirement
70
+ version_requirements: *id005
71
+ - !ruby/object:Gem::Dependency
72
+ name: rspec
73
+ requirement: &id006 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 2.3.0
79
+ type: :development
80
+ prerelease: false
81
+ version_requirements: *id006
82
+ - !ruby/object:Gem::Dependency
83
+ name: rcov
84
+ requirement: &id007 !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: "0"
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: *id007
93
+ - !ruby/object:Gem::Dependency
94
+ name: ZenTest
95
+ requirement: &id008 !ruby/object:Gem::Requirement
55
96
  none: false
56
97
  requirements:
57
98
  - - ">="
58
99
  - !ruby/object:Gem::Version
59
- hash: 43
60
- segments:
61
- - 4
62
- - 4
63
- - 2
64
100
  version: 4.4.2
65
101
  type: :development
66
- version_requirements: *id003
102
+ prerelease: false
103
+ version_requirements: *id008
67
104
  description: " ApplixHash#from_argv builds hashes from ARGV like argument vectors\n according to following examples: \n \n '-f' --> { :f => true }\n '--flag' --> { :flag => true }\n '--flag:false' --> { :flag => false }\n '--flag=false' --> { :flag => 'false' }\n '--option=value' --> { :option => \"value\" }\n '--int=1' --> { :int => \"1\" }\n '--float=2.3' --> { :float => \"2.3\" }\n '--float:2.3' --> { :float => 2.3 }\n '--txt=\"foo bar\"' --> { :txt => \"foo bar\" }\n '--txt:'\"foo bar\"'' --> { :txt => \"foo bar\" }\n '--txt:%w{foo bar}' --> { :txt => [\"foo\", \"bar\"] }\n '--now:Time.now' --> { :now => #<Date: 3588595/2,0,2299161> }\n \n remaining arguments(non flag/options) are inserted as [:arguments,\n args], eg:\n Hash.from_argv %w(--foo --bar=loo 123 now)\n becomes \n { :foo => true, :bar => 'loo', :arguments => [\"123\", \"now\"] }\n \n"
68
105
  email: dirk@sebrink.de
69
106
  executables: []
@@ -77,11 +114,13 @@ files:
77
114
  - .autotest
78
115
  - .document
79
116
  - .rspec
117
+ - Gemfile
80
118
  - LICENSE
81
119
  - README.mkd
82
120
  - Rakefile
83
121
  - VERSION
84
122
  - lib/applix.rb
123
+ - lib/applix/hash.rb
85
124
  - lib/applix/oattr.rb
86
125
  - spec/applix_spec.rb
87
126
  - spec/oattr_spec.rb
@@ -100,7 +139,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
100
139
  requirements:
101
140
  - - ">="
102
141
  - !ruby/object:Gem::Version
103
- hash: 3
142
+ hash: 2866843717498845092
104
143
  segments:
105
144
  - 0
106
145
  version: "0"
@@ -109,14 +148,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
148
  requirements:
110
149
  - - ">="
111
150
  - !ruby/object:Gem::Version
112
- hash: 3
113
- segments:
114
- - 0
115
151
  version: "0"
116
152
  requirements: []
117
153
 
118
154
  rubyforge_project:
119
- rubygems_version: 1.4.2
155
+ rubygems_version: 1.6.2
120
156
  signing_key:
121
157
  specification_version: 3
122
158
  summary: build typed option hashed from command line arguments