clint 0.1.5 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/clint.rb +23 -1
  2. data/man/man7/clint.7.gz +0 -0
  3. metadata +6 -60
@@ -2,7 +2,7 @@ class Clint
2
2
 
3
3
  def initialize(options={})
4
4
  reset
5
- @strict = !options[:strict].nil?
5
+ @strict = !!options[:strict]
6
6
  end
7
7
 
8
8
  def usage
@@ -118,6 +118,28 @@ class Clint
118
118
  @args = args
119
119
  end
120
120
 
121
+ # Pass options and arguments however possible to the given callable, which
122
+ # could be a Proc or just an object that responds to the method call.
123
+ def dispatch(callable)
124
+ arity = begin
125
+ callable.arity
126
+ rescue NoMethodError
127
+ callable.method(:call).arity
128
+ end
129
+ if @args.length == arity
130
+ callable.call(*@args)
131
+ elsif -@args.length - 1 == arity
132
+ callable.call(*(@args + [@options]))
133
+ else
134
+ dispatch callable.new
135
+ exit 0
136
+ end
137
+ rescue Exception => e
138
+ raise e if SystemExit == e.class
139
+ usage
140
+ exit 1
141
+ end
142
+
121
143
  # Treat the first non-option argument as a subcommand in the given class.
122
144
  # If a suitable class method is found, it is called with all remaining
123
145
  # arguments, including @options if we can get away with it. Otherwise,
Binary file
metadata CHANGED
@@ -1,12 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clint
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 1
8
- - 5
9
- version: 0.1.5
4
+ version: 0.2.1
10
5
  platform: ruby
11
6
  authors:
12
7
  - Richard Crowley
@@ -14,58 +9,11 @@ autorequire:
14
9
  bindir: bin
15
10
  cert_chain: []
16
11
 
17
- date: 2010-03-09 00:00:00 +00:00
12
+ date: 2010-03-30 00:00:00 +00:00
18
13
  default_executable:
19
14
  dependencies: []
20
15
 
21
- description: |
22
- clint(7) -- Ruby command line argument parser
23
- =============================================
24
-
25
- ## SYNOPSIS
26
-
27
- require 'clint'
28
- c = Clint.new
29
- c.usage do
30
- $stderr.puts "Usage: #{File.basename(__FILE__)} [-h|--help]"
31
- end
32
- c.help do
33
- $stderr.puts " -h, --help\tshow this help message"
34
- end
35
- c.options :help => false, :h => :help
36
- c.parse ARGV
37
- if c.options[:help]
38
- c.help
39
- exit 1
40
- end
41
- c.subcommand Klass
42
-
43
- ## DESCRIPTION
44
-
45
- Clint is an alternative Ruby command line argument parser that's very good for programs using the subcommand pattern familiar from `git`(1), `svn`(1), `apt-get`(8), and many others. In addition, it separates option declarations from usage and help messages becuase the author feels like that's a better idea.
46
-
47
- Clint options are declared by passing hash arguments to `Clint#options`. The hash keys should be `Symbol`s. If the value is also a `Symbol`, an alias is defined from the key to the value. If the value is a `Class`, Clint attempts to find a default value for that class. Otherwise, the value is treated as the default and the value's class will be used to construct type-accurate values from command line arguments.
48
-
49
- `Clint#options` may be called repeatedly to declare extra options and aliases. `Clint#reset` can be used at any time to clear all declared options and aliases.
50
-
51
- `Clint#parse` may likewise be called repeatedly. At the end of each invocation, it stores the remaining non-option arguments, meaning that arguments (for example, `ARGV`) must only be passed as a parameter to the first invocation.
52
-
53
- `Clint#subcommand` may be called after `Clint#parse` to automatically handle the subcommand pattern as follows. The first non-option argument is taken to be the subcommand, which must exist as a singleton or instance method of the class object passed to `Clint#subcommand`. If a suitable class method is found, it is called with all remaining arguments, including a hash of the parsed options if we can get away with it. Otherwise, an instance is constructed with the next non-option argument and the instance method is called with all remaining arguments, again including a hash of the parsed options if we can get away with it.
54
-
55
- Due to limitations in the Ruby 1.8 grammar, all methods that could act as subcommands must not declare default argument values except `options={}` if desired.
56
-
57
- ## THEME SONG
58
-
59
- Leo Arnaud and John Williams - "Bugler's Dream"
60
-
61
- ## AUTHOR
62
-
63
- Richard Crowley <r@rcrowley.org>
64
-
65
- ## SEE ALSO
66
-
67
- The standard Ruby `OptionParser` class <http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html>.
68
-
16
+ description: command line argument parser
69
17
  email: r@rcrowley.org
70
18
  executables: []
71
19
 
@@ -89,20 +37,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
89
37
  requirements:
90
38
  - - ">="
91
39
  - !ruby/object:Gem::Version
92
- segments:
93
- - 0
94
40
  version: "0"
41
+ version:
95
42
  required_rubygems_version: !ruby/object:Gem::Requirement
96
43
  requirements:
97
44
  - - ">="
98
45
  - !ruby/object:Gem::Version
99
- segments:
100
- - 0
101
46
  version: "0"
47
+ version:
102
48
  requirements: []
103
49
 
104
50
  rubyforge_project:
105
- rubygems_version: 1.3.6
51
+ rubygems_version: 1.3.5
106
52
  signing_key:
107
53
  specification_version: 3
108
54
  summary: command line argument parser