simple-cli-options 0.1.0.beta1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/option.rb +76 -0
- data/lib/options.rb +64 -0
- data/lib/simple-cli-options.rb +4 -0
- metadata +64 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 13bfaed8074ca4a161936d0e7467e10f5268c9ae8aaab4a68d3a47bb1861a6f6
|
|
4
|
+
data.tar.gz: 2916c0ea3b668b499a5a0d22fa7e7a3e0a2de753f856d2fd2529ca284dcee920
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 3b416b95da5e146933480332868968382291864aa1fadab49e140e736adff1f6bc2c2543c1480a2d526113f42ad0da44895d43e01fba65d30d4038e1ecdbda71
|
|
7
|
+
data.tar.gz: 41420744ead523fa5b942e59987928a5013dc08305d725a4aeabce726c9e7c45738a470a9d413cbe80c3b8b202aefdda41d317a8ed1be7416306e951bdbd0814
|
data/lib/option.rb
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# typed: strict
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require 'sorbet-runtime'
|
|
5
|
+
require 'stringio'
|
|
6
|
+
|
|
7
|
+
# ==========================================
|
|
8
|
+
# 1. Option クラス(各フラグの定義と検証・変換)
|
|
9
|
+
# ==========================================
|
|
10
|
+
class Option
|
|
11
|
+
extend T::Sig
|
|
12
|
+
|
|
13
|
+
sig { returns(Symbol) }
|
|
14
|
+
attr_reader :name
|
|
15
|
+
|
|
16
|
+
sig { returns(String) }
|
|
17
|
+
attr_reader :short, :long, :desc
|
|
18
|
+
|
|
19
|
+
sig { returns(T::Boolean) }
|
|
20
|
+
attr_reader :required_flag
|
|
21
|
+
|
|
22
|
+
sig do
|
|
23
|
+
params(
|
|
24
|
+
name: Symbol,
|
|
25
|
+
short: String,
|
|
26
|
+
long: String,
|
|
27
|
+
desc: String,
|
|
28
|
+
required: T::Boolean,
|
|
29
|
+
options: T.untyped
|
|
30
|
+
).void
|
|
31
|
+
end
|
|
32
|
+
def initialize(name, short:, long:, desc:, required: false, **options)
|
|
33
|
+
@name = name
|
|
34
|
+
@short = short
|
|
35
|
+
@long = long
|
|
36
|
+
@desc = desc
|
|
37
|
+
@required_flag = required
|
|
38
|
+
|
|
39
|
+
validate_structure!
|
|
40
|
+
|
|
41
|
+
# バリデーションは追加(Array)可能、変換は上書き
|
|
42
|
+
@validators = T.let([options[:validate]].compact, T::Array[T.proc.params(arg0: String).returns(T::Boolean)])
|
|
43
|
+
@converter = T.let(options[:convert] || ->(v) { v }, T.proc.params(arg0: String).returns(T.untyped))
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
sig { params(block: T.proc.params(arg0: String).returns(T::Boolean)).returns(T.self_type) }
|
|
47
|
+
def validate(&block)
|
|
48
|
+
@validators << block
|
|
49
|
+
self
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
sig { params(block: T.proc.params(arg0: String).returns(T.untyped)).returns(T.self_type) }
|
|
53
|
+
def convert(&block)
|
|
54
|
+
@converter = block
|
|
55
|
+
self
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
sig { params(value: String).returns(T.untyped) }
|
|
59
|
+
def process(value)
|
|
60
|
+
@validators.each do |v|
|
|
61
|
+
raise "Validation failed for option '#{@name}' with value: #{value}" unless v.call(value)
|
|
62
|
+
end
|
|
63
|
+
@converter.call(value)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
private
|
|
67
|
+
|
|
68
|
+
sig { void }
|
|
69
|
+
def validate_structure!
|
|
70
|
+
raise ArgumentError, "Property 'name' and 'desc' cannot be empty." if @name.to_s.empty? || @desc.empty?
|
|
71
|
+
|
|
72
|
+
return unless @short.empty? && @long.empty?
|
|
73
|
+
|
|
74
|
+
raise ArgumentError, "At least one of 'short' or 'long' flags must be provided for option '#{@name}'."
|
|
75
|
+
end
|
|
76
|
+
end
|
data/lib/options.rb
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# typed: strict
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require 'sorbet-runtime'
|
|
5
|
+
require_relative 'option'
|
|
6
|
+
|
|
7
|
+
# ==========================================
|
|
8
|
+
# 2. Options クラス(オプションの管理と解析)
|
|
9
|
+
# ==========================================
|
|
10
|
+
class Options
|
|
11
|
+
extend T::Sig
|
|
12
|
+
|
|
13
|
+
sig { params(description: String).void }
|
|
14
|
+
def initialize(description: '')
|
|
15
|
+
@description = T.let(description, String)
|
|
16
|
+
@options = T.let([], T::Array[Option])
|
|
17
|
+
@values = T.let({}, T::Hash[Symbol, T.untyped])
|
|
18
|
+
@program_name = T.let(File.basename($0), String)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
sig { params(option: Option).void }
|
|
22
|
+
def add(option)
|
|
23
|
+
@options << option
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
sig { void }
|
|
27
|
+
def show_help
|
|
28
|
+
puts "#{@description}\n\n" unless @description.empty?
|
|
29
|
+
puts "Usage:\n #{@program_name} [flags]\n\n"
|
|
30
|
+
puts 'Flags:'
|
|
31
|
+
@options.each do |opt|
|
|
32
|
+
flags = [
|
|
33
|
+
opt.short.empty? ? nil : opt.short,
|
|
34
|
+
opt.long.empty? ? nil : opt.long
|
|
35
|
+
].compact.join(', ')
|
|
36
|
+
printf " %-20s %s\n", flags, opt.desc
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
sig { params(argv: T::Array[String]).void }
|
|
41
|
+
def parse!(argv)
|
|
42
|
+
if argv.include?('-h') || argv.include?('--help')
|
|
43
|
+
show_help
|
|
44
|
+
exit 0
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
@options.each do |opt|
|
|
48
|
+
# short または long に一致する引数を探す
|
|
49
|
+
idx = argv.find_index { |arg| arg == opt.short || arg == opt.long }
|
|
50
|
+
|
|
51
|
+
if idx && argv[idx + 1]
|
|
52
|
+
@values[opt.name] = opt.process(T.must(argv[idx + 1]))
|
|
53
|
+
elsif opt.required_flag
|
|
54
|
+
warn "Error: Missing required option: #{opt.long.empty? ? opt.short : opt.long}"
|
|
55
|
+
exit 1
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
sig { params(name: Symbol).returns(T.untyped) }
|
|
61
|
+
def get(name)
|
|
62
|
+
@values[name]
|
|
63
|
+
end
|
|
64
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: simple-cli-options
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0.beta1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- HIROAKI SATOU
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: sorbet-runtime
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0.5'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0.5'
|
|
26
|
+
description: |
|
|
27
|
+
simple-cli-options is a small Ruby library for parsing command-line flags with validation and conversion.
|
|
28
|
+
Define options with short/long forms, then parse ARGV and read values by name.
|
|
29
|
+
This gem is currently in BETA; APIs may change in future releases.
|
|
30
|
+
email:
|
|
31
|
+
- ''
|
|
32
|
+
executables: []
|
|
33
|
+
extensions: []
|
|
34
|
+
extra_rdoc_files: []
|
|
35
|
+
files:
|
|
36
|
+
- lib/option.rb
|
|
37
|
+
- lib/options.rb
|
|
38
|
+
- lib/simple-cli-options.rb
|
|
39
|
+
homepage: https://github.com/hiroakisatou/simple-options
|
|
40
|
+
licenses:
|
|
41
|
+
- MIT
|
|
42
|
+
metadata:
|
|
43
|
+
homepage_uri: https://github.com/hiroakisatou/simple-options
|
|
44
|
+
source_code_uri: https://github.com/hiroakisatou/simple-options
|
|
45
|
+
changelog_uri: https://github.com/hiroakisatou/simple-options/blob/main/README.md
|
|
46
|
+
rdoc_options: []
|
|
47
|
+
require_paths:
|
|
48
|
+
- lib
|
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: 3.0.0
|
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
|
+
requirements:
|
|
56
|
+
- - ">="
|
|
57
|
+
- !ruby/object:Gem::Version
|
|
58
|
+
version: '0'
|
|
59
|
+
requirements: []
|
|
60
|
+
rubygems_version: 4.0.6
|
|
61
|
+
specification_version: 4
|
|
62
|
+
summary: A small Ruby library for parsing command-line flags (short and long options
|
|
63
|
+
with values).
|
|
64
|
+
test_files: []
|