lbq-cli 0.1.1 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 66a5ba81f5fbd6a6244a625a7c5bf94e6375629ed268952451d52aaae98c6fb0
4
- data.tar.gz: 903186900b1bbef0a843b876326b310bcabb65d2d110382af61532a77d54ff2b
3
+ metadata.gz: 946347f2656aebc8fbfc5d385aa9c05b4c454390a0c9458bab5a0e77038c603a
4
+ data.tar.gz: ed2f88c6f82dd23688b1a558005693b21d8629908ae4b92897af4890720afba4
5
5
  SHA512:
6
- metadata.gz: 7938e0ac10691b8cfa8dfefa592767e6b4195e3c9c8f1653d8bad7b983caa923324021c564b07711d1b328432a82557c77496e5de031064da358bce5597b2139
7
- data.tar.gz: 022ef7fc725b69bb8f5d6ab3d374e5f112682ec0be99a05a37467a0073fb9002b790fb803f73563a98b952227e7354c4a3081b63fdd80cd615997f2f4ee2db55
6
+ metadata.gz: 684e119fa4484b6c6e4032c755e5ca9e237cfd2194a8dfb5823aff900bfca8ebeaafdddc6a18d085c189b0c1319cb910d2555b1bc0ddf46f0bb09bf97f532867
7
+ data.tar.gz: f7f20ee1fd7635994b388e7fffa22d1e4450ee99e2df34c52e68ff058c23fc87b3dbbb2d5c274055a66b9894871d3b7b5aee45c0e137304380de109fc84e4a3d
data/.gitignore CHANGED
@@ -1,8 +1,8 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage/
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source "https://rubygems.org"
2
-
3
- # Specify your gem's dependencies in lbq-cli.gemspec
4
- gemspec
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in lbq-cli.gemspec
4
+ gemspec
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lbq-cli (0.1.0)
4
+ lbq-cli (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -17,4 +17,4 @@ DEPENDENCIES
17
17
  rake (~> 10.0)
18
18
 
19
19
  BUNDLED WITH
20
- 2.0.2
20
+ 2.1.4
@@ -1,21 +1,21 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2019 hyrious
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.
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 hyrious
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 CHANGED
@@ -1,40 +1,29 @@
1
- # Lbq::Cli
2
-
3
- Simple command line task runner.
4
-
5
- ## Installation
6
-
7
- $ gem install lbq-cli
8
- $ lbq # create folder and example scripts, show help
9
-
10
- ## Usage
11
-
12
- Put scripts in your `~/lbq/` folder, say `cmd.rb`.
13
-
14
- **Note:** on Windows, the home folder is `%UserProfile%`.
15
-
16
- $ lbq cmd [...args]
17
-
18
- See example scripts for quick getting started.
19
-
20
- See [the doc](https://hyrious.me/lbq-cli) for more scripts.
21
-
22
- ## Development
23
-
24
- 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.
25
-
26
- 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).
27
-
28
- ## Contributing
29
-
30
- Bug reports and pull requests are welcome on GitHub at https://github.com/hyrious/lbq-cli.
31
-
32
- ## License
33
-
34
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
35
-
36
- ## ChangeLog
37
-
38
- #### 0.1.0
39
-
40
-
1
+ # Lbq::Cli
2
+
3
+ Simple command line task runner.
4
+
5
+ ## Installation
6
+
7
+ $ gem install lbq-cli
8
+
9
+ ## Usage
10
+
11
+ Put scripts in your `~/lbq/` folder, say `cmd.rb`.
12
+
13
+ **Note:** on Windows, the home folder is `%UserProfile%`.
14
+
15
+ $ lbq cmd [...args]
16
+
17
+ ## Development
18
+
19
+ 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.
20
+
21
+ 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).
22
+
23
+ ## Contributing
24
+
25
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hyrious/lbq-cli.
26
+
27
+ ## License
28
+
29
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile CHANGED
@@ -1,2 +1,2 @@
1
- require "bundler/gem_tasks"
2
- task :default => :spec
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -1,14 +1,14 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "lbq/cli"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start(__FILE__)
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "lbq/cli"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup CHANGED
@@ -1,8 +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
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
@@ -1,159 +1,16 @@
1
1
  <!DOCTYPE html>
