cli-parser 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f46baa494ac45ec68b037ae8575060c88efc6dcb
4
+ data.tar.gz: 8a534b47ddc3bb831f43359b68bd5c1ff437dbe3
5
+ SHA512:
6
+ metadata.gz: 9baf45f4a0936b4bd57829106f65e58033147b64b42433c88ea6d0e9a087c2d262bc790d5a83e0f118f153c69cafd10466f511299946776754491aff9cbae47f
7
+ data.tar.gz: 8077f765c9a7f6480790cc98a46b09affaff860ce003ea54a78770a71c5bec75a141a0174cfa4d3d491efe03c596ee43fbba34b421abe7f99bd2e5215076f6a0
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Taco Jan Osinga
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the 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 shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/lib/cli-parser.rb ADDED
@@ -0,0 +1,27 @@
1
+ module CliParser
2
+ extend self
3
+
4
+ def parse(flags = [], options = [], cmd = nil)
5
+ cmd = ARGV.map{ | x | x.index(/\s/) ? "\"#{x}\"" : x}.join(' ') if cmd.to_s.empty?
6
+ result_args = []
7
+ result_opts = {}
8
+
9
+ keywords = %w("[^"]*" [^\s-][^\s]+)
10
+ keywords << flags.collect { | f | Regexp.quote(f) }.join('|')
11
+ keywords << options.collect { | o | Regexp.quote(o) + '\s+([^\s"]+|"[^"]*")+' }.join('|')
12
+ regex = Regexp.new('(' + keywords.compact.join('|') + ')')
13
+
14
+ cmd.scan(regex).each { | a |
15
+ if a[0].start_with?('-')
16
+ k, v = a[0].split(' ', 2)
17
+ v = true if flags.include?(k)
18
+ v = v.to_s.gsub(/^\"/, '').sub(/\"$/, '') if options.include?(k)
19
+ result_opts[k] = v
20
+ elsif !a[0].to_s.empty?
21
+ result_args.push(a[0])
22
+ end
23
+ }
24
+ return [result_args, result_opts]
25
+ end
26
+
27
+ end
@@ -0,0 +1,24 @@
1
+ require 'test/unit'
2
+ require '../lib/cli-parser'
3
+
4
+ module CliParserTest
5
+
6
+ class CliParserTest < Test::Unit::TestCase
7
+
8
+ #noinspection RubyStringKeysInHashInspection
9
+ def test_parse
10
+ cmd = '-w arg1 -o Test arg2 -t "Hello World" arg3'
11
+
12
+ args, options = CliParser.parse(%w(-w), %w(-o -t), cmd)
13
+ assert_equal(%w(arg1 arg2 arg3), args)
14
+ assert_equal({ '-w' => true, '-o' => 'Test', '-t' => 'Hello World' }, options)
15
+
16
+ cmd = 'test1 test2 test3'
17
+ args, options = CliParser.parse([], [], cmd)
18
+ assert_equal(%w(test1 test2 test3), args)
19
+ assert_equal({}, options)
20
+ end
21
+
22
+ end
23
+
24
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cli-parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Taco Jan Osinga
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A command-line parser. Extracts arguments, switches and options.
14
+ email: info@osingasoftware.nl
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - LICENSE
20
+ - lib/cli-parser.rb
21
+ - test/test-cli-parser.rb
22
+ homepage: https://github.com/tjosinga/cli-parser
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.2.2
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: CLI Parser
46
+ test_files:
47
+ - test/test-cli-parser.rb