optparse 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: false
2
+ require 'optparse'
3
+
4
+ class OptionParser::AC < OptionParser
5
+ private
6
+
7
+ def _check_ac_args(name, block)
8
+ unless /\A\w[-\w]*\z/ =~ name
9
+ raise ArgumentError, name
10
+ end
11
+ unless block
12
+ raise ArgumentError, "no block given", ParseError.filter_backtrace(caller)
13
+ end
14
+ end
15
+
16
+ ARG_CONV = proc {|val| val.nil? ? true : val}
17
+
18
+ def _ac_arg_enable(prefix, name, help_string, block)
19
+ _check_ac_args(name, block)
20
+
21
+ sdesc = []
22
+ ldesc = ["--#{prefix}-#{name}"]
23
+ desc = [help_string]
24
+ q = name.downcase
25
+ ac_block = proc {|val| block.call(ARG_CONV.call(val))}
26
+ enable = Switch::PlacedArgument.new(nil, ARG_CONV, sdesc, ldesc, nil, desc, ac_block)
27
+ disable = Switch::NoArgument.new(nil, proc {false}, sdesc, ldesc, nil, desc, ac_block)
28
+ top.append(enable, [], ["enable-" + q], disable, ['disable-' + q])
29
+ enable
30
+ end
31
+
32
+ public
33
+
34
+ def ac_arg_enable(name, help_string, &block)
35
+ _ac_arg_enable("enable", name, help_string, block)
36
+ end
37
+
38
+ def ac_arg_disable(name, help_string, &block)
39
+ _ac_arg_enable("disable", name, help_string, block)
40
+ end
41
+
42
+ def ac_arg_with(name, help_string, &block)
43
+ _check_ac_args(name, block)
44
+
45
+ sdesc = []
46
+ ldesc = ["--with-#{name}"]
47
+ desc = [help_string]
48
+ q = name.downcase
49
+ with = Switch::PlacedArgument.new(*search(:atype, String), sdesc, ldesc, nil, desc, block)
50
+ without = Switch::NoArgument.new(nil, proc {}, sdesc, ldesc, nil, desc, block)
51
+ top.append(with, [], ["with-" + q], without, ['without-' + q])
52
+ with
53
+ end
54
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: false
2
+ require 'optparse'
3
+ require 'date'
4
+
5
+ OptionParser.accept(DateTime) do |s,|
6
+ begin
7
+ DateTime.parse(s) if s
8
+ rescue ArgumentError
9
+ raise OptionParser::InvalidArgument, s
10
+ end
11
+ end
12
+ OptionParser.accept(Date) do |s,|
13
+ begin
14
+ Date.parse(s) if s
15
+ rescue ArgumentError
16
+ raise OptionParser::InvalidArgument, s
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+ require 'optparse'
3
+
4
+ class OptionParser
5
+ def define_by_keywords(options, meth, **opts)
6
+ meth.parameters.each do |type, name|
7
+ case type
8
+ when :key, :keyreq
9
+ op, cl = *(type == :key ? %w"[ ]" : ["", ""])
10
+ define("--#{name}=#{op}#{name.upcase}#{cl}", *opts[name]) do |o|
11
+ options[name] = o
12
+ end
13
+ end
14
+ end
15
+ options
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: false
2
+ # -*- ruby -*-
3
+
4
+ require 'shellwords'
5
+ require 'optparse'
6
+
7
+ OptionParser.accept(Shellwords) {|s,| Shellwords.shellwords(s)}
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: false
2
+ require 'optparse'
3
+ require 'time'
4
+
5
+ OptionParser.accept(Time) do |s,|
6
+ begin
7
+ (Time.httpdate(s) rescue Time.parse(s)) if s
8
+ rescue
9
+ raise OptionParser::InvalidArgument, s
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: false
2
+ # -*- ruby -*-
3
+
4
+ require 'optparse'
5
+ require 'uri'
6
+
7
+ OptionParser.accept(URI) {|s,| URI.parse(s) if s}
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: false
2
+ # OptionParser internal utility
3
+
4
+ class << OptionParser
5
+ def show_version(*pkgs)
6
+ progname = ARGV.options.program_name
7
+ result = false
8
+ show = proc do |klass, cname, version|
9
+ str = "#{progname}"
10
+ unless klass == ::Object and cname == :VERSION
11
+ version = version.join(".") if Array === version
12
+ str << ": #{klass}" unless klass == Object
13
+ str << " version #{version}"
14
+ end
15
+ [:Release, :RELEASE].find do |rel|
16
+ if klass.const_defined?(rel)
17
+ str << " (#{klass.const_get(rel)})"
18
+ end
19
+ end
20
+ puts str
21
+ result = true
22
+ end
23
+ if pkgs.size == 1 and pkgs[0] == "all"
24
+ self.search_const(::Object, /\AV(?:ERSION|ersion)\z/) do |klass, cname, version|
25
+ unless cname[1] == ?e and klass.const_defined?(:Version)
26
+ show.call(klass, cname.intern, version)
27
+ end
28
+ end
29
+ else
30
+ pkgs.each do |pkg|
31
+ begin
32
+ pkg = pkg.split(/::|\//).inject(::Object) {|m, c| m.const_get(c)}
33
+ v = case
34
+ when pkg.const_defined?(:Version)
35
+ pkg.const_get(n = :Version)
36
+ when pkg.const_defined?(:VERSION)
37
+ pkg.const_get(n = :VERSION)
38
+ else
39
+ n = nil
40
+ "unknown"
41
+ end
42
+ show.call(pkg, n, v)
43
+ rescue NameError
44
+ end
45
+ end
46
+ end
47
+ result
48
+ end
49
+
50
+ def each_const(path, base = ::Object)
51
+ path.split(/::|\//).inject(base) do |klass, name|
52
+ raise NameError, path unless Module === klass
53
+ klass.constants.grep(/#{name}/i) do |c|
54
+ klass.const_defined?(c) or next
55
+ klass.const_get(c)
56
+ end
57
+ end
58
+ end
59
+
60
+ def search_const(klass, name)
61
+ klasses = [klass]
62
+ while klass = klasses.shift
63
+ klass.constants.each do |cname|
64
+ klass.const_defined?(cname) or next
65
+ const = klass.const_get(cname)
66
+ yield klass, cname, const if name === cname
67
+ klasses << const if Module === const and const != ::Object
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ name = File.basename(__FILE__, ".gemspec")
4
+ version = ["lib", Array.new(name.count("-")+1, "..").join("/")].find do |dir|
5
+ break File.foreach(File.join(__dir__, dir, "#{name.tr('-', '/')}.rb")) do |line|
6
+ /^\s*OptionParser::Version\s*=\s*"(.*)"/ =~ line and break $1
7
+ end rescue nil
8
+ end
9
+
10
+ Gem::Specification.new do |spec|
11
+ spec.name = name
12
+ spec.version = version
13
+ spec.authors = ["Nobu Nakada"]
14
+ spec.email = ["nobu@ruby-lang.org"]
15
+
16
+ spec.summary = %q{OptionParser is a class for command-line option analysis.}
17
+ spec.description = %q{OptionParser is a class for command-line option analysis.}
18
+ spec.homepage = "https://github.com/ruby/optparse"
19
+ spec.license = ["Ruby", "BSD-2-Clause"]
20
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
21
+ spec.licenses = ["Ruby", "BSD-2-Clause"]
22
+
23
+ spec.metadata["homepage_uri"] = spec.homepage
24
+ spec.metadata["source_code_uri"] = spec.homepage
25
+
26
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
27
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: optparse
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nobu Nakada
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-09-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: OptionParser is a class for command-line option analysis.
14
+ email:
15
+ - nobu@ruby-lang.org
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".github/workflows/test.yml"
21
+ - ".gitignore"
22
+ - COPYING
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - README.md
26
+ - Rakefile
27
+ - bin/console
28
+ - bin/setup
29
+ - lib/optionparser.rb
30
+ - lib/optparse.rb
31
+ - lib/optparse/ac.rb
32
+ - lib/optparse/date.rb
33
+ - lib/optparse/kwargs.rb
34
+ - lib/optparse/shellwords.rb
35
+ - lib/optparse/time.rb
36
+ - lib/optparse/uri.rb
37
+ - lib/optparse/version.rb
38
+ - optparse.gemspec
39
+ homepage: https://github.com/ruby/optparse
40
+ licenses:
41
+ - Ruby
42
+ - BSD-2-Clause
43
+ metadata:
44
+ homepage_uri: https://github.com/ruby/optparse
45
+ source_code_uri: https://github.com/ruby/optparse
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 2.5.0
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubygems_version: 3.1.2
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: OptionParser is a class for command-line option analysis.
65
+ test_files: []