lbq-cli 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: 4e54c16efe2254003565d861182a5d37dae35965946f4733c62046a787b04a18
4
+ data.tar.gz: 96c52f3aca838207f4bb31e10ef4ce6bae4e8e79c9133e1f930687dcd87a062d
5
+ SHA512:
6
+ metadata.gz: fd8c677ecd62d3ccc3a5eefd6a23dea7f261b0ffda2cedcaede5dc984e685a1c8078a307d06b82bb1187afac88e57001d40bb198e98c8e25aefc9818c091f5a8
7
+ data.tar.gz: fdff1013211b2cf020f4d49d9021df739fff644bddc415cb9189e03a1ad55d3597783e87837092a7c83c4522520a103aa9b215d5506134ea21a3b3ca19b75624
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,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in lbq-cli.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lbq-cli (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ rake (10.5.0)
10
+
11
+ PLATFORMS
12
+ x64-mingw32
13
+
14
+ DEPENDENCIES
15
+ bundler (~> 2.0)
16
+ lbq-cli!
17
+ rake (~> 10.0)
18
+
19
+ BUNDLED WITH
20
+ 2.0.2
data/LICENSE.txt ADDED
@@ -0,0 +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.
data/README.md ADDED
@@ -0,0 +1,40 @@
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
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +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__)
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
data/docs/index.html ADDED
@@ -0,0 +1,10 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <title>LBQ CLI</title>
6
+ </head>
7
+ <body>
8
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quidem quas qui voluptatem beatae nostrum delectus ea similique deserunt molestiae! Maxime qui sapiente debitis enim voluptate, quae accusantium recusandae placeat similique.
9
+ </body>
10
+ </html>
data/examples/argv.rb ADDED
@@ -0,0 +1,37 @@
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
data/examples/echo.rb ADDED
@@ -0,0 +1,10 @@
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
data/examples/help.rb ADDED
@@ -0,0 +1,67 @@
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
data/exe/lbq ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "lbq/cli"
4
+
5
+ Lbq::Cli.execute *ARGV
data/lbq-cli.gemspec ADDED
@@ -0,0 +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
@@ -0,0 +1,5 @@
1
+ module Lbq
2
+ module Cli
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
data/lib/lbq/cli.rb ADDED
@@ -0,0 +1,224 @@
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
+ ARGV.clear.push *#{argv.inspect}
127
+
128
+ def arg2key arg
129
+ (arg.start_with?('--') ? arg[2..-1].tr('-', '_') : arg[1..-1]).to_sym
130
+ end
131
+
132
+ def typed value, type=String
133
+ case
134
+ when type == String
135
+ String(value)
136
+ when type == Integer
137
+ Integer(value)
138
+ when type == Float
139
+ Float(value)
140
+ when type.respond_to?(:call)
141
+ type.call(value)
142
+ else
143
+ # unknown type
144
+ value
145
+ end
146
+ end
147
+
148
+ def params
149
+ @params ||= {}
150
+ if block_given?
151
+ yield
152
+ @default_options.each do |index, (args, type)|
153
+ if value = ARGV[index]
154
+ value = typed value, type
155
+ args.each do |arg|
156
+ key = arg2key arg
157
+ params[key] = value
158
+ end
159
+ ARGV[index] = nil
160
+ end
161
+ end if @default_options
162
+ ARGV.compact!
163
+ ARGV.each { |arg| @missing.call arg } if @missing.respond_to? :call
164
+ end
165
+ @params
166
+ end
167
+
168
+ def switch *args, default: false
169
+ args = args.select { |arg| arg.start_with? '-' }
170
+ if args.any? { |arg| (i = ARGV.index arg) and (ARGV.delete_at i) }
171
+ args.each do |arg|
172
+ key = arg2key arg
173
+ params[key] = !default
174
+ end
175
+ !default
176
+ else
177
+ default
178
+ end
179
+ end
180
+
181
+ def option *args, default: nil, index: nil, type: String
182
+ raw_str = ''
183
+ args = args.select { |arg| arg.start_with? '-' }
184
+ args.each do |arg|
185
+ key = arg2key arg
186
+ params[key] = default
187
+ end if default
188
+ if args.any? { |arg| (i = ARGV.index { |a| a.start_with? "\#{arg}=" }) and (raw_str = ARGV.delete_at i) }
189
+ value = typed (raw_str.split('=')[1] || default), type
190
+ args.each do |arg|
191
+ key = arg2key arg
192
+ params[key] = value
193
+ end
194
+ value
195
+ 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) }
196
+ value = typed (raw_str || default), type
197
+ args.each do |arg|
198
+ key = arg2key arg
199
+ params[key] = value
200
+ end
201
+ value
202
+ elsif index
203
+ @default_options ||= {}
204
+ @default_options[index] = [args, type]
205
+ else
206
+ nil
207
+ end
208
+ end
209
+
210
+ def missing &blk
211
+ @missing = blk
212
+ end
213
+
214
+ def main
215
+ yield
216
+ end
217
+ RUBY
218
+ eval <<~RUBY, TOPLEVEL_BINDING.dup, filename, -script.lines.size
219
+ #{script}
220
+ #{File.read filename}
221
+ RUBY
222
+ end
223
+ end
224
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lbq-cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - hyrious
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-07-21 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: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
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: Simple command line task runner.
42
+ email:
43
+ - hyrious@outlook.com
44
+ executables:
45
+ - lbq
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - Gemfile.lock
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/setup
57
+ - docs/index.html
58
+ - examples/argv.rb
59
+ - examples/echo.rb
60
+ - examples/help.rb
61
+ - exe/lbq
62
+ - lbq-cli.gemspec
63
+ - lib/lbq/cli.rb
64
+ - lib/lbq/cli/version.rb
65
+ homepage: https://hyrious.me/lbq-cli
66
+ licenses:
67
+ - MIT
68
+ metadata:
69
+ homepage_uri: https://hyrious.me/lbq-cli
70
+ source_code_uri: https://github.com/hyrious/lbq-cli
71
+ changelog_uri: https://github.com/hyrious/lbq-cli#changelog
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubygems_version: 3.0.4
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: lbq cmd --arg -abc --opt=val
91
+ test_files: []