opp 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.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +24 -0
  3. data/lib/opp.rb +91 -0
  4. metadata +46 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 81ad8ada6040b0889e5ecb235ac3c3f6984861a873c6b43ccef2983302d36329
4
+ data.tar.gz: 7274afc3ef775161202bf0ecd3347d3e960b6a73c09481185d9531819af7445b
5
+ SHA512:
6
+ metadata.gz: ccac5009a2952880b2da533090bde8734301a8348ecc1b11e5cb86cc5a0a663b5ba12d2edf6ecf7fc927da8058c35a6bb953e611f9b95e4e504aa6b0ba729955
7
+ data.tar.gz: c2e5a220072aa313e6e92bdf9f92d77a2da513f1a7ce800672e12006f9977bccc0d15736e6797a4c444cfc63254c27973d51859b06683360423c56fd73323a72
data/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <http://unlicense.org/>
data/lib/opp.rb ADDED
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Inspiration for this came from a feature request by Sampersand: https://bugs.ruby-lang.org/issues/21015
4
+
5
+ require 'set'
6
+
7
+ reserved_names = Set[
8
+ 'FILENAME', 'VERBOSE', 'DEBUG', 'stdin', 'stdout', # globals
9
+ 'stderr', 'LOAD_PATH', 'LOADED_FEATURES',
10
+ 'ERROR_INFO', 'ERROR_POSITION', 'FS', 'FIELD_SEPARATOR', # English module
11
+ 'OFS', 'OUTPUT_FIELD_SEPARATOR', 'RS', 'INPUT_RECORD_SEPARATOR',
12
+ 'ORS', 'OUTPUT_RECORD_SEPARATOR', 'INPUT_LINE_NUMBER', 'NR',
13
+ 'LAST_READ_LINE', 'DEFAULT_OUTPUT', 'DEFAULT_INPUT', 'PID', 'PROCESS_ID',
14
+ 'CHILD_STATUS', 'LAST_MATCH_INFO', 'IGNORECASE', 'ARGV', 'MATCH',
15
+ 'PREMATCH', 'POSTMATCH', 'LAST_PAREN_MATCH'
16
+ ]
17
+
18
+ make_valid_name = lambda do |name|
19
+ name = name.to_s.gsub('-', '_')
20
+ reserved_names.include?(name) ? "_#{name}" : name
21
+ end
22
+ set_global = lambda do |name, value, mode = :normal|
23
+ name = name.to_s.strip # strip might not be necessary
24
+ raise "Illegal parameter name: «#{name}»" unless /\A\$?[a-zA-Z_][a-zA-Z0-9_]*\z/ =~ name
25
+
26
+ name = "$#{name}" unless name.start_with?('$')
27
+ eq_sign = case mode
28
+ when :normal then '='
29
+ when :optional then '||='
30
+ when :adder then '+='
31
+ else raise 'Illegal operation in `set_global`.'
32
+ end
33
+ eval("#{name} #{eq_sign} ObjectSpace._id2ref(#{value.object_id})", binding, __FILE__, __LINE__)
34
+ value
35
+ end
36
+ global_is_an_integer = lambda do |name|
37
+ name = name.to_s.strip
38
+ raise "Illegal parameter name: «#{name}»" unless /\A\$?[a-zA-Z_][a-zA-Z0-9_]*\z/ =~ name
39
+
40
+ name = "$#{name}" unless name.start_with?('$')
41
+ eval(name, binding, __FILE__, __LINE__).is_a?(Integer)
42
+ end
43
+ process_value = lambda do |value|
44
+ case value # in the feature request, matz was against this
45
+ when 'true' then true
46
+ when 'false' then false
47
+ when 'nil' then nil
48
+ when /\A[0-9]+\z/ then Integer(value)
49
+ else value
50
+ end
51
+ end
52
+
53
+ while true
54
+ break if ARGV.empty?
55
+
56
+ arg = ARGV.first
57
+ case arg
58
+ when '--', '', /\A[^-]/ # this is for breaking early, otherwise the else section would handle these
59
+ break
60
+ when /\A--(?<name>[a-zA-Z_][a-zA-Z0-9_-]*)(?<value>=.*)?\z/
61
+ rem = Regexp.last_match
62
+ name = make_valid_name[rem['name']]
63
+ value = if rem['value']
64
+ process_value[rem['value'][1..]]
65
+ else
66
+ true
67
+ end
68
+ set_global[name, value]
69
+ when /\A-(?<flags>[a-zA-Z][a-zA-Z0-9]*)(?<value>=.*)?\z/
70
+ rem = Regexp.last_match
71
+ flags = rem['flags'].chars
72
+ raise "Illegal argument format: «#{arg}»" if /[0-9]/ =~ flags.first ||
73
+ (rem['value'] && /[a-zA-Z]/ !~ flags.last)
74
+
75
+ set_global[make_valid_name[flags.pop], process_value[rem['value'][1..]]] if rem['value']
76
+
77
+ flags.slice_before(/[a-zA-Z]/).each do |ary|
78
+ name = make_valid_name[ary.shift]
79
+ if ary.empty?
80
+ set_global[name, 0, :optional]
81
+ set_global[name, 1, :adder] if global_is_an_integer[name]
82
+ else
83
+ set_global[name, Integer(ary.join)]
84
+ end
85
+ end
86
+ else # something starts with three dashes or one or two dashes followed by non-conventional characters
87
+ break
88
+ end
89
+
90
+ ARGV.shift
91
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: opp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Astra
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-10-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Parses options passed on the command-line and assigns them to eponymous
14
+ global variables, similar in functionality to Ruby interpreter's `-s` flag but with
15
+ more features.
16
+ email:
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - LICENSE
22
+ - lib/opp.rb
23
+ homepage:
24
+ licenses:
25
+ - UNLICENSE
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubygems_version: 3.3.15
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Automatic CLI options to global variables parser
46
+ test_files: []