simple_command_line_parser 0.0.1
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/simple_command_line_parser.rb +163 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 44ad08c20f0031b4d60e6fdc75379e4353c8f3b7
|
4
|
+
data.tar.gz: 97764ce12639c868a1df57ad1fa85cd623b86b88
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 953181bc5bf01ae59f065f76909720c3f7572497693b622812dd1eb4cf553eac87a43b188d5757f1b06bc8d479f01937205389c36e859c5d9579a4508d709722
|
7
|
+
data.tar.gz: 671aec1d163d5b1285dfdacc06cf6d9019e74a8c64a87b88e453bca42e0c4a63bbf0dea9f0d0d0f4b7e9adde1468ff301052b06e5f9dad3f4fd0e459e14a6720
|
@@ -0,0 +1,163 @@
|
|
1
|
+
|
2
|
+
module BlackStack
|
3
|
+
|
4
|
+
class SimpleCommandLineParser
|
5
|
+
STRING = 0
|
6
|
+
INT = 1
|
7
|
+
BOOL = 2
|
8
|
+
BOOL_VALUES = ['yes', 'no']
|
9
|
+
|
10
|
+
attr_accessor :args, :conf, :desc
|
11
|
+
|
12
|
+
# list tha possible values for the :type param in the h configuration.
|
13
|
+
# Example: h = [{:name=>'w', :mandatory=>true, :description=>"Worker Name", :type=>BlackStack::CommandLineParser::STRING}]
|
14
|
+
def self.types()
|
15
|
+
[
|
16
|
+
BlackStack::SimpleCommandLineParser::STRING,
|
17
|
+
BlackStack::SimpleCommandLineParser::INT,
|
18
|
+
BlackStack::SimpleCommandLineParser::BOOL
|
19
|
+
]
|
20
|
+
end
|
21
|
+
|
22
|
+
# possible values for the bool parameters in the command line
|
23
|
+
def self.bool_values()
|
24
|
+
BlackStack::SimpleCommandLineParser::BOOL_VALUES
|
25
|
+
end
|
26
|
+
|
27
|
+
# will raise an exception if argv parameters does not meet with the configuration specification.
|
28
|
+
def self.validate_arguments(argv)
|
29
|
+
# validate configuration format
|
30
|
+
raise "Array of strings expected in the argv parameter." if !argv.kind_of?(Array)
|
31
|
+
argv.each { |x| raise "Array of strings expected in the argv parameter." if !x.kind_of?(String) }
|
32
|
+
end
|
33
|
+
|
34
|
+
# will raise an exception if the h configuration does not meet with the configuration specification.
|
35
|
+
def self.validate_configuration(h)
|
36
|
+
raise "Array of strings expected in the h parameter." if !h.kind_of?(Array)
|
37
|
+
h.each { |x| raise "Array of hashes expected in the h parameter." if !x.kind_of?(Hash) }
|
38
|
+
h.each { |x|
|
39
|
+
raise "A :name key expected in all the params specification." if !x.key?(:name)
|
40
|
+
raise "A :mandatory key expected in all the params specification." if !x.key?(:mandatory)
|
41
|
+
raise "A :description key expected in all the params specification." if !x.key?(:description)
|
42
|
+
raise "A :type key expected in all the params specification." if !x.key?(:type)
|
43
|
+
raise "String expected in the value of the :name parameter." if !x[:name].kind_of?(String)
|
44
|
+
raise "String expected in the value of the :mandatory parameter." if (x[:mandatory]!=true && x[:mandatory]!=false)
|
45
|
+
raise "String expected in the value of the :description parameter." if !x[:description].kind_of?(String)
|
46
|
+
raise "Invalid code in the :type parameter." if !self.types.include?(x[:type])
|
47
|
+
}
|
48
|
+
end # validate_configuration
|
49
|
+
|
50
|
+
# return an array of hashes (args) frm the array of string (argv).
|
51
|
+
# Example of the argv array: ['w=worker01', 'd=euler']
|
52
|
+
# Example of the args array: [{'w'=>'worker01'}, {'d'=>'euler'}]
|
53
|
+
def self.parse_argumnts(argv)
|
54
|
+
Hash[ argv.flat_map{|s| s.scan(/([^=\s]+)(?:=(\S+))?/) } ]
|
55
|
+
end
|
56
|
+
|
57
|
+
# will raise an exception if the argv array does not match with the h configuration
|
58
|
+
def self.validate_values(argv, h)
|
59
|
+
argc = SimpleCommandLineParser.parse_argumnts(argv)
|
60
|
+
h.each { |x|
|
61
|
+
# if the parameter is mandatory, it must exist in the argc array
|
62
|
+
raise "Command line parameter #{x[:name]} expected." if ( x[:mandatory]==true && !argc.key?(x[:name]) )
|
63
|
+
|
64
|
+
# if the parmaeter exists, it must match with the type
|
65
|
+
raise "Type mismatch for the command line parameter #{x[:name]}." if x[:type]==SimpleCommandLineParser::STRING && argc[x[:name]].to_s.size==0
|
66
|
+
raise "Type mismatch for the command line parameter #{x[:name]}." if x[:type]==SimpleCommandLineParser::INT && argc[x[:name]].to_s!=argc[x.name].to_i.to_s
|
67
|
+
raise "Type mismatch for the command line parameter #{x[:name]}." if x[:type]==SimpleCommandLineParser::BOOL && !SimpleCommandLineParser::bool_values.include?(argc[x[:name]].to_s)
|
68
|
+
}
|
69
|
+
end
|
70
|
+
|
71
|
+
# argv: Array of strings. It is usually the command line parameters.
|
72
|
+
# h: Array of hashes. Parser configuration.
|
73
|
+
# Example: h = [{:name=>'w', :mandatory=>true, :description=>"Worker Name", :type=>BlackStack::CommandLineParser::STRING}]
|
74
|
+
def set_conf(argv, x)
|
75
|
+
# validations
|
76
|
+
raise ":configuration key expected" if !x.key?(:configuration)
|
77
|
+
raise ":description key expected" if !x.key?(:description)
|
78
|
+
|
79
|
+
# will raise an exception if the argv parameters does not meet with the configuration specification.
|
80
|
+
SimpleCommandLineParser.validate_arguments(argv)
|
81
|
+
|
82
|
+
# will raise an exception if the h configuration does not meet with the configuration specification.
|
83
|
+
SimpleCommandLineParser.validate_configuration(x[:configuration])
|
84
|
+
|
85
|
+
# will raise an exception if the argv array does not match with the h configuration
|
86
|
+
SimpleCommandLineParser.validate_values(argv, x[:configuration])
|
87
|
+
|
88
|
+
# map the parameterss to the object attributes
|
89
|
+
self.args = SimpleCommandLineParser.parse_argumnts(argv)
|
90
|
+
self.conf = x[:configuration]
|
91
|
+
self.desc = x[:description]
|
92
|
+
|
93
|
+
end # def set_conf
|
94
|
+
|
95
|
+
# Unlike the set_conf method, this constructor will use the ARGV constant as the array of command line arguments.
|
96
|
+
# This constructor will simply call the set_conf method.
|
97
|
+
# h: Array of hashes. Parser configuration.
|
98
|
+
# Example: h = [{:name=>'w', :mandatory=>true, :description=>"Worker Name", :type=>BlackStack::CommandLineParser::STRING}]
|
99
|
+
def initialize(x)
|
100
|
+
self.set_conf(ARGV, x)
|
101
|
+
end # initialize
|
102
|
+
|
103
|
+
def key?(k)
|
104
|
+
self.args.key?(k)
|
105
|
+
end
|
106
|
+
|
107
|
+
def value(k)
|
108
|
+
self.args[k][:value]
|
109
|
+
end
|
110
|
+
|
111
|
+
def askHelp?()
|
112
|
+
( key?('h')==true || key?('help')==true || key?('?')==true )
|
113
|
+
end
|
114
|
+
|
115
|
+
def validate()
|
116
|
+
# valido que los argumentos existan en la list de comandos conocidos
|
117
|
+
self.args.each do |key, value|
|
118
|
+
if (self.allowedParams().include?(key) == false)
|
119
|
+
raise "Unhandled command option or parameter (#{key}). Run this command with the option 'help' to get informaton about expected parameters."
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
# TODO: si se pasa el comando w, entonces no se debepasar el comando d
|
124
|
+
|
125
|
+
# TODO: si se pasa el comando w, entonces r debe ser "parent"
|
126
|
+
|
127
|
+
# TODO: si se pasa el comando d, entonces no se debepasar el comando w
|
128
|
+
|
129
|
+
# TODO: si se pasa el comando d, entonces r debe ser "child"
|
130
|
+
|
131
|
+
end
|
132
|
+
|
133
|
+
def appUrl()
|
134
|
+
division_name = self.value("d")
|
135
|
+
|
136
|
+
# validar que el formato no sea nulo
|
137
|
+
if (division_name.to_s.length == 0)
|
138
|
+
raise "Division name expected."
|
139
|
+
end
|
140
|
+
|
141
|
+
if (division_name != "local")
|
142
|
+
dname = division_name
|
143
|
+
else
|
144
|
+
dname = Socket.gethostname # nombre del host local
|
145
|
+
end
|
146
|
+
|
147
|
+
uri = URI("#{WORKER_API_SERVER_URL}/api1.2/division/get.json")
|
148
|
+
res = CallPOST(uri, {'api_key' => WORKER_API_KEY, 'dname' => "#{dname}"})
|
149
|
+
parsed = JSON.parse(res.body)
|
150
|
+
if (parsed["status"] == WORKER_API_SUCCESS)
|
151
|
+
wid = parsed["value"]
|
152
|
+
|
153
|
+
ret = "#{parsed["app_url"]}:#{parsed["app_port"]}"
|
154
|
+
else
|
155
|
+
raise "Error getting connection string: #{parsed["status"]}"
|
156
|
+
end
|
157
|
+
|
158
|
+
return ret
|
159
|
+
end
|
160
|
+
|
161
|
+
end # SimpleCommandLineParser
|
162
|
+
|
163
|
+
end # module BlackStack
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_command_line_parser
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Leandro Daniel Sardi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-11-04 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: This gem has been designed as a part of the BlackStack framework.
|
14
|
+
email: leandro.sardi@expandedventure.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/simple_command_line_parser.rb
|
20
|
+
homepage: https://rubygems.org/gems/blackstack/simple_command_line_parser
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.4.5.1
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Easy command line parser library.
|
44
|
+
test_files: []
|