gflag 0.1.0 → 0.1.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 +4 -4
- data/Rakefile +8 -0
- data/lib/gflag/version.rb +5 -0
- data/lib/gflag.rb +102 -0
- metadata +8 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f7d317f6547e1a9178d8caf2ee9df3ff368ea30c350301b8667ef540e80bdbf5
|
|
4
|
+
data.tar.gz: e8ab6353d8f9eb7b76e5995cd7d08ba7d3f5c298840eed1df957841901fc5b7b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a4c51e0c4bf8327541f34b811028c666876ab3397f18a90a2673994ed6a8f64d92dc4333821d0d064daa1f25729d0fc2764b08c78fa4435f3596d63f13e7aea0
|
|
7
|
+
data.tar.gz: c79b1cd84c34a7fb8502345232bde6570eb79948ad99063f12f2de99b6797df9a44ef125c2a4f67ec0fd03a25d4816ff32e07c9573cb029c63f82621e223a0dc
|
data/Rakefile
ADDED
data/lib/gflag.rb
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
## `g.rb`: An option parser that's more sophisticated than `-s`
|
|
4
|
+
#
|
|
5
|
+
# Inspired by my feature request (https://bugs.ruby-lang.org/issues/21015),
|
|
6
|
+
# this implements what I wish the `-g` flag in Ruby would be: A fast-and-dirty
|
|
7
|
+
# way to specify command-line-arguments that just populate a corresponding
|
|
8
|
+
# global variable.
|
|
9
|
+
##
|
|
10
|
+
|
|
11
|
+
## Emulate other `$-x` globals, so you can check for `g`'s inclusion with `$-g`.
|
|
12
|
+
$-g = true
|
|
13
|
+
|
|
14
|
+
## The list of all global variables that `g` parsed out
|
|
15
|
+
GLOBALS = []
|
|
16
|
+
|
|
17
|
+
## A singleton method on `GLOBALS` used to validate that all variables that
|
|
18
|
+
# were parsed were expected. Note that because of how `g` works, there's no way
|
|
19
|
+
# to differentiate between `-x` and `--x`, but I've never had that be a problem.
|
|
20
|
+
def GLOBALS.expect!(*allowed)
|
|
21
|
+
# Delete all leading `-`s that were possibly provided
|
|
22
|
+
allowed = Array(*allowed).map { _1.sub!(/\A-*/, '') }
|
|
23
|
+
|
|
24
|
+
# Find the first disallowed flag, and fail if it exists
|
|
25
|
+
bad = (self - allowed).first and abort "flag #{bad} is not a recognized flag"
|
|
26
|
+
|
|
27
|
+
# Hunky dory!
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
## Set a global variable to a value. This not only assigns to the actual global
|
|
31
|
+
# variable, but also adds it to the `GLOBALS` array, if it's not already there.
|
|
32
|
+
def GLOBALS.assign(flag, value, orig)
|
|
33
|
+
flag.tr!('-', '_')
|
|
34
|
+
|
|
35
|
+
# Only allow alphanumerics as flags
|
|
36
|
+
unless flag.match? /\A\p{Alnum}+\z/
|
|
37
|
+
abort "flag #{orig} is not a valid flag name"
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Special case booleans, nil, and integers:
|
|
41
|
+
value = case value
|
|
42
|
+
when 'true' then true
|
|
43
|
+
when 'false' then false
|
|
44
|
+
when 'nil' then nil
|
|
45
|
+
when /\A\d+\z/ then Integer value
|
|
46
|
+
else value
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Add the global variable to `self`
|
|
50
|
+
(self << flag).uniq!
|
|
51
|
+
|
|
52
|
+
# Assign the global variable. Sadly, there's no `global_variable_set`, so we
|
|
53
|
+
# must use `eval`
|
|
54
|
+
eval "\$#{flag} = value"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Handle each argument, extracting the flag, or putting it back and `break`ing if we're done
|
|
58
|
+
while (arg = ARGV.shift)
|
|
59
|
+
case arg
|
|
60
|
+
# Negated flags: `--no-foo` is the same as `--foo=false`
|
|
61
|
+
when /\A--no-([^=]+)\z/
|
|
62
|
+
GLOBALS.assign($1, false, arg)
|
|
63
|
+
|
|
64
|
+
# Special case: `--` on its own is an early break
|
|
65
|
+
when '--'
|
|
66
|
+
break
|
|
67
|
+
|
|
68
|
+
# long-form flags, both with and without arguments
|
|
69
|
+
when /\A--([^=]+)\K(=)?/
|
|
70
|
+
GLOBALS.assign($1, $2 ? $' : true, $`)
|
|
71
|
+
|
|
72
|
+
# Shorthand flags
|
|
73
|
+
when /\A-[^-]/
|
|
74
|
+
arg = arg.delete_prefix '-'
|
|
75
|
+
|
|
76
|
+
while (short = arg.slice! 0)
|
|
77
|
+
# Shorthand flag is given an argument with `=`
|
|
78
|
+
if arg.delete_prefix!('=')
|
|
79
|
+
GLOBALS.assign(short, arg, "-#{short}")
|
|
80
|
+
break
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Shorthand flag is given an integer argument; `=` can be omitted
|
|
84
|
+
if arg.start_with?(/\d/)
|
|
85
|
+
unless (arg = Integer(arg, exception: false))
|
|
86
|
+
abort("integer argument for -#{short} has trailing chars: #{arg}")
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
GLOBALS.assign(short, arg, "-#{short}")
|
|
90
|
+
break
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Everything else is a single-character short string
|
|
94
|
+
GLOBALS.assign(short, true, "-#{short}")
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Everything else is a normal argument, and we stop parsing
|
|
98
|
+
else
|
|
99
|
+
ARGV.unshift arg
|
|
100
|
+
break
|
|
101
|
+
end
|
|
102
|
+
end
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: gflag
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- SamW
|
|
8
|
-
bindir:
|
|
8
|
+
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies: []
|
|
12
12
|
email:
|
|
13
13
|
- mail@sampersand.me
|
|
@@ -17,11 +17,13 @@ extra_rdoc_files: []
|
|
|
17
17
|
files:
|
|
18
18
|
- LICENSE
|
|
19
19
|
- README.md
|
|
20
|
+
- Rakefile
|
|
21
|
+
- lib/gflag.rb
|
|
22
|
+
- lib/gflag/version.rb
|
|
20
23
|
homepage: https://github.com/sampersand/gflag
|
|
21
24
|
licenses:
|
|
22
25
|
- MIT
|
|
23
|
-
metadata:
|
|
24
|
-
homepage_uri: https://github.com/sampersand/gflag
|
|
26
|
+
metadata: {}
|
|
25
27
|
rdoc_options: []
|
|
26
28
|
require_paths:
|
|
27
29
|
- lib
|
|
@@ -36,7 +38,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
36
38
|
- !ruby/object:Gem::Version
|
|
37
39
|
version: '0'
|
|
38
40
|
requirements: []
|
|
39
|
-
rubygems_version: 3.
|
|
41
|
+
rubygems_version: 3.7.0.dev
|
|
40
42
|
specification_version: 4
|
|
41
43
|
summary: Adds a the '-g' flag to ruby, which does rudimentary opt parsing
|
|
42
44
|
test_files: []
|