mini-cli 0.1.2 → 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 +4 -4
- data/lib/mini-cli.rb +6 -6
- data/lib/mini-cli/run.rb +22 -22
- data/lib/mini-cli/version.rb +1 -1
- data/test/mini-cli/run_test.rb +29 -26
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 86629e6e4c057eaa03c51483b6931c35517b9753ebdafa2f7b459e494be8b682
|
4
|
+
data.tar.gz: e16f2d52c65a4a84b7e9f631915b2d161c9e13c2d038695c0a7b764280f862c5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef6755a1c1dfdca0313daac675704265c4b35a9a44cad752ef859435abbc69b71ff0350796b86963b1b1038aa03ee609fe85a3e2063ce4b3d1d2d72efc505aea
|
7
|
+
data.tar.gz: b5d68657a09c053b5311a4cf5427a779e374a483142df5ca0704ad4121c4138e506ef508be97261d1bc97bf90111a4abd876b457cf0d747e38cf178bc40b4045
|
data/lib/mini-cli.rb
CHANGED
@@ -25,8 +25,8 @@ module MiniCli
|
|
25
25
|
exit(code)
|
26
26
|
end
|
27
27
|
|
28
|
-
def parse_argv(argv = nil, &
|
29
|
-
return @__argv_converter =
|
28
|
+
def parse_argv(argv = nil, &argv_converter)
|
29
|
+
return @__argv_converter = argv_converter if argv_converter
|
30
30
|
argv ||= ARGV.dup
|
31
31
|
exit(show_help) if argv.index('--help') || argv.index('-h')
|
32
32
|
args = __argv_parser.parse(argv, method(:error).to_proc)
|
@@ -133,11 +133,11 @@ module MiniCli
|
|
133
133
|
when /-([[:alnum:]]), --([[[:alnum:]]-]+) ([[:upper:]]+)\s+\S+/
|
134
134
|
named_option(Regexp.last_match)
|
135
135
|
when /--([[[:alnum:]]-]+) ([[:upper:]]+)\s+\S+/
|
136
|
-
|
136
|
+
named_short_option(Regexp.last_match)
|
137
137
|
when /-([[:alnum:]]), --([[[:alnum:]]-]+)\s+\S+/
|
138
138
|
option(Regexp.last_match)
|
139
139
|
when /--([[[:alnum:]]-]+)\s+\S+/
|
140
|
-
|
140
|
+
short_option(Regexp.last_match)
|
141
141
|
end
|
142
142
|
end
|
143
143
|
end
|
@@ -146,7 +146,7 @@ module MiniCli
|
|
146
146
|
@options[match[1]] = @options[match[2]] = match[3]
|
147
147
|
end
|
148
148
|
|
149
|
-
def
|
149
|
+
def named_short_option(match)
|
150
150
|
@options[match[1]] = match[2]
|
151
151
|
end
|
152
152
|
|
@@ -154,7 +154,7 @@ module MiniCli
|
|
154
154
|
@options[match[1]] = @options[match[2]] = match[2]
|
155
155
|
end
|
156
156
|
|
157
|
-
def
|
157
|
+
def short_option(match)
|
158
158
|
@options[match[1]] = match[1]
|
159
159
|
end
|
160
160
|
end
|
data/lib/mini-cli/run.rb
CHANGED
@@ -1,32 +1,32 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'open3'
|
4
|
-
|
5
3
|
module MiniCli
|
6
|
-
def run(*cmd)
|
7
|
-
|
8
|
-
opts
|
9
|
-
|
4
|
+
def run(*cmd, stdin_data: nil, status: false, chdir: nil)
|
5
|
+
in_read, in_write = IO.pipe
|
6
|
+
opts = { err: %i[child out], in: in_read }
|
7
|
+
opts[:chdir] = chdir if chdir
|
8
|
+
ret = IO.popen(*cmd, opts, &__run_proc(stdin_data, in_write))
|
9
|
+
status ? [Process.last_status, ret] : ret
|
10
|
+
rescue Errno::ENOENT
|
10
11
|
nil
|
12
|
+
ensure
|
13
|
+
in_read&.close
|
14
|
+
in_write&.close
|
11
15
|
end
|
12
16
|
|
13
17
|
private
|
14
18
|
|
15
|
-
def
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
end
|
28
|
-
|
29
|
-
def __run_opt(opts)
|
30
|
-
%i[stdout stderr status].keep_if { |s| opts.delete(s) }
|
19
|
+
def __run_proc(stdin_data, in_write)
|
20
|
+
return :read unless stdin_data
|
21
|
+
proc do |out|
|
22
|
+
in_write.sync = true
|
23
|
+
if stdin_data.respond_to?(:readpartial)
|
24
|
+
IO.copy_stream(stdin_data, in_write)
|
25
|
+
else
|
26
|
+
in_write.write(stdin_data)
|
27
|
+
end
|
28
|
+
in_write.close
|
29
|
+
out.read
|
30
|
+
end
|
31
31
|
end
|
32
32
|
end
|
data/lib/mini-cli/version.rb
CHANGED
data/test/mini-cli/run_test.rb
CHANGED
@@ -1,44 +1,47 @@
|
|
1
1
|
require_relative '../helper'
|
2
2
|
|
3
3
|
class RunTest < Test
|
4
|
-
def
|
5
|
-
|
6
|
-
|
4
|
+
def test_simple
|
5
|
+
result = subject.run('pwd')
|
6
|
+
assert_equal(Dir.pwd + "\n", result)
|
7
7
|
end
|
8
8
|
|
9
|
-
def
|
10
|
-
|
11
|
-
|
9
|
+
def test_simple_error
|
10
|
+
result = subject.run('ls /no-valid-dir')
|
11
|
+
assert_match(/No such file or directory/, result)
|
12
|
+
end
|
12
13
|
|
13
|
-
|
14
|
-
|
14
|
+
def test_chdir
|
15
|
+
home = Dir.home
|
16
|
+
refute(home == Dir.pwd)
|
17
|
+
result = subject.run('pwd', chdir: home)
|
18
|
+
assert_equal(home + "\n", result)
|
15
19
|
end
|
16
20
|
|
17
21
|
def test_status
|
18
|
-
result = subject.run('pwd',
|
19
|
-
assert_instance_of(TrueClass, result)
|
20
|
-
|
21
|
-
status = subject.run('pwd', status: true)
|
22
|
-
assert_instance_of(Process::Status, status)
|
23
|
-
|
24
|
-
out, status = subject.run('pwd', stdout: true, status: true)
|
25
|
-
assert_equal(@pwd, out)
|
22
|
+
status, result = subject.run('pwd', status: true)
|
26
23
|
assert_instance_of(Process::Status, status)
|
27
24
|
assert(status.success?)
|
25
|
+
assert_equal(Dir.pwd + "\n", result)
|
28
26
|
end
|
29
27
|
|
30
|
-
def
|
31
|
-
|
32
|
-
assert_match(/No such file or directory/, err)
|
33
|
-
|
34
|
-
out, err = subject.run('ls /no-valid-dir', stdout: true, stderr: true)
|
35
|
-
assert_empty(out)
|
36
|
-
assert_match(/No such file or directory/, err)
|
37
|
-
|
38
|
-
err, status = subject.run('ls /no-valid-dir', stderr: true, status: true)
|
39
|
-
assert_match(/No such file or directory/, err)
|
28
|
+
def test_status_error
|
29
|
+
status, result = subject.run('ls /no-valid-dir', status: true)
|
40
30
|
assert_instance_of(Process::Status, status)
|
41
31
|
refute(status.success?)
|
32
|
+
assert_match(/No such file or directory/, result)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_stdin
|
36
|
+
string = 'Hello World'
|
37
|
+
result = subject.run('cat', stdin_data: string)
|
38
|
+
assert_equal(string, result)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_stdin_stream
|
42
|
+
stream = StringIO.new("Hello World")
|
43
|
+
result = subject.run('cat', stdin_data: stream)
|
44
|
+
assert_equal(stream.string, result)
|
42
45
|
end
|
43
46
|
|
44
47
|
def test_failure
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mini-cli
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Blumtritt
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-10-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|