2
- <html lang="en">
2
+ <html lang="zh-Hans-CN">
3
3
  <head>
4
- <meta charset="UTF-8">
5
- <meta name="viewport" content="width=device-width">
6
- <title>LBQ CLI</title>
7
- <style>
8
- * {
9
- box-sizing: border-box;
10
- }
11
- body {
12
- font-family: 'Microsoft Yahei UI Light', Segoe UI, sans-serif;
13
- }
14
- pre, code {
15
- font-family: 'Courier New', monospace;
16
- }
17
- pre {
18
- margin-top: 2.5em;
19
- white-space: pre-wrap;
20
- word-break: break-all;
21
- padding: 8px;
22
- position: relative;
23
- box-shadow: 0 0 0 1px #0c0c0c inset;
24
- font-size: 14px;
25
- }
26
- h1, h2 {
27
- margin: 0;
28
- padding: 8px;
29
- color: #f2f2f2;
30
- background-color: #0c0c0c;
31
- font-size: 22px;
32
- font-weight: normal;
33
- font-family: 'Microsoft Yahei UI Light', Segoe UI, sans-serif;
34
- }
35
- h2 {
36
- font-size: 16px;
37
- }
38
- pre::before {
39
- content: attr(data-title);
40
- position: absolute;
41
- bottom: 100%;
42
- left: 0;
43
- padding: 4px 8px;
44
- color: #f2f2f2;
45
- background-color: #0c0c0c;
46
- }
47
- </style>
4
+ <meta charset="UTF-8">
5
+ <title>lbq-cli scripts</title>
6
+ <meta name="viewport" content="width=device-width">
7
+ <link rel="stylesheet" href="https://hyrious.me/style.css">
8
+ <script src="https://hyrious.me/main.js"></script>
48
9
  </head>
49
10
  <body>
50
- <h1>Example <code>LBQ-CLI</code> Scripts</h1>
51
- <pre data-title="edit"><code>params do
52
- option('--command', '-c', '--file', '-f', 'the script to edit', index: 0)
53
- end
54
-
55
- main do
56
- if params[:c]
57
- file = "#{params[:c]}.rb"
58
- full_path = File.expand_path(file, __dir__)
59
- unless File.exist? full_path
60
- puts "Not found #{file}"
61
- next
62
- end
63
- system 'subl', full_path
64
- end
65
- end</code></pre>
66
- <pre data-title="fate luck"><code>params do
67
- option('--command', '-c', 'luck', index: 0)
68
- end
69
-
70
- main do
71
- case params[:c]
72
- when 'luck'
73
- luck = rand 0..100
74
- progress = '|' * luck + ' ' * (100 - luck)
75
- printf "%3d%% [%s]", luck, progress
76
- puts
77
- end
78
- end</code></pre>
79
- <pre data-title="todo"><code>class Storage
80
- def initialize file
81
- @file = file
82
- end
83
-
84
- def load_data
85
- if File.exist? @file
86
- open(@file, 'rb') { |f| Marshal.load f }
87
- end
88
- end
89
-
90
- def save_data obj
91
- open(@file, 'wb') { |f| Marshal.dump obj, f }
92
- end
93
- end
94
-
95
- S = Storage.new File.expand_path 'todo.data', __dir__
96
-
97
- params do
98
- option('--task', '-t', 'what to do?', index: 0)
99
- option('--edit', '-e', 'which to edit?', type: Integer)
100
- switch('--edit', '-e', 'edit which?')
101
- option('--delete', '-d', 'which to delete?', type: Integer)
102
- switch('--delete', '-d', 'delete which?')
103
- switch('--clear', '-c', 'remove all todos')
104
- end
105
-
106
- main do
107
- data = S.load_data || []
108
- if new_task = params[:t]
109
- if old_task = params[:e]
110
- data[old_task] = new_task
111
- else
112
- data << new_task
113
- end
114
- S.save_data data
115
- elsif old_task = params[:d]
116
- if old_task == true
117
- puts 'Run "todo -d &lt;id&gt;" to delete task.'
118
- else
119
- data.delete_at old_task
120
- S.save_data data
121
- end
122
- elsif old_task = params[:e]
123
- if old_task == true
124
- puts 'Run "todo -e &lt;id&gt; &lt;todo&gt;" to edit task.'
125
- end
126
- elsif params[:c]
127
- S.save_data data.clear
128
- end
129
- data.each_with_index do |task, i|
130
- puts "[#{i}] #{task}"
131
- end
132
- end</code></pre>
133
- <h2>Windows Only <small>(Some of them can be easily converted to *NIX systems.)</small></h2>
134
- <pre data-title="rerun">params do
135
- option('--file', '--dir', '-d', '--watch', '-w', index: 0)
136
- end
137
-
138
- @pid = 0
139
- @mtime = 0
140
-
141
- main do
142
- if file = params[:file]
143
- loop do
144
- @mtime = File.mtime file
145
- @pid = spawn 'ruby', file
146
- puts "##{$$} -> ##{@pid}"
147
- loop do
148
- sleep 5
149
- break if File.mtime(file) != @mtime
150
- end
151
- system 'taskkill', '/F', '/PID', @pid.to_s
152
- end
153
- end
154
- rescue Interrupt
155
- system 'taskkill', '/F', '/PID', @pid.to_s
156
- puts 'bye'
157
- end<code></code></pre>
11
+ <article>
12
+
13
+ <p class="copyright">&copy; 2020 hyrious</p>
14
+ </article>
158
15
  </body>
