applix 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 dirk luesebrink
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.
data/README.markdown ADDED
@@ -0,0 +1,40 @@
1
+ # applix
2
+
3
+ `Hash#from_argv` builds a hash from ARGV like argument vector according to
4
+ following examples:
5
+
6
+ '-f' --> { :f => true }
7
+ '--flag' --> { :flag => true }
8
+ '--flag:false' --> { :flag => false }
9
+ '--flag=false' --> { :flag => 'false' }
10
+ '--option=value' --> { :option => "value" }
11
+ '--int=1' --> { :int => "1" }
12
+ '--float=2.3' --> { :float => "2.3" }
13
+ '--float:2.3' --> { :float => 2.3 }
14
+ '--txt="foo bar"' --> { :txt => "foo bar" }
15
+ '--txt:\'"foo bar"\'' --> { :txt => "foo bar" }
16
+ '--txt:%w{foo bar}' --> { :txt => ["foo", "bar"] }
17
+ '--now:Time.now' --> { :now => #<Date: 3588595/2,0,2299161> }
18
+
19
+ Remaining arguments(non flag/options) are inserted as [:args], eg:
20
+
21
+ Hash.from_argv %w(--foo --bar=loo 123 now)
22
+
23
+ becomes:
24
+
25
+ { :foo => true, :bar => 'loo', :args => ["123", "now"] }
26
+
27
+ ## Note on Patches/Pull Requests
28
+
29
+ * Fork the project.
30
+ * Make your feature addition or bug fix.
31
+ * Add tests for it. This is important so I don't break it in a
32
+ future version unintentionally.
33
+ * Commit, do not mess with rakefile, version, or history.
34
+ (if you want to have your own version, that is fine but
35
+ bump version in a commit by itself I can ignore when I pull)
36
+ * Send me a pull request. Bonus points for topic branches.
37
+
38
+ == Copyright
39
+
40
+ Copyright (c) 2009 dirk luesebrink. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,71 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "applix"
8
+ gem.summary = %Q{build typed option hashed from command line arguments}
9
+ gem.description = <<-TXT.gsub /\n\n/, ''
10
+ ApplixHash#from_argv builds hashes from ARGV like argument vectors
11
+ according to following examples:
12
+
13
+ '-f' --> { :f => true }
14
+ '--flag' --> { :flag => true }
15
+ '--flag:false' --> { :flag => false }
16
+ '--flag=false' --> { :flag => 'false' }
17
+ '--option=value' --> { :option => "value" }
18
+ '--int=1' --> { :int => "1" }
19
+ '--float=2.3' --> { :float => "2.3" }
20
+ '--float:2.3' --> { :float => 2.3 }
21
+ '--txt="foo bar"' --> { :txt => "foo bar" }
22
+ '--txt:\'"foo bar"\'' --> { :txt => "foo bar" }
23
+ '--txt:%w{foo bar}' --> { :txt => ["foo", "bar"] }
24
+ '--now:Time.now' --> { :now => #<Date: 3588595/2,0,2299161> }
25
+
26
+ remaining arguments(non flag/options) are inserted as [:arguments,
27
+ args], eg:
28
+ Hash.from_argv %w(--foo --bar=loo 123 now)
29
+ becomes
30
+ { :foo => true, :bar => 'loo', :arguments => ["123", "now"] }
31
+
32
+ TXT
33
+ gem.email = "dirk@sebrink.de"
34
+ gem.homepage = "http://github.com/crux/applix"
35
+ gem.authors = ["dirk luesebrink"]
36
+ gem.add_development_dependency "rspec"
37
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
38
+ end
39
+ rescue LoadError
40
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
41
+ end
42
+
43
+ require 'spec/rake/spectask'
44
+ Spec::Rake::SpecTask.new(:spec) do |spec|
45
+ spec.libs << 'lib' << 'spec'
46
+ spec.spec_files = FileList['spec/**/*_spec.rb']
47
+ end
48
+
49
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
50
+ spec.libs << 'lib' << 'spec'
51
+ spec.pattern = 'spec/**/*_spec.rb'
52
+ spec.rcov = true
53
+ end
54
+
55
+ task :spec => :check_dependencies
56
+
57
+ task :default => :spec
58
+
59
+ require 'rake/rdoctask'
60
+ Rake::RDocTask.new do |rdoc|
61
+ if File.exist?('VERSION')
62
+ version = File.read('VERSION')
63
+ else
64
+ version = ""
65
+ end
66
+
67
+ rdoc.rdoc_dir = 'rdoc'
68
+ rdoc.title = "applix #{version}"
69
+ rdoc.rdoc_files.include('README*')
70
+ rdoc.rdoc_files.include('lib/**/*.rb')
71
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
data/applix.gemspec ADDED
@@ -0,0 +1,75 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
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 = %q{applix}
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["dirk luesebrink"]
12
+ s.date = %q{2009-11-14}
13
+ s.description = %q{ ApplixHash#from_argv builds hashes from ARGV like argument vectors
14
+ according to following examples:
15
+
16
+ '-f' --> { :f => true }
17
+ '--flag' --> { :flag => true }
18
+ '--flag:false' --> { :flag => false }
19
+ '--flag=false' --> { :flag => 'false' }
20
+ '--option=value' --> { :option => "value" }
21
+ '--int=1' --> { :int => "1" }
22
+ '--float=2.3' --> { :float => "2.3" }
23
+ '--float:2.3' --> { :float => 2.3 }
24
+ '--txt="foo bar"' --> { :txt => "foo bar" }
25
+ '--txt:'"foo bar"'' --> { :txt => "foo bar" }
26
+ '--txt:%w{foo bar}' --> { :txt => ["foo", "bar"] }
27
+ '--now:Time.now' --> { :now => #<Date: 3588595/2,0,2299161> }
28
+
29
+ remaining arguments(non flag/options) are inserted as [:arguments,
30
+ args], eg:
31
+ Hash.from_argv %w(--foo --bar=loo 123 now)
32
+ becomes
33
+ { :foo => true, :bar => 'loo', :arguments => ["123", "now"] }
34
+
35
+ }
36
+ s.email = %q{dirk@sebrink.de}
37
+ s.extra_rdoc_files = [
38
+ "LICENSE",
39
+ "README.markdown"
40
+ ]
41
+ s.files = [
42
+ ".document",
43
+ ".gitignore",
44
+ "LICENSE",
45
+ "README.markdown",
46
+ "Rakefile",
47
+ "VERSION",
48
+ "applix.gemspec",
49
+ "lib/applix.rb",
50
+ "spec/applix_spec.rb",
51
+ "spec/spec_helper.rb"
52
+ ]
53
+ s.homepage = %q{http://github.com/crux/applix}
54
+ s.rdoc_options = ["--charset=UTF-8"]
55
+ s.require_paths = ["lib"]
56
+ s.rubygems_version = %q{1.3.4}
57
+ s.summary = %q{build typed option hashed from command line arguments}
58
+ s.test_files = [
59
+ "spec/applix_spec.rb",
60
+ "spec/spec_helper.rb"
61
+ ]
62
+
63
+ if s.respond_to? :specification_version then
64
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
65
+ s.specification_version = 3
66
+
67
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
68
+ s.add_development_dependency(%q<rspec>, [">= 0"])
69
+ else
70
+ s.add_dependency(%q<rspec>, [">= 0"])
71
+ end
72
+ else
73
+ s.add_dependency(%q<rspec>, [">= 0"])
74
+ end
75
+ end
data/lib/applix.rb ADDED
@@ -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
@@ -0,0 +1,104 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
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
+ end
8
+
9
+
10
+ __END__
11
+
12
+ port this from unit to rspec...
13
+
14
+ #!/usr/bin/env ruby
15
+
16
+ require 'test/unit'
17
+ require 'pp'
18
+
19
+ #require 'hash_from_argv'
20
+ #H = Hash::FromArgvHelperNamespaceToAvoidPolution
21
+
22
+ require 'applix-hash'
23
+ H = ApplixHash
24
+
25
+ class HashFromArgvTest < Test::Unit::TestCase
26
+
27
+ def test_applix_hash_parse
28
+ # -f becomes { :f => true }
29
+ # --flag becomes { :flag => true }
30
+ assert_equal [:f, true], H.parse("-f")
31
+ assert_equal [:flag, true], H.parse("--flag")
32
+ # --flag:false becomes { :flag => false }
33
+ assert_equal [:flag, false], H.parse("--flag:false")
34
+
35
+ # --option=value becomes { :option => "value" }
36
+ assert_equal [:opt, "val"], H.parse("--opt=val")
37
+
38
+ # --int=1 becomes { :int => "1" }
39
+ # --float=2.3 becomes { :float => "2.3" }
40
+ # --float:2.3 becomes { :float => 2.3 }
41
+ # -f:1.234 becomes { :f => 1.234 }
42
+ assert_equal [:int, "1"], H.parse("--int=1")
43
+ assert_equal [:float, "2.3"], H.parse("--float=2.3")
44
+ assert_equal [:float, 2.3], H.parse("--float:2.3")
45
+ assert_equal [:f, 1.234], H.parse("-f:1.234")
46
+
47
+ # --txt="foo bar" becomes { :txt => "foo bar" }
48
+ # --txt:'"foo bar"' becomes { :txt => "foo bar" }
49
+ # --txt:%w{foo bar} becomes { :txt => ["foo", "bar"] }
50
+ assert_equal [:txt, "foo bar"], H.parse('--txt="foo bar"')
51
+ assert_equal [:txt, "foo bar"], H.parse(%q{--txt:'"foo bar"'})
52
+ assert_equal [:txt, ["foo", "bar"]], H.parse(%q{--txt:'%w{foo bar}'})
53
+
54
+ # --now:Time.now becomes { :now => Mon Jul 09 01:30:21 0200 2007 }
55
+ dt = Time.now - H.parse("--now:Time.now")[1]
56
+ assert dt < 0.02
57
+ end
58
+
59
+ # XXX this is hacking the new Hash.from_argv interface into the old
60
+ # dissect signature to make proper reuse of the existing unit tests
61
+ def dissect(argv)
62
+ h = Hash.from_argv(argv)
63
+ [h[:arguments], h.delete_if { |k, _| k == :arguments }]
64
+ end
65
+
66
+ def test_applix_dissect_flags
67
+ assert_equal [[], {:foo=>true}], dissect(%w{--foo})
68
+ end
69
+
70
+ def test_applix_colon_option
71
+ _, opts = dissect ["-a:%w{a b c}"]
72
+ assert_equal({:a=>['a', 'b', 'c']}, opts)
73
+
74
+ _, opts = dissect ["-b:(1..10)"]
75
+ assert_equal({:b=>(1..10)}, opts)
76
+
77
+ _, opts = dissect ["-c:Time.now"]
78
+ assert opts[:c] < Time.now
79
+ assert (Time.now - opts[:c]) < 0.01
80
+ end
81
+
82
+ def test_applix_dissect_options
83
+ _, opts = dissect %w{-v=3.15}
84
+ assert_equal({:v=>"3.15"}, opts)
85
+ _, opts = dissect %w{-v:3.15}
86
+ assert_equal({:v=>3.15}, opts)
87
+
88
+ av, opts = dissect %w{--een --twee=3 -v:3.15}
89
+ assert_equal [], av
90
+ assert_equal({:een => true, :twee => "3", :v => 3.15}, opts)
91
+ end
92
+
93
+ def test_applix_dissect_without_options
94
+ #assert_equal [[], {}], dissect
95
+ #assert_equal [ARGV, {}], dissect
96
+ %w{een twee 3 3.15 foo-bar}.inject([]) do |memo, arg|
97
+ memo << arg
98
+ av, opts = dissect(memo)
99
+ assert_equal memo, av
100
+ assert_equal({}, opts)
101
+ memo
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'applix'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: applix
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - dirk luesebrink
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-14 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ 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"
26
+ email: dirk@sebrink.de
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.markdown
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.markdown
39
+ - Rakefile
40
+ - VERSION
41
+ - applix.gemspec
42
+ - lib/applix.rb
43
+ - spec/applix_spec.rb
44
+ - spec/spec_helper.rb
45
+ has_rdoc: true
46
+ homepage: http://github.com/crux/applix
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --charset=UTF-8
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.3.4
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: build typed option hashed from command line arguments
73
+ test_files:
74
+ - spec/applix_spec.rb
75
+ - spec/spec_helper.rb