Skip to content
Browse files

Initial import.

  • Loading branch information...
0 parents commit 686bb9f9f04171a60b596ae326b55521b6dfb326 @soveran committed Oct 10, 2010
Showing with 191 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +19 −0 LICENSE
  3. +76 −0 README.markdown
  4. +6 −0 Rakefile
  5. +11 −0 clap.gemspec
  6. +19 −0 clap.gemspec.erb
  7. +37 −0 lib/clap.rb
  8. +22 −0 test/clap_test.rb
1 .gitignore
@@ -0,0 +1 @@
+/pkg
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2010 Michel Martens
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
76 README.markdown
@@ -0,0 +1,76 @@
+Clap
+====
+
+Command line argument parsing for simple applications.
+
+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.
+
+Usage
+-----
+
+Using Clap is simple: just pass `ARGV` and a hash of flags, and it will extract
+the arguments as needed.
+
+ Clap.run(ARGV,
+ "-a" => lambda { |param| ... },
+ "-b" => lambda { ... }
+
+To better illustrate the usage, in the following examples `ARGV` is replaced by
+an array of strings.
+
+If you want your command line application to require a file or display a
+version number, you can configure it like this:
+
+ Clap.run(%w(-r foo -v),
+ "-r" => lambda { |file| require file },
+ "-v" => lambda { puts VERSION }
+
+This will detect the `-r` or `-v` flags and act accordingly. Note that it will
+also read the necessary number of parameters for each flag based on the arity
+of the passed lambda.
+
+Another example, for an application that takes a `-v` flag and also a list of
+files:
+
+ files = Clap.run(%w(-v foo bar),
+ "-v" => lambda { puts VERSION }
+
+ files == %w(foo bar)
+
+If you are in doubt, check the tests for the different use cases.
+
+Installation
+------------
+
+ $ gem install clap
+
+License
+-------
+
+Copyright (c) 2010 Michel Martens
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
6 Rakefile
@@ -0,0 +1,6 @@
+task :test do
+ require "cutest"
+ Cutest.run(Dir["test/*.rb"])
+end
+
+task :default => :test
11 clap.gemspec
@@ -0,0 +1,11 @@
+Gem::Specification.new do |s|
+ s.name = "clap"
+ s.version = "0.0.1"
+ s.summary = "Command line argument parsing for simple applications."
+ 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."
+ s.authors = ["Michel Martens"]
+ s.email = ["michel@soveran.com"]
+ s.homepage = "http://github.com/soveran/clap"
+ s.files = ["LICENSE", "README.markdown", "Rakefile", "lib/clap.rb", "clap.gemspec", "test/clap_test.rb"]
+ s.add_development_dependency "cutest", "~> 0.1"
+end
19 clap.gemspec.erb
@@ -0,0 +1,19 @@
+<% require "./lib/clap" -%>
+Gem::Specification.new do |s|
+ s.name = "clap"
+ s.version = "<%= Clap::VERSION %>"
+ s.summary = "Command line argument parsing for simple applications."
+ 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."
+ s.authors = ["Michel Martens"]
+ s.email = ["michel@soveran.com"]
+ s.homepage = "http://github.com/soveran/clap"
+ s.files = <%= Dir[
+ "LICENSE",
+ "README.markdown",
+ "Rakefile",
+ "lib/**/*.rb",
+ "*.gemspec",
+ "test/**/*.rb"
+ ].inspect %>
+ s.add_development_dependency "cutest", "~> 0.1"
+end
37 lib/clap.rb
@@ -0,0 +1,37 @@
+class Clap
+ VERSION = "0.0.1"
+
+ attr :argv
+ attr :opts
+
+ def self.run(args, opts)
+ new(args, opts).run
+ end
+
+ def initialize(argv, opts)
+ @argv = argv.reverse.dup
+ @opts = opts
+ end
+
+ def run
+ args = []
+
+ while argv.any?
+ item = argv.pop
+
+ if opts[item]
+
+ # Call the lambda with N items from argv,
+ # where N is the lambda's arity.
+ opts[item].call(*argv.pop(opts[item].arity))
+ else
+
+ # Collect the items that don't correspond to
+ # flags.
+ args << item
+ end
+ end
+
+ args
+ end
+end
22 test/clap_test.rb
@@ -0,0 +1,22 @@
+require File.expand_path("../lib/clap", File.dirname(__FILE__))
+
+test "flag with argument" do
+ result = Clap.run %w(-x y), "-x" => lambda { |flag| assert flag == "y" }
+ assert result == []
+end
+
+test "flag with wrong number of arguments" do
+ assert_raise(ArgumentError) do
+ Clap.run %w(-x), "-x" => lambda { |flag| }
+ end
+end
+
+test "extract flags with parameters" do
+ result = Clap.run %w(a b -x y c), "-x" => lambda { |flag| assert flag == "y" }
+ assert result == %w(a b c)
+end
+
+test "extract flags with no parameters" do
+ result = Clap.run %w(a b -x c), "-x" => lambda {}
+ assert result == %w(a b c)
+end

0 comments on commit 686bb9f

Please sign in to comment.
Something went wrong with that request. Please try again.