commandline2 0.7.2

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.
@@ -0,0 +1,12 @@
1
+
2
+ class Module
3
+ def param_accessor(*symbols)
4
+ symbols.each { |sym|
5
+ self.class_eval %{
6
+ def #{sym}(*val)
7
+ val.empty? ? @#{sym} : @#{sym} = val
8
+ end
9
+ }
10
+ }
11
+ end
12
+ end
@@ -0,0 +1,79 @@
1
+ # stolen directly from Open3::open3.rb!
2
+ #
3
+ # open4.rb: Spawn a program like popen, but with stderr, and pid, too. You might
4
+ # also want to use this if you want to bypass the shell. (By passing multiple
5
+ # args, which IO#popen does not allow)
6
+ #
7
+ # Usage:
8
+ # require "open4"
9
+ #
10
+ # stdin, stdout, stderr, pid = Open4.popen4('nroff -man')
11
+ # or
12
+ # include Open4
13
+ # stdin, stdout, stderr, pid = popen4('nroff -man')
14
+
15
+ module Open4
16
+ #--{{{
17
+ #[stdin, stdout, stderr, pid] = popen4(command);
18
+ def popen4(*cmd)
19
+ #--{{{
20
+ pw = IO::pipe # pipe[0] for read, pipe[1] for write
21
+ pr = IO::pipe
22
+ pe = IO::pipe
23
+
24
+ verbose = $VERBOSE
25
+ begin
26
+ $VERBOSE = nil # shut up warning about forking in threads, world writable
27
+ # dirs, etc
28
+ cid =
29
+ fork{
30
+ # child
31
+ pw[1].close
32
+ STDIN.reopen(pw[0])
33
+ pw[0].close
34
+
35
+ pr[0].close
36
+ STDOUT.reopen(pr[1])
37
+ pr[1].close
38
+
39
+ pe[0].close
40
+ STDERR.reopen(pe[1])
41
+ pe[1].close
42
+
43
+ STDOUT.sync = true
44
+ STDERR.sync = true
45
+
46
+ exec(*cmd)
47
+ }
48
+ ensure
49
+ $VERBOSE = verbose
50
+ end
51
+
52
+ pw[0].close
53
+ pr[1].close
54
+ pe[1].close
55
+ pi = [pw[1], pr[0], pe[0]]
56
+ pw[1].sync = true
57
+ if defined? yield
58
+ begin
59
+ yield(cid, *pi)
60
+ return(Process::waitpid2(cid).last)
61
+ ensure
62
+ pi.each{|p| p.close unless p.closed?}
63
+ end
64
+ end
65
+ [cid, pw[1], pr[0], pe[0]]
66
+ #--}}}
67
+ end
68
+ alias open4 popen4
69
+ module_function :popen4
70
+ module_function :open4
71
+ #--}}}
72
+ end
73
+
74
+ if $0 == __FILE__
75
+ status = Open4::popen4('sh'){|cid,i,o,e|i.puts 'echo 42';i.close; puts o.read;}
76
+ p [status]
77
+ p status.exitstatus
78
+ p status == 0
79
+ end
@@ -0,0 +1,77 @@
1
+
2
+ module Test
3
+
4
+ module Unit
5
+
6
+ class TestCase
7
+ require 'open4'
8
+ require 'timeout'
9
+ def run_app(app, *args)
10
+ cmd = [app, args].flatten.join(" ")
11
+ out = nil
12
+ err = nil
13
+ Timeout::timeout(5) {
14
+ status = Open4.popen4(cmd) { |cid, stdin, stdout, stderr|
15
+ out = stdout.read
16
+ err = stderr.read
17
+ stdin.close_write
18
+ }
19
+ }
20
+ rescue(Timeout::Error) => err
21
+ puts "ERROR running #{app} #{args}"
22
+ puts err
23
+ puts err.backtrace
24
+ [out, err, status]
25
+ end
26
+
27
+
28
+ # the above is having problems. I don't know why
29
+ def run_app(app, *args)
30
+ cmd = "#{app} #{args.join(" ")}"
31
+ puts "==> Command: #{cmd}" if $DEBUG
32
+ out = `#{cmd} 2> stderr.txt`
33
+ status = $?
34
+ err = File.read("stderr.txt")
35
+ File.delete("stderr.txt")
36
+ [out, err, status]
37
+ end
38
+
39
+ def assert_success(app, *args)
40
+ args.compact!
41
+ out, err, status = run_app(app, args)
42
+ assert_equal(true, status.success?,
43
+ "Failure running #{[app,args].delete_if { |i| i.empty? }.join(" ")} "+
44
+ "with error(s):\n => stderr: '#{err}'\n => stdout: '#{out}'.")
45
+ yield(out, err, status) if block_given?
46
+ end
47
+
48
+ def assert_failure(app, *args)
49
+ out, err, status = run_app(app, args)
50
+ assert_equal(false, status.success?,
51
+ "No failure running #{[app,args].delete_if { |i| i.empty? }.join(" ")}.")
52
+ yield(out, err, status) if block_given?
53
+ end
54
+
55
+ def assert_coredump(app, *args)
56
+ out, err, status = run_app(app, args)
57
+ assert_equal(true, status.coredump?,
58
+ "Running #{[app,args].delete_if { |i| i.empty? }.join(" ")} did not coredump.")
59
+ yield(out, err, status) if block_given?
60
+ end
61
+ def assert_no_coredump(app, *args)
62
+ out, err, status = run_app(app, args)
63
+ assert_equal(false, status.coredump?,
64
+ "Running #{[app,args].delete_if { |i| i.empty? }.join(" ")} caused coredump.")
65
+ yield(out, err, status) if block_given?
66
+ end
67
+
68
+ def assert_stopped(app, *args)
69
+ out, err, status = run_app(app, args)
70
+ assert_equal(true, status.stopped?,
71
+ "Running #{[app,args].delete_if { |i| i.empty? }.join(" ")} not stopped.")
72
+ yield(out, err, status) if block_given?
73
+ end
74
+
75
+ end
76
+ end#module Unit
77
+ end#module Test
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: commandline2
3
+ version: !ruby/object:Gem::Version
4
+ hash: 7
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 7
9
+ - 2
10
+ version: 0.7.2
11
+ platform: ruby
12
+ authors:
13
+ - Alexander Brausch
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-10-21 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description:
22
+ email: abrausch@web.de
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/commandline/application.rb
31
+ - lib/commandline/kernel.rb
32
+ - lib/commandline/optionparser/option.rb
33
+ - lib/commandline/optionparser/optiondata.rb
34
+ - lib/commandline/optionparser/optionparser.rb
35
+ - lib/commandline/optionparser.rb
36
+ - lib/commandline/utils.rb
37
+ - lib/commandline.rb
38
+ - lib/open4.rb
39
+ - lib/test/unit/systemtest.rb
40
+ homepage: http://rubyforge.org/projects/optionparser/
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.8.7
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Tools to facilitate creation of command line applications and flexible parsing of command line options.
73
+ test_files: []
74
+