arguvia 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 649a7cfe18e7aea4094964e4a5974b8983dc43f3
4
+ data.tar.gz: 8e363991a977f8d560a649c9999dee1aeae2e14e
5
+ SHA512:
6
+ metadata.gz: 25474e2f5bbb4decb95fe282a85594981257b69fbf939239aba0b3f17598a7d115cc6024ac14ca9c602737fda1c89c7b51052903ba2c8a9525db44f5c0453771
7
+ data.tar.gz: 10ec1e19d3909014bae42be9ca82bd10b72b4fa3c569b5e6227c425453f769961b34e1ac9bb941fb7fa6bc1ce4f5e1ca798ef5f407681a843b38818c36d87f55
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2013 Joseph (fluorine@github)
2
+
3
+ Permission is hereby granted, free of charge, to any
4
+ person obtaining a copy of this software and associated
5
+ documentation files (the "Software"), to deal in the
6
+ Software without restriction, including without limitation
7
+ the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the
9
+ Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice
13
+ shall be included in all copies or substantial portions of
14
+ the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17
+ KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18
+ WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
19
+ PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
20
+ OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,62 @@
1
+ Arguvia v0.1.0
2
+ ==============
3
+ This gem contains a function-like argument parser
4
+ for command-line applications.
5
+
6
+ This is an example of a command-line application
7
+ that uses function-like arguments:
8
+
9
+ dictionary.rb words(10) open-file(words.txt) Singular
10
+
11
+
12
+ Description
13
+ -----------
14
+ Arguvia gem allows any Ruby application to use function-like
15
+ arguments for its command-line interface.
16
+
17
+ This gem contains the `Arguvia` class, which parses the given
18
+ list of arguments to map them to class members and indeternal
19
+ Ruby values.
20
+
21
+
22
+ Using the Arguvia class
23
+ -----------------------
24
+ Let's take the introductory example to show how
25
+ the `Arguvia` class works:
26
+
27
+ dictionary.rb words(10) open-file(words.txt) Singular
28
+
29
+ The `dictionary.rb` hypothetic script uses function-like
30
+ command-line arguments through the `arguvia` gem.
31
+
32
+ The `Arguvia` class generates the class members for
33
+ the given command-line arguments.
34
+
35
+ require 'arguvia'
36
+ x = Arguvia.new # It equals to 'Arguvia.new(ARGV)'
37
+ x.words # => 10
38
+ x.open_file # => "words.txt"
39
+ x.singular # => true
40
+ x.undefined # => false
41
+
42
+ `Arguvia` class uses the dynamic and metaprogramming
43
+ features of Ruby to generate an object, which members maps
44
+ the given arguments.
45
+
46
+ Examples of valid arguments
47
+ ----------------------------------
48
+ |Argument |Identifier | Value |
49
+ |:---------:|:---------:|:------:|
50
+ |foo |`x.foo` |true |
51
+ |--etc |`x._etc ` |true |
52
+ |-h |`x._h` |true |
53
+ | |`x.baz` |nil |
54
+ |func(1) |`x.func` |1 |
55
+ |dir(etc) |`x.dir` |"etc" |
56
+ |xs(1, 2, 3)|`x.xs` |[1,2,3] |
57
+ |pair(a, 7) |`x.pair` |["a", 7]|
58
+
59
+ To do
60
+ -----
61
+ - Support for special symbols.
62
+ - Support for literal strings (allow internal spaces)
@@ -0,0 +1,58 @@
1
+ class Arguvia
2
+ def initialize(args = ARGV)
3
+ @args = {}
4
+
5
+ # Add bindings
6
+ args.each do |arg|
7
+ case arg
8
+ when /^([\w\-]+)$/
9
+ @args[__parse_key(arg)] = true
10
+ when /^([\w\-]+)\((.*)\)$/
11
+ @args[__parse_key($1)] = __parse_value($2)
12
+ else
13
+ raise ArgumentError
14
+ end
15
+ end
16
+ end
17
+
18
+ # Generate ghost methods, if requiered
19
+ def method_missing(name, *args)
20
+ super unless args.length == 0
21
+ @args[name]
22
+ end
23
+
24
+ # Add responding methods
25
+ def respond_to?(name)
26
+ return true if @args.keys.include? name
27
+ super(name)
28
+ end
29
+
30
+ #
31
+ # Internal methods
32
+ #
33
+
34
+ private
35
+
36
+ # Parse key type
37
+ def __parse_key(str)
38
+ return str.gsub(/-+/, "_").downcase.to_sym
39
+ end
40
+
41
+ # Parse value type
42
+ def __parse_value(str)
43
+ case str
44
+ when /,/
45
+ # Split and evaluate recursively
46
+ # if multiple arguments.
47
+ str.split(/,/).map do |item|
48
+ __parse_value(item.strip)
49
+ end
50
+ when /-?\d+\.\d+/
51
+ str.to_f
52
+ when /-?\d+/
53
+ str.to_i
54
+ else
55
+ str
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module Arguvia
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,71 @@
1
+ require 'arguvia'
2
+ require 'test/unit'
3
+
4
+ class TestArguvia < Test::Unit::TestCase
5
+ def setup
6
+ # Simple boolean arguments
7
+ @simple_strs = ["hola", "mundo", "vi-va", "--foo", "rivendel"]
8
+ @simple_args = Arguvia.new(@simple_strs)
9
+
10
+ # Function-like arguments
11
+ @func_strs = ["hola(mundo)", "integer(12)", "float(12.345)",
12
+ "multi(1, 2, 3)", "ma-ny(a, ab, cde)"]
13
+ @func_res = {hola: "mundo", integer: 12,
14
+ float: 12.345, multi: [1, 2, 3],
15
+ ma_ny: ["a", "ab", "cde"]}
16
+ @func_args = Arguvia.new(@func_strs)
17
+
18
+ # Complex arguments
19
+ @complex_strs = ["hola-mundo(saludo, despedida)", "--help",
20
+ "numbers(1, 1.23, -1, -3.245)",
21
+ "--file(/usr/hi/hola.txt)", "--PATH(ruby.exe)"]
22
+ @complex_res = {hola_mundo: ["saludo", "despedida"], _help: true,
23
+ numbers: [1, 1.23, -1, -3.245], _file: "/usr/hi/hola.txt",
24
+ _path: "ruby.exe"}
25
+ @complex_args = Arguvia.new(@complex_strs)
26
+ end
27
+
28
+ #
29
+ # Test simple arguments's method respond_to?
30
+ #
31
+
32
+ def test_false_respond_to
33
+ false_strs = ["a", "bar", "gaga", "etc", "demons"]
34
+ false_strs.each do |item|
35
+ assert_equal(@simple_args.respond_to?(item.to_sym), false)
36
+ end
37
+ end
38
+
39
+ def test_true_respond_to
40
+ @simple_strs.each do |item|
41
+ assert(@simple_args.respond_to? item.gsub(/-+/, "_").to_sym)
42
+ end
43
+ end
44
+
45
+ #
46
+ # Test simple arguments's generated members
47
+ #
48
+ def test_simple_arguvia_members
49
+ @simple_strs.each do |name|
50
+ assert(eval("@simple_args.#{name.gsub(/-+/, "_")}"))
51
+ end
52
+ end
53
+
54
+ #
55
+ # Test function-like argumens
56
+ #
57
+ def test_function_like_arguments
58
+ @func_res.each do |key, value|
59
+ assert_equal(eval("@func_args.#{key}"), value)
60
+ end
61
+ end
62
+
63
+ #
64
+ # Test complex function-like argumens
65
+ #
66
+ def test_complex_arguments
67
+ @complex_res.each do |key, value|
68
+ assert_equal(eval("@complex_args.#{key}"), value)
69
+ end
70
+ end
71
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: arguvia
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joseph
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This script allows applications to parse and use command-line arguments
14
+ in a function-like fashion. For example, 'dict words(10) open-file(words.txt)'
15
+ email: fluorine@github.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/arguvia.rb
21
+ - lib/arguvia/version.rb
22
+ - README.md
23
+ - LICENSE.txt
24
+ - test/test_arguvia.rb
25
+ homepage: https://github.com/fluorine/Arguvia
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.0.3
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: A function-like argument parser for command-line applications.
49
+ test_files: []