mini-cli 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1e6c0c096fa96ac3c5687b159637ae5762c1dfa9d18201b0253476bfb96050ca
4
- data.tar.gz: 21eb69f322117c7eb8cc2c810292b1b38a3a3f268356cdd9c5cd2f2d0d876a9b
3
+ metadata.gz: 86629e6e4c057eaa03c51483b6931c35517b9753ebdafa2f7b459e494be8b682
4
+ data.tar.gz: e16f2d52c65a4a84b7e9f631915b2d161c9e13c2d038695c0a7b764280f862c5
5
5
  SHA512:
6
- metadata.gz: 5c975c9bff22c0841ac32ac6340d74ed908babb282f5791a7ed0951ad7f59fd0f4e9bcb3226a83f4dd1548ccfe340c2071072a94f4ac27e8937cae234fa584ac
7
- data.tar.gz: c8544fba204f56ad6e2359d37ffe9e76ce9997ad34653270d1241d7c82d1153d8826e260f2f40d4840065dc70bd7133139d5cb835fbcee7394696245e825e512
6
+ metadata.gz: ef6755a1c1dfdca0313daac675704265c4b35a9a44cad752ef859435abbc69b71ff0350796b86963b1b1038aa03ee609fe85a3e2063ce4b3d1d2d72efc505aea
7
+ data.tar.gz: b5d68657a09c053b5311a4cf5427a779e374a483142df5ca0704ad4121c4138e506ef508be97261d1bc97bf90111a4abd876b457cf0d747e38cf178bc40b4045
@@ -25,8 +25,8 @@ module MiniCli
25
25
  exit(code)
26
26
  end
27
27
 
28
- def parse_argv(argv = nil, &block)
29
- return @__argv_converter = block if block
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
- named_option_short(Regexp.last_match)
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
- option_short(Regexp.last_match)
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 named_option_short(match)
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 option_short(match)
157
+ def short_option(match)
158
158
  @options[match[1]] = match[1]
159
159
  end
160
160
  end
@@ -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
- opts = Hash === cmd.last ? __run_opt(cmd.last) : %i[stdout]
8
- opts.delete(:stderr) ? __run3(opts, cmd) : __run2(opts, cmd)
9
- rescue SystemCallError
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 __run3(opts, cmd)
16
- result = Open3.capture3(*cmd)
17
- result.shift unless opts.first == :stdout
18
- result.pop unless opts.last == :status
19
- result.size == 1 ? result.first : result
20
- end
21
-
22
- def __run2(opts, cmd)
23
- result = Open3.capture2e(*cmd)
24
- return result.last.success? if opts.empty?
25
- return result if opts.size == 2
26
- opts.first == :status ? result.last : result.first
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MiniCli
4
- VERSION = '0.1.2'
4
+ VERSION = '0.2.0'
5
5
  end
@@ -1,44 +1,47 @@
1
1
  require_relative '../helper'
2
2
 
3
3
  class RunTest < Test
4
- def setup
5
- super
6
- @pwd = Dir.pwd + "\n"
4
+ def test_simple
5
+ result = subject.run('pwd')
6
+ assert_equal(Dir.pwd + "\n", result)
7
7
  end
8
8
 
9
- def test_std_out_only
10
- out = subject.run('pwd')
11
- assert_equal(@pwd, out)
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
- out = subject.run('pwd', stdout: true)
14
- assert_equal(@pwd, out)
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', stdout: false)
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 test_std_error
31
- err = subject.run('ls /no-valid-dir', stderr: true)
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.1.2
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-06-19 00:00:00.000000000 Z
11
+ date: 2020-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler