particlecmd 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ed67116073b359825b4b1d0c4c80e2d62c35f3f2e3a11eb6e8264b0f155d49c0
4
+ data.tar.gz: 04c06d7534433e1418507c7580863c910e1171ea0499fe351c4eef78388c10a5
5
+ SHA512:
6
+ metadata.gz: 8d00f5eb8831d37a62e01dd2226267c1ce570b13c3c86187cb5f2b1062fc92e7880d7b6b3b3e2d1b348590ca506417f5a5c0c086eac53c001dc4d4d9bfaa9a09
7
+ data.tar.gz: aad30fab5a877f750981f2e9c8454fd8ffc020fabcc214bb4e686c79ab39f03712d4059fc1f8bb58597a1361eec11e4cbae196534eac8cf10d40746910eedfa0
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in particlecmd.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ particlecmd (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ rake (10.5.0)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ bundler (~> 1.16)
16
+ particlecmd!
17
+ rake (~> 10.0)
18
+
19
+ BUNDLED WITH
20
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Nickolay Ilyushin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # Particlecmd
2
+
3
+ A simple command parser for CLI utils and/or bots/whatever
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'particlecmd'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install particlecmd
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'particlecmd'
25
+
26
+ sample = ParticleCMD::Definition.new 'add' do |d|
27
+ d.positional 'a', description: 'First value'
28
+ d.positional 'b', description: 'Second value'
29
+
30
+ d.collect_extra
31
+
32
+ d.flag 'multiply', description: 'Multiply instead of adding'
33
+
34
+ d.option 'divide', argname: 'X', description: 'Divide the result by X'
35
+ end
36
+
37
+ require 'shellwords'
38
+ puts (sample.match ParticleCMD::Info.new Shellwords.split '1 2 3 \\\' 4 5 "6 7\' 8" --multiply --divide=123').inspect
39
+ # #<ParticleCMD::Result:0x000056286c6ab630 @extra=["3", "'", "4", "5", "6 7' 8"], @positionals={"a"=>"1", "b"=>"2"}, @flags={"multiply"=>true}, @options={"divide"=>"123"}>
40
+ # returns nil if definition and info do not match
41
+ ```
42
+
43
+ ## Development
44
+
45
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
46
+
47
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
48
+
49
+ ## Contributing
50
+
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/handicraftsman/particlecmd.
52
+
53
+ ## License
54
+
55
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'particlecmd'
5
+
6
+ $sample = ParticleCMD::Definition.new 'add' do |d|
7
+ d.positional 'a', description: 'First value'
8
+ d.positional 'b', description: 'Second value'
9
+
10
+ d.collect_extra
11
+
12
+ d.flag 'multiply', description: 'Multiply instead of adding'
13
+
14
+ d.option 'divide', argname: 'X', description: 'Divide the result by X'
15
+ end
16
+
17
+ require 'shellwords'
18
+ puts ($sample.match ParticleCMD::Info.new Shellwords.split '1 2 3 \\\' 4 5 "6 7\' 8" --multiply --divide=123').inspect
19
+
20
+ require 'irb'
21
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,154 @@
1
+ class ParticleCMD::Definition
2
+ attr_accessor :name
3
+
4
+ def initialize(name)
5
+ @name = name
6
+ @positionals = []
7
+ @flags = []
8
+ @options = []
9
+ @collecting = false
10
+ yield self
11
+ end
12
+
13
+ def self.from_string(name, string)
14
+ d = new name do end
15
+ string.split.each do |word|
16
+ if word[0] == '-'
17
+ i = word.match(/-+(.+?)(=(.+))?$/)
18
+ if i[3]
19
+ d.option i[1], argname: i[3]
20
+ else
21
+ d.flag i[1]
22
+ end
23
+ else
24
+ d.positional word
25
+ end
26
+ end
27
+ d
28
+ end
29
+
30
+ def description(type, name, desc)
31
+ case type
32
+ when :positional
33
+ @positionals
34
+ when :flag
35
+ @flags
36
+ when :option
37
+ @options
38
+ else
39
+ raise RuntimeError.new "Invalid argument type: #{type}"
40
+ end.find { |i| i[:name] == name }[:description] = desc
41
+ end
42
+
43
+ def collect_extra
44
+ @collecting = true
45
+ end
46
+
47
+ def positional(name, **_opts)
48
+ opts = {
49
+ name: name,
50
+ description: ''
51
+ }.merge _opts
52
+ @positionals << opts
53
+ end
54
+
55
+ def flag(name, **_opts)
56
+ opts = {
57
+ name: name,
58
+ description: ''
59
+ }.merge _opts
60
+ @flags << opts
61
+ end
62
+
63
+ def option(name, **_opts)
64
+ opts = {
65
+ name: name,
66
+ description: '',
67
+ argname: 'VALUE',
68
+ default: nil
69
+ }.merge _opts
70
+ @options << opts
71
+ end
72
+
73
+ def command_signature(include_name = true)
74
+ s = ''
75
+ s << "#{@name} " if include_name
76
+ @positionals.each do |p|
77
+ s << "#{p[:name]} "
78
+ end
79
+ @flags.each do |f|
80
+ s << "[--#{f[:name]}] "
81
+ end
82
+ @options.each do |o|
83
+ s << "[--#{o[:name]}=#{o[:argname]}] "
84
+ end
85
+ s
86
+ end
87
+
88
+ def command_description
89
+ s = ''
90
+
91
+ nm = [
92
+ (@positionals.map { |p| p[:name].length }.max or 0),
93
+ (@flags.map { |f| f[:name].length + 2 }.max or 0),
94
+ (@options.map { |o| o[:name].length + o[:argname].length + 3 }.max or 0)
95
+ ].max
96
+
97
+ @positionals.each do |p|
98
+ s << (sprintf " %*s %s\n", nm, p[:name], p[:description])
99
+ end
100
+ @flags.each do |f|
101
+ s << (sprintf " %*s %s\n", nm, "--#{f[:name]}", f[:description])
102
+ end
103
+ @options.each do |o|
104
+ s << (sprintf " %*s=%s %s", nm-2, "--#{o[:name]}", o[:argname], o[:description])
105
+ end
106
+ s
107
+ end
108
+
109
+ def help_message
110
+ 'Usage: ' + command_signature + "\nArguments:\n" + command_description
111
+ end
112
+
113
+ def match(info)
114
+ if @collecting
115
+ return nil if @positionals.length > info.positionals.length
116
+ else
117
+ return nil if @positionals.length != info.positionals.length
118
+ end
119
+ return nil if @flags.length < info.flags.length
120
+ return nil if @options.length < info.options.length
121
+ return nil unless (info.flags - @flags.map { |f| f[:name] }).empty?
122
+ return nil unless (info.options.keys - @options.map { |o| o[:name] }).empty?
123
+
124
+ res = ParticleCMD::Result.new
125
+
126
+ for i in 0..(@positionals.length - 1) do
127
+ res.positionals[@positionals[i][:name]] = info.positionals[i]
128
+ end
129
+
130
+ for i in 0..(@flags.length - 1) do
131
+ n = @flags[i][:name]
132
+ res.flags[n] = if info.flags.include? n
133
+ true
134
+ else
135
+ false
136
+ end
137
+ end
138
+
139
+ for i in 0..(@options.length - 1) do
140
+ n = @options[i][:name]
141
+ res.options[n] = if info.options.include? n
142
+ info.options[n]
143
+ else
144
+ nil
145
+ end
146
+ end
147
+
148
+ if @collecting && @positionals.length < info.positionals.length
149
+ res.extra = info.positionals[@positionals.length, info.positionals.length]
150
+ end
151
+
152
+ res
153
+ end
154
+ end
@@ -0,0 +1,21 @@
1
+ class ParticleCMD::Info
2
+ attr_accessor :positionals, :flags, :options
3
+
4
+ def initialize(argv)
5
+ @positionals = []
6
+ @flags = []
7
+ @options = {}
8
+ argv.each do |word|
9
+ if word[0] == '-'
10
+ i = word.match(/-+(.+?)(=(.+))?$/)
11
+ if i[3]
12
+ @options[i[1]] = i[3]
13
+ else
14
+ @flags << i[1]
15
+ end
16
+ else
17
+ @positionals << word
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,10 @@
1
+ class ParticleCMD::Result
2
+ attr_accessor :extra, :positionals, :flags, :options
3
+
4
+ def initialize
5
+ @extra = []
6
+ @positionals = {}
7
+ @flags = {}
8
+ @options = {}
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module ParticleCMD
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,8 @@
1
+ require 'particlecmd/version'
2
+
3
+ module ParticleCMD
4
+ end
5
+
6
+ require 'particlecmd/definition'
7
+ require 'particlecmd/info'
8
+ require 'particlecmd/result'
@@ -0,0 +1,26 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'particlecmd/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'particlecmd'
8
+ spec.version = ParticleCMD::VERSION
9
+ spec.authors = ['Nickolay Ilyushin']
10
+ spec.email = ['nickolay02@inbox.ru']
11
+
12
+ spec.summary = 'A command parser'
13
+ spec.description = 'A command parser'
14
+ spec.homepage = 'https://github.com/handicraftsman/particlecmd'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.16'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: particlecmd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nickolay Ilyushin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-03-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: A command parser
42
+ email:
43
+ - nickolay02@inbox.ru
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - Gemfile.lock
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/console
55
+ - bin/setup
56
+ - lib/particlecmd.rb
57
+ - lib/particlecmd/definition.rb
58
+ - lib/particlecmd/info.rb
59
+ - lib/particlecmd/result.rb
60
+ - lib/particlecmd/version.rb
61
+ - particlecmd.gemspec
62
+ homepage: https://github.com/handicraftsman/particlecmd
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.7.3
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: A command parser
86
+ test_files: []