simple_command_line_parser 0.0.1 → 1.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 44ad08c20f0031b4d60e6fdc75379e4353c8f3b7
4
- data.tar.gz: 97764ce12639c868a1df57ad1fa85cd623b86b88
3
+ metadata.gz: 8fdb8a4c6de0e6a5c6789dd66f4e81bd72c9cd31
4
+ data.tar.gz: 69fa6a11bbc7471f54ae4b12186c1edea19b7fd4
5
5
  SHA512:
6
- metadata.gz: 953181bc5bf01ae59f065f76909720c3f7572497693b622812dd1eb4cf553eac87a43b188d5757f1b06bc8d479f01937205389c36e859c5d9579a4508d709722
7
- data.tar.gz: 671aec1d163d5b1285dfdacc06cf6d9019e74a8c64a87b88e453bca42e0c4a63bbf0dea9f0d0d0f4b7e9adde1468ff301052b06e5f9dad3f4fd0e459e14a6720
6
+ metadata.gz: b2e70774b3b6b3a7ab88d77dd0fdf4b8e5db8ac69a8e8fe81713d2026d7c68b1f984dde4ca29203f8957536a581749d4d1313c5e08aceb8c31f3afd54e46a2bb
7
+ data.tar.gz: 4c24163caee8a128b6cb403254a87578ca8c347cb836ebc9f3dc3f2112f63bf48b456557c6030ccc4a35db70c17025cfd5363df5a1a7176de1cef241e637e28f
@@ -2,14 +2,17 @@
2
2
  module BlackStack
3
3
 
4
4
  class SimpleCommandLineParser
5
+ YES = 'yes'
6
+ NO = 'no'
7
+
5
8
  STRING = 0
6
9
  INT = 1
7
10
  BOOL = 2
8
- BOOL_VALUES = ['yes', 'no']
11
+ BOOL_VALUES = [BlackStack::SimpleCommandLineParser::YES, BlackStack::SimpleCommandLineParser::NO]
9
12
 
10
13
  attr_accessor :args, :conf, :desc
11
14
 
12
- # list tha possible values for the :type param in the h configuration.
15
+ # List tha possible values for the :type param in the h configuration.
13
16
  # Example: h = [{:name=>'w', :mandatory=>true, :description=>"Worker Name", :type=>BlackStack::CommandLineParser::STRING}]
14
17
  def self.types()
15
18
  [
@@ -18,20 +21,28 @@ module BlackStack
18
21
  BlackStack::SimpleCommandLineParser::BOOL
19
22
  ]
20
23
  end
24
+
25
+ # List tha possible values for the :type param in the h configuration.
26
+ # Example: h = [{:name=>'w', :mandatory=>true, :description=>"Worker Name", :type=>BlackStack::CommandLineParser::STRING}]
27
+ def self.type_name(n)
28
+ return 'string' if n == BlackStack::SimpleCommandLineParser::STRING
29
+ return 'integer' if n == BlackStack::SimpleCommandLineParser::INT
30
+ return 'boolean' if n == BlackStack::SimpleCommandLineParser::BOOL
31
+ end
21
32
 
22
33
  # possible values for the bool parameters in the command line
23
34
  def self.bool_values()
24
35
  BlackStack::SimpleCommandLineParser::BOOL_VALUES
25
36
  end
26
37
 
27
- # will raise an exception if argv parameters does not meet with the configuration specification.
38
+ # Will raise an exception if argv parameters does not meet with the configuration specification.
28
39
  def self.validate_arguments(argv)
29
40
  # validate configuration format
30
41
  raise "Array of strings expected in the argv parameter." if !argv.kind_of?(Array)
31
42
  argv.each { |x| raise "Array of strings expected in the argv parameter." if !x.kind_of?(String) }
32
43
  end
33
44
 
34
- # will raise an exception if the h configuration does not meet with the configuration specification.
45
+ # Will raise an exception if the h configuration does not meet with the configuration specification.
35
46
  def self.validate_configuration(h)
36
47
  raise "Array of strings expected in the h parameter." if !h.kind_of?(Array)
37
48
  h.each { |x| raise "Array of hashes expected in the h parameter." if !x.kind_of?(Hash) }
@@ -40,32 +51,49 @@ module BlackStack
40
51
  raise "A :mandatory key expected in all the params specification." if !x.key?(:mandatory)
41
52
  raise "A :description key expected in all the params specification." if !x.key?(:description)
42
53
  raise "A :type key expected in all the params specification." if !x.key?(:type)
54
+ raise "A :default key expected if the param is not mandatory." if ( x[:mandatory]==false && !x.key?(:default) )
55
+
43
56
  raise "String expected in the value of the :name parameter." if !x[:name].kind_of?(String)
44
57
  raise "String expected in the value of the :mandatory parameter." if (x[:mandatory]!=true && x[:mandatory]!=false)
45
58
  raise "String expected in the value of the :description parameter." if !x[:description].kind_of?(String)
46
59
  raise "Invalid code in the :type parameter." if !self.types.include?(x[:type])
47
- }
60
+
61
+ # if the :default key exists, it must match with the type
62
+ if x.key?(:default)
63
+ raise "Type mismatch for the default value of #{x[:name]}." if x[:type]==SimpleCommandLineParser::STRING && x[:default].to_s.size==0
64
+ raise "Type mismatch for the default value of #{x[:name]}." if x[:type]==SimpleCommandLineParser::INT && x[:default].to_s!=x[:default].to_i.to_s
65
+ raise "Type mismatch for the default value of #{x[:name]}." if x[:type]==SimpleCommandLineParser::BOOL && x[:default]!=true && x[:default]!=false
66
+ end
67
+ }
48
68
  end # validate_configuration
