rclopts 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/lib/rclopts/gem_info.rb +12 -0
- data/lib/rclopts/required_arg_option_parser.rb +65 -0
- data/lib/rclopts.rb +2 -0
- metadata +49 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 Richard Cook
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
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, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
require 'ostruct'
|
3
|
+
require 'set'
|
4
|
+
|
5
|
+
module Rclopts
|
6
|
+
class RequiredArgOptionParser < OptionParser
|
7
|
+
class RequiredArgOptionParserError < Exception; end
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@required_arg_names = Set.new
|
11
|
+
super
|
12
|
+
end
|
13
|
+
|
14
|
+
def on_required(*args, &block)
|
15
|
+
arg_name = nil
|
16
|
+
args.each do |arg|
|
17
|
+
if arg =~ /^((--\w+)(-\w+)*)/
|
18
|
+
arg_name = $1
|
19
|
+
break
|
20
|
+
end
|
21
|
+
end
|
22
|
+
raise ArgumentError.new('Could not determine argument name') unless arg_name
|
23
|
+
|
24
|
+
@required_arg_names << arg_name
|
25
|
+
|
26
|
+
on *args do |options, value|
|
27
|
+
@missing_arg_names.delete arg_name
|
28
|
+
block.call(options, value)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def on(*args, &block)
|
33
|
+
super *args do |value|
|
34
|
+
block.call(@options, value)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def on_tail(*args, &block)
|
39
|
+
super *args do
|
40
|
+
block.call(@options)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def process_args(args)
|
45
|
+
@missing_arg_names = @required_arg_names.clone
|
46
|
+
@options = OpenStruct.new
|
47
|
+
free_args = args.clone
|
48
|
+
|
49
|
+
begin
|
50
|
+
parse!(free_args)
|
51
|
+
rescue OptionParser::InvalidOption, OptionParser::MissingArgument
|
52
|
+
message = $!.to_s + "\n\n" + self.to_s
|
53
|
+
raise RequiredArgOptionParserError.new(message)
|
54
|
+
end
|
55
|
+
|
56
|
+
if !@missing_arg_names.empty?
|
57
|
+
message = "missing required option: #{@missing_arg_names.to_a.sort.join(', ')}\n\n" + self.to_s
|
58
|
+
raise RequiredArgOptionParserError.new(message)
|
59
|
+
end
|
60
|
+
|
61
|
+
[@options, free_args]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
data/lib/rclopts.rb
ADDED
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rclopts
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Richard Cook
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-07-28 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Ruby command-line helpers
|
15
|
+
email: rcook@rcook.org
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- LICENSE
|
21
|
+
- lib/rclopts/gem_info.rb
|
22
|
+
- lib/rclopts/required_arg_option_parser.rb
|
23
|
+
- lib/rclopts.rb
|
24
|
+
homepage: https://github.com/rcook/rclopts/
|
25
|
+
licenses:
|
26
|
+
- MIT
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
none: false
|
33
|
+
requirements:
|
34
|
+
- - ! '>='
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project:
|
45
|
+
rubygems_version: 1.8.23
|
46
|
+
signing_key:
|
47
|
+
specification_version: 3
|
48
|
+
summary: rclopts
|
49
|
+
test_files: []
|