dorian-arguments 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/dorian/arguments.rb +125 -0
- data/lib/dorian-arguments.rb +3 -0
- metadata +87 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 951dc350b8ef2808b4d70b61ad7febaf0ebee1cd72d86407e5546d6635178bee
|
4
|
+
data.tar.gz: 19eab1e2e8aa96e4f5bc00cd416d52c7bcc17b108b05f22c2b75d09477c82c4d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3a070d3878e7823609ce007d8f53e7b2efde25a4780a1665aec50b625ae1ef6bb42efa8830d7785c783e396cac5a888b72da68e71d828eee82103594bd44fb33
|
7
|
+
data.tar.gz: 5df10a59d4f6ff04bcc287cbdaa28fc35f90498c4d6a817cb40ead81efa2e69ed8acd50e751be2374a4372d82f4e23c520e28c4ea3403de4c39b9fee37087e35
|
@@ -0,0 +1,125 @@
|
|
1
|
+
require "dorian/to_struct"
|
2
|
+
require "bigdecimal"
|
3
|
+
require "active_support"
|
4
|
+
require "active_support/core_ext/string/inflections"
|
5
|
+
|
6
|
+
class Dorian
|
7
|
+
class Arguments
|
8
|
+
attr_reader :definition
|
9
|
+
|
10
|
+
BOOLEAN = "boolean"
|
11
|
+
STRING = "string"
|
12
|
+
NUMBER = "number"
|
13
|
+
INTEGER = "integer"
|
14
|
+
DECIMAL = "decimal"
|
15
|
+
|
16
|
+
DEFAULT_MULTIPLE = false
|
17
|
+
DEFAULT_ALIASES = []
|
18
|
+
DEFAULT_TYPE = BOOLEAN
|
19
|
+
|
20
|
+
TYPES = [
|
21
|
+
BOOLEAN,
|
22
|
+
STRING,
|
23
|
+
NUMBER,
|
24
|
+
INTEGER,
|
25
|
+
DECIMAL
|
26
|
+
]
|
27
|
+
|
28
|
+
DEFAULTS = {
|
29
|
+
BOOLEAN => "false",
|
30
|
+
STRING => "",
|
31
|
+
NUMBER => "0",
|
32
|
+
INTEGER => "0",
|
33
|
+
DECIMAL => "0"
|
34
|
+
}
|
35
|
+
|
36
|
+
MATCHES = {
|
37
|
+
BOOLEAN => /^(0|1|true|false)$/i,
|
38
|
+
STRING => /^.+$/i,
|
39
|
+
NUMBER => /^[0-9.]+$/i,
|
40
|
+
INTEGER => /^[0-9]+$/i,
|
41
|
+
DECIMAL => /^[0-9.]+$/i
|
42
|
+
}
|
43
|
+
|
44
|
+
BOOLEANS = {
|
45
|
+
"0" => false,
|
46
|
+
"1" => true,
|
47
|
+
"true" => true,
|
48
|
+
"false" => false
|
49
|
+
}
|
50
|
+
|
51
|
+
def initialize(**definition)
|
52
|
+
@definition = definition
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.parse(...)
|
56
|
+
new(...).parse
|
57
|
+
end
|
58
|
+
|
59
|
+
def parse
|
60
|
+
arguments = ARGV
|
61
|
+
options = {}
|
62
|
+
files = []
|
63
|
+
|
64
|
+
definition.each do |key, value|
|
65
|
+
if value.is_a?(Hash)
|
66
|
+
type = value[:type] || DEFAULT_TYPE
|
67
|
+
default = value[:default] || DEFAULTS.fetch(type)
|
68
|
+
aliases = value[:alias] || value[:aliases] || DEFAULT_ALIASES
|
69
|
+
else
|
70
|
+
type = value
|
71
|
+
default = DEFAULTS.fetch(type)
|
72
|
+
aliases = DEFAULT_ALIASES
|
73
|
+
end
|
74
|
+
|
75
|
+
keys = ([key] + aliases).map(&:to_s).map(&:parameterize)
|
76
|
+
keys = keys.map { |key| ["--#{key}", "-#{key}"] }.flatten
|
77
|
+
|
78
|
+
indexes = []
|
79
|
+
|
80
|
+
values = arguments.map.with_index do |argument, index|
|
81
|
+
if keys.include?(argument.split("=").first)
|
82
|
+
indexes << index
|
83
|
+
|
84
|
+
if argument.include?("=")
|
85
|
+
argument.split("=", 2).last
|
86
|
+
elsif arguments[index + 1].to_s =~ MATCHES.fetch(type)
|
87
|
+
indexes << index + 1
|
88
|
+
|
89
|
+
arguments[index + 1]
|
90
|
+
elsif type == BOOLEAN
|
91
|
+
"true"
|
92
|
+
else
|
93
|
+
default
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end.reject(&:nil?)
|
97
|
+
|
98
|
+
if type == BOOLEAN
|
99
|
+
values = values.map { |value| BOOLEANS.fetch(value.downcase) }
|
100
|
+
end
|
101
|
+
|
102
|
+
if type == INTEGER
|
103
|
+
values = values.map { |value| value.to_i }
|
104
|
+
end
|
105
|
+
|
106
|
+
if type == DECIMAL || type == NUMBER
|
107
|
+
values = values.map { |value| BigDecimal(value) }
|
108
|
+
end
|
109
|
+
|
110
|
+
values = values.first if values.size < 2
|
111
|
+
values || BOOLEANS.fetch(DEFAULTS.fetch(type)) if type == BOOLEAN
|
112
|
+
|
113
|
+
indexes.sort.reverse.uniq.each { |index| arguments.delete_at(index) }
|
114
|
+
|
115
|
+
options[key] = values
|
116
|
+
end
|
117
|
+
|
118
|
+
files = arguments.select { |argument| File.exist?(argument) }
|
119
|
+
|
120
|
+
arguments -= files
|
121
|
+
|
122
|
+
[arguments, options.to_struct, files]
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dorian-arguments
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dorian Marié
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-08-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bigdecimal
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: dorian-to_struct
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: parses arguments
|
56
|
+
email: dorian@dorianmarie.com
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files: []
|
60
|
+
files:
|
61
|
+
- lib/dorian-arguments.rb
|
62
|
+
- lib/dorian/arguments.rb
|
63
|
+
homepage: https://github.com/dorianmariecom/dorian-arguments
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
metadata:
|
67
|
+
rubygems_mfa_required: 'true'
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubygems_version: 3.5.11
|
84
|
+
signing_key:
|
85
|
+
specification_version: 4
|
86
|
+
summary: parses arguments
|
87
|
+
test_files: []
|