49
69
 
50
- # return an array of hashes (args) frm the array of string (argv).
70
+ # Returns an array of hashes (args) frm the array of string (argv).
51
71
  # Example of the argv array: ['w=worker01', 'd=euler']
52
72
  # Example of the args array: [{'w'=>'worker01'}, {'d'=>'euler'}]
53
73
  def self.parse_argumnts(argv)
54
74
  Hash[ argv.flat_map{|s| s.scan(/([^=\s]+)(?:=(\S+))?/) } ]
55
75
  end
56
76
 
57
- # will raise an exception if the argv array does not match with the h configuration
77
+ # Will raise an exception if the argv array does not match with the h configuration
58
78
  def self.validate_values(argv, h)
59
79
  argc = SimpleCommandLineParser.parse_argumnts(argv)
60
80
  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]) )
81
+ if ( x[:mandatory] == true || argc[x[:name]].to_s.size>0 )
82
+ # if the parameter is mandatory, it must exist in the argc array
83
+ raise "Command line parameter #{x[:name]} expected." if ( x[:mandatory]==true && !argc.key?(x[:name]) )
84
+
85
+ # if the parmaeter exists, it must match with the type
86
+ raise "Type mismatch for the command line parameter #{x[:name]}." if x[:type]==SimpleCommandLineParser::STRING && argc[x[:name]].to_s.size==0
87
+ 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
88
+ raise "Type mismatch for the command line parameter #{x[:name]}." if x[:type]==SimpleCommandLineParser::BOOL && !SimpleCommandLineParser::bool_values.include?(argc[x[:name]].to_s)
89
+ end # if
90
+ } # h.reach
91
+ end
63
92
 
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
- }
93
+ # Return true if the name of anyone of the parameters is 'h', 'help' or '?'.
94
+ # Otherwise return false.
95
+ def asking_help?()
96
+ ( key?('h')==true || key?('help')==true || key?('?')==true )
69
97
  end
70
98
 
71
99
  # argv: Array of strings. It is usually the command line parameters.
@@ -91,45 +119,57 @@ module BlackStack
91
119
  self.desc = x[:description]
92
120
 
93
121
  end # def set_conf
94
-
122
+
123
+ # write a full documentation of the command in the standard output
124
+ def print_help()
125
+ puts self.desc
126
+ puts "Parameters:"
127
+ self.conf.each { |y|
128
+ print "#{y[:name]}"
129
+ print " (#{BlackStack::SimpleCommandLineParser.type_name(y[:type])}. optional)" if !y[:mandatory]
130
+ print " (#{BlackStack::SimpleCommandLineParser.type_name(y[:type])})" if !y[:mandatory]
131
+ print ": "
132
+ print y[:description]
133
+ puts ""
134
+ puts ""
135
+ }
136
+ end
137
+
95
138
  # Unlike the set_conf method, this constructor will use the ARGV constant as the array of command line arguments.
96
139
  # This constructor will simply call the set_conf method.
97
140
  # h: Array of hashes. Parser configuration.
98
141
  # Example: h = [{:name=>'w', :mandatory=>true, :description=>"Worker Name", :type=>BlackStack::CommandLineParser::STRING}]
99
142
  def initialize(x)
100
143
  self.set_conf(ARGV, x)
144
+ self.print_help if self.asking_help?
101
145
  end # initialize
102
146
 
147
+ # Return true if the name of anyone of the parameters is equel to the k parameter.
148
+ # Otherwise return false.
103
149
  def key?(k)
104
150
  self.args.key?(k)
105
151
  end
106
152
 
153
+ # Returns a string, and int, or a bool type value; depending in the type configuration of the param.
154
+ # If the type value is unknown, it will returns nil.
107
155
  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
156
+ h = self.args[k]
157
+ i = self.conf.select {|j| j[:name]==k}.first
158
+ raise "Unknown name of paraeter." if i.nil?
159
+ if !h.nil?
160
+ return h.to_s if i[:type] == BlackStack::SimpleCommandLineParser::STRING
161
+ return h.to_i if i[:type] == BlackStack::SimpleCommandLineParser::INT
162
+ return true if ( h == BlackStack::SimpleCommandLineParser::YES && i[:type] == BlackStack::SimpleCommandLineParser::BOOL )
163
+ return false if ( h == BlackStack::SimpleCommandLineParser::NO && i[:type] == BlackStack::SimpleCommandLineParser::BOOL )
164
+ else
165
+ return i[:default].to_s if i[:type] == BlackStack::SimpleCommandLineParser::STRING
166
+ return i[:default].to_i if i[:type] == BlackStack::SimpleCommandLineParser::INT
167
+ return true if ( i[:default] == true && i[:type] == BlackStack::SimpleCommandLineParser::BOOL )
168
+ return false if ( i[:default] == false && i[:type] == BlackStack::SimpleCommandLineParser::BOOL )
121
169
  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
-
170
+ return nil
131
171
  end
132
-
172
+
133
173
  def appUrl()
134
174
  division_name = self.value("d")
135
175
 
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_command_line_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leandro Daniel Sardi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-04 00:00:00.000000000 Z
11
+ date: 2019-11-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: This gem has been designed as a part of the BlackStack framework.
13
+ description: 'Find documentation here: https://github.com/leandrosardi/simple_command_line_parser.'
14
14
  email: leandro.sardi@expandedventure.com
15
15
  executables: []
16
16
  extensions: []