argparse 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8c47aa7c1a5561464ce640ae7b32a0a502206de110266fe279ef41a13b8c946d
4
+ data.tar.gz: 69ff2ea44dd42997dd375451994d4fa59bc1263da32885d416e0729f3f05a93e
5
+ SHA512:
6
+ metadata.gz: c8172e339f33260c3c3b3d6a01ebe4787b042c15af1306c26e51f76690575c435a24e2a2ec8bb932d0b876cb91f42a8b97a40df8d928fcde4bd3be894a2f4523
7
+ data.tar.gz: 29daa4c7e3cdfe82a0d93a40001209d3e59a418945ec13089f12a84dabf01c29258e0a4604921c84f7f7f292766cd598bc2496f65b0864cab658c6d7775e15e5
@@ -0,0 +1,14 @@
1
+ require_relative "lib/argsparser/version.rb"
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'argparse'
5
+ s.version = ArgsParser::Version
6
+ s.summary = 'A gem for parsing command line arguments'
7
+ s.description = s.summary
8
+ s.authors = ["Matthias Lee"]
9
+ s.email = 'matthias@matthiasclee.com'
10
+ s.files = Dir.glob("**/*")
11
+ s.files.delete("argparse-#{ArgsParser::Version}.gem")
12
+ s.require_paths = ["lib"]
13
+ s.homepage = "https://github.com/Matthiasclee/ArgParse"
14
+ end
data/lib/argparse.rb ADDED
@@ -0,0 +1 @@
1
+ require_relative "argsparser.rb"
@@ -0,0 +1,15 @@
1
+ module ArgsParser
2
+ class Args
3
+ def initialize(options: {}, switches: {})
4
+ args = ARGV
5
+
6
+ x = Parser.parse args, options, switches
7
+
8
+ @switches = Switches.new(x[1])
9
+ @options = Options.new(x[0])
10
+ @data = x[2]
11
+ end
12
+
13
+ attr_reader :switches, :options, :data
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ module ArgsParser
2
+ class Options
3
+ def initialize(options)
4
+ @s = options
5
+ end
6
+
7
+ def [](a)
8
+ if @s[a.to_sym]
9
+ if @s[a.to_sym][:arg] == :none
10
+ return true
11
+ else
12
+ return @s[a.to_sym][:arg]
13
+ end
14
+ else
15
+ return false
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,72 @@
1
+ module ArgsParser
2
+ module Parser
3
+ def self.parse(args, expected_opts, expected_switches)
4
+ @@options = {}
5
+ @@switches = {}
6
+ @@data = []
7
+
8
+ read = :new_arg
9
+ args.each do |arg|
10
+ if read == :new_arg
11
+ if arg.start_with?("--")
12
+ b = arg.sub("--", "").split("=")
13
+ a = b[0].to_sym
14
+
15
+ if expected_opts.keys.include?(a)
16
+ if expected_opts[a][:has_argument]
17
+ if b[1]
18
+ @@options[a] = {arg: b[1]}
19
+ else
20
+ @@options[a] = {arg: :yes}
21
+ read = {arg_for_opt: a}
22
+ end
23
+ else
24
+ @@options[a] = {arg: :none}
25
+ end
26
+ end
27
+ elsif arg.start_with?("-")
28
+ b = arg.sub("-", "").split("=")
29
+ a = b[0].split("")
30
+
31
+ rmode = :new_switch
32
+ a.each do |x|
33
+ x = x.to_sym
34
+
35
+ if rmode == :new_switch
36
+ if expected_switches.keys.include?(x)
37
+ if expected_switches[x][:has_argument]
38
+ if b[1] && x == a.reverse[0].to_sym
39
+ @@switches[x] = {arg: b[1]}
40
+ else
41
+ rmode = {arg_for_switch: x}
42
+ @@switches[x] = {arg: :yes}
43
+ end
44
+ else
45
+ @@switches[x] = {arg: :none}
46
+ end
47
+ end
48
+ elsif rmode[:arg_for_switch]
49
+ @@switches[rmode[:arg_for_switch]][:arg] = x
50
+ rmode = :new_switch
51
+ end
52
+ end
53
+
54
+ if rmode.class == Hash
55
+ read = rmode
56
+ end
57
+ else
58
+ @@data << arg
59
+ end
60
+ elsif read[:arg_for_opt]
61
+ @@options[read[:arg_for_opt]][:arg] = arg
62
+ read = :new_arg
63
+ elsif read[:arg_for_switch]
64
+ @@switches[read[:arg_for_switch]][:arg] = arg
65
+ read = :new_arg
66
+ end
67
+ end
68
+
69
+ return [@@options, @@switches, @@data]
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,19 @@
1
+ module ArgsParser
2
+ class Switches
3
+ def initialize(switches)
4
+ @s = switches
5
+ end
6
+
7
+ def [](a)
8
+ if @s[a.to_sym]
9
+ if @s[a.to_sym][:arg] == :none
10
+ return true
11
+ else
12
+ return @s[a.to_sym][:arg]
13
+ end
14
+ else
15
+ return false
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module ArgsParser
2
+ Version = "0.0.2"
3
+ end
data/lib/argsparser.rb ADDED
@@ -0,0 +1,7 @@
1
+ require_relative "argsparser/version.rb"
2
+ require_relative "argsparser/args.rb"
3
+ require_relative "argsparser/parser.rb"
4
+ require_relative "argsparser/switches.rb"
5
+ require_relative "argsparser/options.rb"
6
+
7
+ module ArgsParser; end
data/readme.md ADDED
@@ -0,0 +1,35 @@
1
+ # ArgParse
2
+
3
+ A gem for parsing command-line arguments.
4
+
5
+ ## Usage:
6
+ ```rb
7
+ require "argparse"
8
+
9
+ o = { # options prefixed with --
10
+ asd: { has_argument: false }, # option with no arguments
11
+ qwe: { has_argument: true } # option with arguments
12
+ }
13
+
14
+ s = { # switched prefixed with -
15
+ q: { has_argument: true }, # switch with arguments
16
+ a: { has_argument: false } # switch without arguments
17
+ }
18
+
19
+ x=ArgsParser::Args.new(options: o, switches: s)
20
+ ```
21
+
22
+ Running it:
23
+ ```sh
24
+ $ ruby example.rb data --asd --qwe aaa -a -qa "more data"
25
+ ```
26
+
27
+ To get args:
28
+ ```rb
29
+ x.switches[:q] # => a
30
+ x.switches[:a] # => true
31
+ x.options[:asd] # => true
32
+ x.options[:qwe] # => aaa
33
+
34
+ x.data.to_s # => ["data", "more data"]
35
+ ```
data/test.rb ADDED
@@ -0,0 +1,20 @@
1
+ require "argparse"
2
+
3
+ o = {
4
+ asd: { has_argument: false },
5
+ qwe: { has_argument: true }
6
+ }
7
+
8
+ s = {
9
+ q: { has_argument: true },
10
+ a: { }
11
+ }
12
+
13
+ x=ArgsParser::Args.new(options: o, switches: s)
14
+
15
+ puts x.switches[:q]
16
+ puts x.switches[:a]
17
+ puts x.options[:asd]
18
+ puts x.options[:qwe]
19
+
20
+ puts x.data.to_s
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: argparse
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Matthias Lee
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-05-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A gem for parsing command line arguments
14
+ email: matthias@matthiasclee.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - argsparser.gemspec
20
+ - lib/argparse.rb
21
+ - lib/argsparser.rb
22
+ - lib/argsparser/args.rb
23
+ - lib/argsparser/options.rb
24
+ - lib/argsparser/parser.rb
25
+ - lib/argsparser/switches.rb
26
+ - lib/argsparser/version.rb
27
+ - readme.md
28
+ - test.rb
29
+ homepage: https://github.com/Matthiasclee/ArgParse
30
+ licenses: []
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubygems_version: 3.1.6
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: A gem for parsing command line arguments
51
+ test_files: []