159
- </html>
16
+ </html>
data/exe/lbq CHANGED
@@ -1,5 +1,5 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "lbq/cli"
4
-
5
- Lbq::Cli.execute *ARGV
1
+ #!/usr/bin/env ruby
2
+
3
+ require "lbq/cli"
4
+
5
+ Lbq::Cli.execute *ARGV
@@ -1,31 +1,31 @@
1
- lib = File.expand_path("lib", __dir__)
2
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
- require "lbq/cli/version"
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = "lbq-cli"
7
- spec.version = Lbq::Cli::VERSION
8
- spec.authors = ["hyrious"]
9
- spec.email = ["hyrious@outlook.com"]
10
-
11
- spec.summary = %q{lbq cmd --arg -abc --opt=val}
12
- spec.description = %q{Simple command line task runner.}
13
- spec.homepage = "https://hyrious.me/lbq-cli"
14
- spec.license = "MIT"
15
-
16
- spec.metadata["homepage_uri"] = spec.homepage
17
- spec.metadata["source_code_uri"] = "https://github.com/hyrious/lbq-cli"
18
- spec.metadata["changelog_uri"] = "https://github.com/hyrious/lbq-cli#changelog"
19
-
20
- # Specify which files should be added to the gem when it is released.
21
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
- end
25
- spec.bindir = "exe"
26
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
- spec.require_paths = ["lib"]
28
-
29
- spec.add_development_dependency "bundler", "~> 2.0"
30
- spec.add_development_dependency "rake", "~> 10.0"
31
- end
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "lbq/cli/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "lbq-cli"
7
+ spec.version = Lbq::Cli::VERSION
8
+ spec.authors = ["hyrious"]
9
+ spec.email = ["hyrious@outlook.com"]
10
+
11
+ spec.summary = %q{lbq cmd --arg -abc --opt=val}
12
+ spec.description = %q{Simple command line task runner.}
13
+ spec.homepage = "https://hyrious.me/lbq-cli"
14
+ spec.license = "MIT"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/hyrious/lbq-cli"
18
+ spec.metadata["changelog_uri"] = "https://github.com/hyrious/lbq-cli#changelog"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 2.0"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ end
@@ -1,226 +1,53 @@
1
- # require "lbq/cli/version"
2
-
3
- module Lbq
4
- module Cli
5
- class NotFoundError < StandardError
6
- attr_reader :cmd
7
- def initialize cmd=nil
8
- @cmd = cmd
9
- msg = "Not found #{cmd ? "\"#{cmd}\"" : "such command"}"
10
- super msg
11
- end
12
- end
13
-
14
- module Vocab
15
- module_function
16
-
17
- def win?
18
- Gem.win_platform?
19
- end
20
-
21
- def touch
22
- win? ? 'type nul >>' : 'touch'
23
- end
24
-
25
- def prompt
26
- win? ? "#{backslash Dir.home}>" : '$ '
27
- end
28
-
29
- def path str
30
- ret = File.expand_path str
31
- ret = backslash ret if win?
32
- ret
33
- end
34
-
35
- def paths
36
- ret = ENV['PATH']
37
- ret = ret && ret.split(File::PATH_SEPARATOR)
38
- ret = ret || %w[/usr/local/bin /usr/ucb /usr/bin /bin]
39
- ret.select { |path| Dir.exist? path }
40
- end
41
-
42
- def exts
43
- ret = ENV['PATHEXT']
44
- ret = ret && ret.split(File::PATH_SEPARATOR)
45
- ret || ['']
46
- end
47
-
48
- def which cmd
49
- paths.each do |path|
50
- exts.each do |ext|
51
- exe = File.join(path, "#{cmd}#{ext}")
52
- if File.executable? exe
53
- return self.path exe
54
- end
55
- end
56
- end
57
- nil
58
- end
59
-
60
- def editor
61
- editors = win? ? %w[subl code notepad] : %w[vim vi emacs nano]
62
- editors.each do |exe|
63
- return exe if which exe
64
- end
65
- 'EDITOR'
66
- end
67
-
68
- def backslash str
69
- str.tr '/', '\\'
70
- end
71
- end
72
-
73
- V = Vocab
74
-
75
- module_function
76
-
77
- def need_init?
78
- !Dir.exist? V.path '~/lbq'
79
- end
80
-
81
- def copy_file from, to
82
- File.open from do |f|
83
- File.open to, 'wb', f.stat.mode do |t|
84
- IO.copy_stream f, t
85
- end
86
- end
87
- end
88
-
89
- def init_example_scripts
90
- files = Dir.glob File.expand_path '../../examples/*', __dir__
91
- dist = File.expand_path '~/lbq'
92
- Dir.mkdir dist unless Dir.exist? dist
93
- files.each do |file|
94
- copy_file file, File.join(dist, File.basename(file))
95
- end
96
- puts <<~HINT
97
- Example scripts are placed at #{V.path '~/lbq'},
98
- check them for quick getting started.
99
-
100
- HINT
101
- end
102
-
103
- # execute 'cmd', '--arg=42', '-a', '--switch'
104
- def execute cmd='help', *args
105
- init_example_scripts if need_init?
106
- raise NotFoundError, cmd unless exist? cmd
107
- load_script cmd, *args
108
- rescue NotFoundError => e
109
- puts <<~HINT
110
- Run command below to create [#{e.cmd}],
111
-
112
- #{V.prompt}#{V.touch} #{V.path "~/lbq/#{e.cmd}.rb"}
113
- #{V.prompt}#{V.editor} #{V.path "~/lbq/#{e.cmd}.rb"}
114
-
115
- Notice "#{V.prompt}" is the prompt and you don't have to type it.
116
- HINT
117
- end
118
-
119
- def exist? cmd
120
- File.exist? File.expand_path "~/lbq/#{cmd}.rb"
121
- end
122
-
123
- def load_script cmd, *argv
124
- filename = File.expand_path "~/lbq/#{cmd}.rb"
125
- script = <<~RUBY
126
- # coding: utf-8
127
-
128
- ARGV.clear.push *#{argv.map { |e| e.dup.encode('utf-8') }.inspect}
129
-
130
- def arg2key arg
131
- (arg.start_with?('--') ? arg[2..-1].tr('-', '_') : arg[1..-1]).to_sym
132
- end
133
-
134
- def typed value, type=String
135
- case
136
- when type == String
137
- String(value)
138
- when type == Integer
139
- Integer(value)
140
- when type == Float
141
- Float(value)
142
- when type.respond_to?(:call)
143
- type.call(value)
144
- else
145
- # unknown type
146
- value
147
- end
148
- end
149
-
150
- def params
151
- @params ||= {}
152
- if block_given?
153
- yield
154
- @default_options.each do |index, (args, type)|
155
- if value = ARGV[index]
156
- value = typed value, type
157
- args.each do |arg|
158
- key = arg2key arg
159
- params[key] = value
160
- end
161
- ARGV[index] = nil
162
- end
163
- end if @default_options
164
- ARGV.compact!
165
- ARGV.each { |arg| @missing.call arg } if @missing.respond_to? :call
166
- end
167
- @params
168
- end
169
-
170
- def switch *args, default: false
171
- args = args.select { |arg| arg.start_with? '-' }
172
- if args.any? { |arg| (i = ARGV.index arg) and (ARGV.delete_at i) }
173
- args.each do |arg|
174
- key = arg2key arg
175
- params[key] = !default
176
- end
177
- !default
178
- else
179
- default
180
- end
181
- end
182
-
183
- def option *args, default: nil, index: nil, type: String
184
- raw_str = ''
185
- args = args.select { |arg| arg.start_with? '-' }
186
- args.each do |arg|
187
- key = arg2key arg
188
- params[key] = default
189
- end if default
190
- if args.any? { |arg| (i = ARGV.index { |a| a.start_with? "\#{arg}=" }) and (raw_str = ARGV.delete_at i) }
191
- value = typed (raw_str.split('=')[1] || default), type
192
- args.each do |arg|
193
- key = arg2key arg
194
- params[key] = value
195
- end
196
- value
197
- elsif args.any? { |arg| (i = ARGV.index arg) and ARGV[i + 1] and ARGV[i + 1][0] != '-' and (ARGV.delete_at i) and (raw_str = ARGV.delete_at i) }
198
- value = typed (raw_str || default), type
199
- args.each do |arg|
200
- key = arg2key arg
201
- params[key] = value
202
- end
203
- value
204
- elsif index
205
- @default_options ||= {}
206
- @default_options[index] = [args, type]
207
- else
208
- nil
209
- end
210
- end
211
-
212
- def missing &blk
213
- @missing = blk
214
- end
215
-
216
- def main
217
- yield
218
- end
219
- RUBY
220
- eval <<~RUBY, TOPLEVEL_BINDING.dup, filename, -script.lines.size
221
- #{script}
222
- #{File.read filename, encoding: 'utf-8'}
223
- RUBY
224
- end
225
- end
226
- end
1
+ require "fileutils"
2
+ require_relative "cli/version"
3
+
4
+ module Lbq
5
+ module Cli
6
+ VERBOSE = false
7
+
8
+ def self.plugins
9
+ @plugins ||= {}
10
+ end
11
+
12
+ def self.init(folder)
13
+ Dir.mkdir folder
14
+ rescue SystemCallError
15
+ puts $!
16
+ end
17
+
18
+ def self.execute(*argv)
19
+ folder = File.join Dir.home, "lbq"
20
+ init folder unless Dir.exist? folder
21
+ pattern = File.join folder, "*.rb"
22
+ Dir.glob(pattern) { |file| load file }
23
+ argv.size.downto 1 do |n|
24
+ plugins.each do |seq, blk|
25
+ zipper = argv[0, n].zip(seq)
26
+ if zipper.all? { |a, p| p === a }
27
+ blk.call(*zipper.map { |a, p| p.match(a) })
28
+ return
29
+ end
30
+ end
31
+ end
32
+ puts 'Put scripts to ~/lbq/*.rb, example:', <<~RUBY
33
+ require 'lbq/cli'
34
+ R 'test' do
35
+ puts 'Hello, world!'
36
+ end
37
+ RUBY
38
+ end
39
+
40
+ def R(*seq, &blk)
41
+ pattern = seq.map(&:inspect).join(" ")
42
+ if Cli.plugins[seq]
43
+ source = Cli.plugins[seq].source_location.join(':')
44
+ puts "Override: #{pattern}, previous is at #{source}"
45
+ end
46
+ Cli.plugins[seq] = blk
47
+ puts "Loaded: #{pattern}" if VERBOSE
48
+ return seq
49
+ end
50
+ end
51
+ end
52
+
53
+ extend Lbq::Cli
@@ -1,5 +1,5 @@
1
- module Lbq
2
- module Cli
3
- VERSION = "0.1.1"
4
- end
5
- end
1
+ module Lbq
2
+ module Cli
3
+ VERSION = "0.2.0"
4
+ end
5
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lbq-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - hyrious
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-09-04 00:00:00.000000000 Z
11
+ date: 2020-01-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -55,9 +55,6 @@ files:
55
55
  - bin/console
56
56
  - bin/setup
57
57
  - docs/index.html
58
- - examples/argv.rb
59
- - examples/echo.rb
60
- - examples/help.rb
61
58
  - exe/lbq
62
59
  - lbq-cli.gemspec
63
60
  - lib/lbq/cli.rb
@@ -84,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
81
  - !ruby/object:Gem::Version
85
82
  version: '0'
86
83
  requirements: []
87
- rubygems_version: 3.0.4
84
+ rubygems_version: 3.0.3
88
85
  signing_key:
89
86
  specification_version: 4
90
87
  summary: lbq cmd --arg -abc --opt=val
@@ -1,37 +0,0 @@
1
-
2
- params do
3
- # switch/option([long_names][, short_names][, description][, options])
4
- # the program simply recognizes them by prefix '-'s
5
- switch('--switch', '--alias-switch', '-s', 'description', default: false)
6
-
7
- # if specified 'index:', you may pass this argument without '--name=',
8
- # $ lbq argv --arg=val
9
- # is the same as
10
- # $ lbq argv val
11
- option('--arg', 'line 1', 'line 2', index: 0)
12
-
13
- # options can have types
14
- # "Integer/Float/String" will result in calling Integer(value), etc.
15
- # or you can specify a proc to transform string input:
16
- # type: -> str { Date.parse str }
17
- option('--number', '-n', type: Integer)
18
-
19
- # unknown options/unexpected inputs will be passed to the missing block
20
- missing do |raw_str|
21
- puts "unknown input: #{raw_str}"
22
- (params[:unknown] ||= []) << raw_str
23
- end
24
- end
25
-
26
- main do
27
- pp params
28
- # -s --arg=1 -n=42 -v => {
29
- # switch: true,
30
- # alias_switch: true,
31
- # s: true,
32
- # arg: '1',
33
- # number: 42,
34
- # n: 42,
35
- # unknown: ['-v']
36
- # }
37
- end
@@ -1,10 +0,0 @@
1
-
2
- params do
3
- missing do |raw_str|
4
- @word = "#{@word} #{raw_str}"
5
- end
6
- end
7
-
8
- main do
9
- puts @word.reverse if @word
10
- end
@@ -1,67 +0,0 @@
1
- # lbq help [cmd]
2
-
3
- params do
4
- option('--command', '--cmd', '-c', 'the command name', index: 0)
5
- end
6
-
7
- main do
8
- script_files = Dir.glob(File.expand_path '*.rb', __dir__)
9
- commands = script_files.map { |e| File.basename e, '.rb' }
10
- if params[:command].nil?
11
- puts <<~HINT
12
- Run "lbq #{File.basename __FILE__, '.rb'} [cmd]" to see script arguments' descriptions.
13
- Valid commands are:
14
-
15
- #{commands.map { |e| " #{e}" }.join("\n")}
16
- HINT
17
- else
18
- cmd = params[:command]
19
- file = File.expand_path "#{cmd}.rb", __dir__
20
- if File.exist? file
21
- script = <<~RUBY
22
- def params
23
- puts
24
- yield if block_given?
25
- end
26
-
27
- def switch *args, default: false
28
- args = { '-' => [], '--' => [], '' => [] }.merge args.group_by { |s| s[/^-*/] }
29
- args[''].unshift "(default: \#{default})"
30
- until args.values.all?(&:empty?)
31
- first = args['-'].shift
32
- first = "\#{first}," if first
33
- printf "%5s %-22s %s\n", first, args['--'].shift, args[''].shift
34
- end
35
- end
36
-
37
- def option *args, default: nil, index: nil, type: String
38
- args = { '-' => [], '--' => [], '' => [] }.merge args.group_by { |s| s[/^-*/] }
39
- desc = []
40
- desc << "default: \#{default}" if default
41
- desc << "index: \#{index}" if index
42
- desc << "type: \#{type}" if type
43
- args[''].unshift "(\#{desc.join(', ')})"
44
- until args.values.all?(&:empty?)
45
- first = args['-'].shift
46
- first = "\#{first}," if first
47
- printf "%5s %-22s %s\n", first, args['--'].shift, args[''].shift
48
- end
49
- end
50
-
51
- def missing
52
- end
53
-
54
- def main
55
- end
56
- RUBY
57
- eval <<~RUBY, TOPLEVEL_BINDING.dup, file, -script.lines.size
58
- #{script}
59
- #{File.read file}
60
- RUBY
61
- else
62
- puts <<~NOTFOUND
63
- Not found command "#{cmd}".
64
- NOTFOUND
65
- end
66
- end
67
- end