cliswitch 0.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/cliswitch.rb +177 -0
  3. metadata +47 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: dcde2ba58f9e0a5e140dad92e1d94ec7da65301656768f0f213af58fa6c32e6f
4
+ data.tar.gz: 9411596874bf5b9befd7854ed32d2f8932daa4312d9c0ce02d6c4793f884b5a6
5
+ SHA512:
6
+ metadata.gz: 031b062e1cb874d370c004db99448eb901c1449af5a08513a899a9241e58adb2999859ddb3cf0ccf163374fdcab74d4659be879308418e0851ed0a5ab6a7383e
7
+ data.tar.gz: 63020821b4e86cd23b0490d08203482aa43c78ed31b9137ed84d9269b661f8c645832de7aad37576da1ee090ef7735135ee9dff7248269ff6b53e482aea50d75
data/lib/cliswitch.rb ADDED
@@ -0,0 +1,177 @@
1
+ # ------------------------------------------------------
2
+ # File : cliswitch.rb
3
+ # Authors : ccmywish <ccmywish@qq.com>
4
+ # Created on : <2022-04-13>
5
+ # Last modified : <2022-04-13>
6
+ #
7
+ # cliswitch:
8
+ #
9
+ # Parse switch options
10
+ #
11
+ # ------------------------------------------------------
12
+
13
+ class CliSwitch
14
+
15
+ VERSION = "0.1.0"
16
+
17
+ class Option
18
+ attr_accessor :name,
19
+ :short,
20
+ :long,
21
+ :self_required,
22
+ :arg_required,
23
+ :arg_optional,
24
+ :next_arg
25
+
26
+ def initialize(name:,
27
+ short: nil,
28
+ long: nil,
29
+ self_required: false,
30
+ # mandatory
31
+ arg_required: 'required',
32
+ next_arg: nil)
33
+
34
+ if long.nil? and short.nil?
35
+ puts "CliSwitch: Error! Must provide a short or long switch"
36
+ return
37
+ end
38
+
39
+ case arg_required
40
+ when 'required', 'optional', 'noarg'
41
+ # do nothing
42
+ else
43
+ puts "CliSwitch: Error! arg_required must be ['required', 'optional', 'noarg'] "
44
+ return
45
+ end
46
+
47
+ @name = name
48
+ @short = short
49
+ @long = long
50
+ @self_required = self_required
51
+ @arg_required = arg_required
52
+ @next_arg = next_arg
53
+ end
54
+ end
55
+
56
+
57
+ attr_accessor :args
58
+ attr_accessor :options
59
+ attr_accessor :options_config
60
+
61
+
62
+ def initialize(options_config)
63
+ @options_config = options_config
64
+ end
65
+
66
+
67
+ def parse(arr)
68
+
69
+ @args = []
70
+ @options = []
71
+ new_arr = []
72
+
73
+ #
74
+ # Clear hyphen switch's stick effect
75
+ #
76
+ arr.each_with_index do
77
+ if _1 =~ /^-\w.*/
78
+ if _1.size > 2
79
+ new_arr << _1[0,2] << _1[2..]
80
+ next
81
+ end
82
+ end
83
+ new_arr << _1
84
+ end
85
+
86
+ arr = new_arr
87
+
88
+ #
89
+ # Parse the result to @options
90
+ #
91
+ arr.each_with_index do
92
+
93
+ # hyphen switch match
94
+ if _1 =~ /^-\w$/ or _1 =~ /^--\w.+$/
95
+ if op = search_in_options_config(_1)
96
+
97
+ # Whether receive the next arg
98
+ case op.arg_required
99
+ when 'noarg'
100
+ # do nothing
101
+ when 'required'
102
+ op.next_arg = arr[_2 + 1]
103
+ arr.delete_at(_2+1)
104
+ when 'optional'
105
+ next_arg = arr[_2 + 1]
106
+ if next_arg.start_with?('-') && search_in_options_config(next_arg)
107
+ # do nothing
108
+ else
109
+ op.next_arg = next_arg
110
+ arr.delete_at(_2+1)
111
+ end
112
+ when 'noarg'
113
+ # do nothing
114
+ end
115
+ @options << op
116
+ else
117
+ puts "Error: Unknown option: #{_1}"
118
+ end
119
+ next
120
+ end
121
+
122
+
123
+ # join args
124
+ @args << _1
125
+ end
126
+
127
+
128
+ validate_mandatory_options()
129
+
130
+ # Debug
131
+ # puts "=> args:"
132
+ # p @args
133
+ # puts "=> options:"
134
+ # p @options
135
+ #
136
+
137
+ end
138
+
139
+
140
+ private
141
+
142
+
143
+ def search_in_options_config(op)
144
+ @options_config.each do
145
+ if _1.short == op or _1.long == op
146
+ # Handle multiple same switch
147
+ return _1.dup
148
+ end
149
+ end
150
+ nil
151
+ end
152
+
153
+
154
+ def validate_mandatory_options
155
+
156
+ @options_config.each do |opc|
157
+
158
+ # Must required switch
159
+ if opc.self_required == true
160
+ exist = @options.each do |o|
161
+ break true if o.name == opc.name
162
+ false
163
+ end
164
+
165
+ if not exist
166
+ s = opc.short ? opc.short : nil
167
+ l = opc.long ? opc.long : nil
168
+ puts "Error: You must provide option #{s ? s : l} #{ s&&l ? 'or ' + l : nil }"
169
+ end
170
+ end
171
+
172
+ end
173
+
174
+ end
175
+
176
+
177
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cliswitch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ccmywish
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-04-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 'cliswitch: cli option parse.
14
+
15
+ '
16
+ email: ccmywish@qq.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/cliswitch.rb
22
+ homepage: https://github.com/ccmywish
23
+ licenses:
24
+ - MIT
25
+ metadata:
26
+ bug_tracker_uri: https://github.com/ccmywish
27
+ source_code_uri: https://github.com/ccmywish
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubygems_version: 3.3.7
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: 'cliswitch: cli option parse'
47
+ test_files: []