clap 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2010 Michel Martens
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 deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ 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 THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,76 @@
1
+ Clap
2
+ ====
3
+
4
+ Command line argument parsing for simple applications.
5
+
6
+ Description
7
+ -----------
8
+
9
+ Clap is a small library that can be bundled with your command line application.
10
+ It covers the simple case of executing code based on the flags or parameters
11
+ passed, and it does so with just under 30 lines of code.
12
+
13
+ Usage
14
+ -----
15
+
16
+ Using Clap is simple: just pass `ARGV` and a hash of flags, and it will extract
17
+ the arguments as needed.
18
+
19
+ Clap.run(ARGV,
20
+ "-a" => lambda { |param| ... },
21
+ "-b" => lambda { ... }
22
+
23
+ To better illustrate the usage, in the following examples `ARGV` is replaced by
24
+ an array of strings.
25
+
26
+ If you want your command line application to require a file or display a
27
+ version number, you can configure it like this:
28
+
29
+ Clap.run(%w(-r foo -v),
30
+ "-r" => lambda { |file| require file },
31
+ "-v" => lambda { puts VERSION }
32
+
33
+ This will detect the `-r` or `-v` flags and act accordingly. Note that it will
34
+ also read the necessary number of parameters for each flag based on the arity
35
+ of the passed lambda.
36
+
37
+ Another example, for an application that takes a `-v` flag and also a list of
38
+ files:
39
+
40
+ files = Clap.run(%w(-v foo bar),
41
+ "-v" => lambda { puts VERSION }
42
+
43
+ files == %w(foo bar)
44
+
45
+ If you are in doubt, check the tests for the different use cases.
46
+
47
+ Installation
48
+ ------------
49
+
50
+ $ gem install clap
51
+
52
+ License
53
+ -------
54
+
55
+ Copyright (c) 2010 Michel Martens
56
+
57
+ Permission is hereby granted, free of charge, to any person
58
+ obtaining a copy of this software and associated documentation
59
+ files (the "Software"), to deal in the Software without
60
+ restriction, including without limitation the rights to use,
61
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
62
+ copies of the Software, and to permit persons to whom the
63
+ Software is furnished to do so, subject to the following
64
+ conditions:
65
+
66
+ The above copyright notice and this permission notice shall be
67
+ included in all copies or substantial portions of the Software.
68
+
69
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
70
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
71
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
72
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
73
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
74
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
75
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
76
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,6 @@
1
+ task :test do
2
+ require "cutest"
3
+ Cutest.run(Dir["test/*.rb"])
4
+ end
5
+
6
+ task :default => :test
@@ -0,0 +1,11 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "clap"
3
+ s.version = "0.0.1"
4
+ s.summary = "Command line argument parsing for simple applications."
5
+ s.description = "Clap is a small library that can be bundled with your command line application. It covers the simple case of executing code based on the flags or parameters passed, and it does so with just under 30 lines of code."
6
+ s.authors = ["Michel Martens"]
7
+ s.email = ["michel@soveran.com"]
8
+ s.homepage = "http://github.com/soveran/clap"
9
+ s.files = ["LICENSE", "README.markdown", "Rakefile", "lib/clap.rb", "clap.gemspec", "test/clap_test.rb"]
10
+ s.add_development_dependency "cutest", "~> 0.1"
11
+ end
@@ -0,0 +1,37 @@
1
+ class Clap
2
+ VERSION = "0.0.1"
3
+
4
+ attr :argv
5
+ attr :opts
6
+
7
+ def self.run(args, opts)
8
+ new(args, opts).run
9
+ end
10
+
11
+ def initialize(argv, opts)
12
+ @argv = argv.reverse.dup
13
+ @opts = opts
14
+ end
15
+
16
+ def run
17
+ args = []
18
+
19
+ while argv.any?
20
+ item = argv.pop
21
+
22
+ if opts[item]
23
+
24
+ # Call the lambda with N items from argv,
25
+ # where N is the lambda's arity.
26
+ opts[item].call(*argv.pop(opts[item].arity))
27
+ else
28
+
29
+ # Collect the items that don't correspond to
30
+ # flags.
31
+ args << item
32
+ end
33
+ end
34
+
35
+ args
36
+ end
37
+ end
@@ -0,0 +1,22 @@
1
+ require File.expand_path("../lib/clap", File.dirname(__FILE__))
2
+
3
+ test "flag with argument" do
4
+ result = Clap.run %w(-x y), "-x" => lambda { |flag| assert flag == "y" }
5
+ assert result == []
6
+ end
7
+
8
+ test "flag with wrong number of arguments" do
9
+ assert_raise(ArgumentError) do
10
+ Clap.run %w(-x), "-x" => lambda { |flag| }
11
+ end
12
+ end
13
+
14
+ test "extract flags with parameters" do
15
+ result = Clap.run %w(a b -x y c), "-x" => lambda { |flag| assert flag == "y" }
16
+ assert result == %w(a b c)
17
+ end
18
+
19
+ test "extract flags with no parameters" do
20
+ result = Clap.run %w(a b -x c), "-x" => lambda {}
21
+ assert result == %w(a b c)
22
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: clap
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Michel Martens
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-10-10 00:00:00 -03:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: cutest
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ - 1
31
+ version: "0.1"
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: Clap is a small library that can be bundled with your command line application. It covers the simple case of executing code based on the flags or parameters passed, and it does so with just under 30 lines of code.
35
+ email:
36
+ - michel@soveran.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - LICENSE
45
+ - README.markdown
46
+ - Rakefile
47
+ - lib/clap.rb
48
+ - clap.gemspec
49
+ - test/clap_test.rb
50
+ has_rdoc: true
51
+ homepage: http://github.com/soveran/clap
52
+ licenses: []
53
+
54
+ post_install_message:
55
+ rdoc_options: []
56
+
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ requirements: []
76
+
77
+ rubyforge_project:
78
+ rubygems_version: 1.3.7
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: Command line argument parsing for simple applications.
82
+ test_files: []
83
+