simple_args 0.0.0 → 0.0.2
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/README.md +17 -0
- data/lib/simple_args/match.rb +34 -0
- data/lib/simple_args.rb +53 -2
- metadata +5 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bcb09029ed6fbaa9620cbf36ce26bbca6396c9f3a96e2a0219bca983eabbeabe
|
4
|
+
data.tar.gz: 0f99bc3b2b8bb4bf210acd831788027db9684f4fb7f0ed2a1121521a123b54c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 91e6b7e259dc0e00690a7b4b59b006134c217280961cf71e58ee8233924221d02c132d9fea8dcf4b6146b56a56102dc2196c42fa7283a3ff63909fe3a01ac7ad
|
7
|
+
data.tar.gz: 06444c90a3c32b8a7fe3c43f4dc644ce026b728a6917817860a8907f6bed6b9ae3a7e76e43a250da87b4bc21799d0a81060ae651cb5e99ba17ed1096f27583b0
|
data/README.md
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
[](https://badge.fury.io/rb/simple_args)
|
2
|
+
|
1
3
|
# simple_args
|
2
4
|
|
3
5
|
A simple argument parser, which is the right fit if you want
|
@@ -7,3 +9,18 @@ A simple argument parser, which is the right fit if you want
|
|
7
9
|
* A result object with a `keywords` hash and `positionals` array
|
8
10
|
* Defined methods on your result for predefined options
|
9
11
|
* Use value options __only__ with the `--key=value` syntax
|
12
|
+
|
13
|
+
#
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
# Author
|
18
|
+
|
19
|
+
Copyright © 2024 Robert Dober
|
20
|
+
robert.dober@gmail.com
|
21
|
+
|
22
|
+
# LICENSE
|
23
|
+
|
24
|
+
GNU AFFERO GENERAL PUBLIC LICENSE, Version 3, 19 November 2007. Please refer to [LICENSE](LICENSE) for details.
|
25
|
+
|
26
|
+
<!-- SPDX-License-Identifier: AGPL-3.0-or-later -->
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class SimpleArgs
|
4
|
+
class Match
|
5
|
+
|
6
|
+
attr_reader :matchdata
|
7
|
+
class << self
|
8
|
+
def match(rgx, subject)
|
9
|
+
m = rgx.match(subject)
|
10
|
+
return new(m) if m
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def captures = matchdata.captures
|
15
|
+
def named_captures = matchdata.named_captures
|
16
|
+
def to_a = matchdata.to_a
|
17
|
+
def to_s = matchdata.to_s
|
18
|
+
|
19
|
+
def deconstruct(*) = to_a
|
20
|
+
def deconstruct_keys(*)
|
21
|
+
named_captures
|
22
|
+
.compact
|
23
|
+
.inject({}) do |h, (k, v)|
|
24
|
+
h.merge( k.to_sym => v )
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
def initialize(matchdata)
|
30
|
+
@matchdata = matchdata
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
# SPDX-License-Identifier: AGPL-3.0-or-later
|
data/lib/simple_args.rb
CHANGED
@@ -1,7 +1,58 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative 'simple_args/match'
|
3
4
|
class SimpleArgs
|
4
|
-
VERSION = '0.0.
|
5
|
-
|
5
|
+
VERSION = '0.0.2'
|
6
|
+
ArgsError = Class.new(StandardError)
|
7
|
+
|
8
|
+
AttrRgx = %r{\A (?: -- (?<key>\w+) = (?<value>\w+) | -- (?<long>\w+) | - (?<short>\w+) | (?<end>--) ) \z}x
|
9
|
+
attr_reader :keywords, :positionals
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def initialize(argv, **aliases)
|
14
|
+
parse_args(argv)
|
15
|
+
aliases.each do |aliass, original|
|
16
|
+
if keywords.has_key?(aliass)
|
17
|
+
raise ArgsError, "Double assignment between --#{original} and -#{aliass}" if keywords.has_key?(original)
|
18
|
+
keywords[original] = keywords[aliass]
|
19
|
+
end
|
20
|
+
define_singleton_method(original) { keywords[original] }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def assign_shorts(short)
|
25
|
+
short.grapheme_clusters.each do |sh|
|
26
|
+
keywords[sh.to_sym] = true
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def parse_args(argv)
|
31
|
+
@keywords = {}
|
32
|
+
@positionals = []
|
33
|
+
|
34
|
+
rest = parse_options(argv)
|
35
|
+
@positionals += rest
|
36
|
+
end
|
37
|
+
|
38
|
+
def parse_options(argv)
|
39
|
+
until argv.empty?
|
40
|
+
argv => [arg, *argv]
|
41
|
+
match = Match.match(AttrRgx, arg)
|
42
|
+
case match
|
43
|
+
in {key:, value:}
|
44
|
+
keywords[key.to_sym] = value
|
45
|
+
in {long:}
|
46
|
+
keywords[long.to_sym] = true
|
47
|
+
in {short:}
|
48
|
+
assign_shorts(short)
|
49
|
+
in {end:}
|
50
|
+
return argv
|
51
|
+
else
|
52
|
+
positionals << arg
|
53
|
+
end
|
54
|
+
end
|
55
|
+
[]
|
56
|
+
end
|
6
57
|
end
|
7
58
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
metadata
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_args
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Dober
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-06-
|
12
|
-
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: ex_aequo
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: 0.2.6
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: 0.2.6
|
11
|
+
date: 2024-06-30 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
27
13
|
description: |
|
28
14
|
A simple argument parser, which is the right fit if you want
|
29
15
|
|
@@ -40,6 +26,7 @@ files:
|
|
40
26
|
- LICENSE
|
41
27
|
- README.md
|
42
28
|
- lib/simple_args.rb
|
29
|
+
- lib/simple_args/match.rb
|
43
30
|
homepage: https://codeberg.org/lab419/simple_args
|
44
31
|
licenses:
|
45
32
|
- AGPL-3.0-or-later
|
@@ -59,7 +46,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
46
|
- !ruby/object:Gem::Version
|
60
47
|
version: '0'
|
61
48
|
requirements: []
|
62
|
-
rubygems_version: 3.5.
|
49
|
+
rubygems_version: 3.5.14
|
63
50
|
signing_key:
|
64
51
|
specification_version: 4
|
65
52
|
summary: A simple argument parser, which is the right fit if you want * Aliases for
|