scriptster 0.2.1 → 0.2.2

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
  SHA1:
3
- metadata.gz: 4ab01abdd932c4089a19e464727861c4397c886d
4
- data.tar.gz: 771d12e4026b18fc257387c5a4f59ad71debe8ef
3
+ metadata.gz: 551e2e0a9a146fcef001e12bc824a0e802b390e0
4
+ data.tar.gz: 286aaacba36201700bd8e4ace12f49a530f32f98
5
5
  SHA512:
6
- metadata.gz: 35ff0d7d04e9187d2703636d1cb82e0a4c3879a60d0f64b0d0fff411b3ece52b061a78d4ddd075de2308a5c417df275a530a2040c78041d97606dea5f1ea7d05
7
- data.tar.gz: a930061dd62ee56bba2566f41dd186fafc6ae4d31224f34e8858561b65d391ca34374797f63c4eb544ba1d7e2430959cfb05b77ad9fa3a645bd48313cf3ee4dc
6
+ metadata.gz: d16e10d4d6a8442f06b5782075e167cf29906a3e0ab2377b168d98fbd81eb96d27bcc639349c61bc06ea38ef3885c178d5aeebe5f8165cb18d0066706fc86b07
7
+ data.tar.gz: 0ea04e268a4994bd1b9beba50dfeeae794c8ee0f1416344b389b96dd3303a0fb48de0acc0dd40361fe298b0f22844c107ac30fb54577de848995a0f40dd42b09
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 0.2.2 (2015-07-12)
2
+
3
+ Improvements:
4
+
5
+ - added the parse_args method
6
+
1
7
  ## 0.2.1 (2015-07-06)
2
8
 
3
9
  Bug fixes:
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Docopt docs: http://bit.ly/1LT7Rch
4
+ # Scriptster::configure docs: http://bit.ly/1H4X3kR
5
+ # Scriptster::cmd docs: http://bit.ly/1LOuVrB
6
+
7
+ require 'scriptster'
8
+ include Scriptster
9
+
10
+ Scriptster::configure do |conf|
11
+ # Which log levels will be printed
12
+ # (one of :quiet, :essential, :important, :informative or :verbose)
13
+ conf.verbosity = :informative
14
+
15
+ # Customise the log line format
16
+ conf.log_format = '%{timestamp} [%{name}] %{type} %{message}'
17
+
18
+ # Save the logs into a file
19
+ conf.file = "/tmp/lsl-log"
20
+
21
+ # Either :light or :dark.
22
+ conf.colours = :dark
23
+ end
24
+
25
+ args = parse_args <<DOCOPT
26
+ Usage:
27
+ #{File.basename __FILE__} [-h] [<dir>]
28
+ #{File.basename __FILE__} -a
29
+
30
+ Options:
31
+ -a, --aaa Option description
32
+ -h, --help Show this message.
33
+ DOCOPT
34
+
35
+ log :warn, "There are four logging levels (:error, :warn, :info and :debug)"
36
+
37
+ dir = args['<dir>'] || '~'
38
+ log :info, "Listing files in #{dir}"
39
+ ls = cmd "ls -l #{dir} | grep -v '^total'",
40
+ show_out: true, # print stdout as the command runs
41
+ show_err: true, # print stderr
42
+ out_level: :debug, # which log level to print the output to
43
+ tag: 'ls -l', # modify the default command tag
44
+ expect: 0, # the expected return status (can be an Array as well)
45
+ raise: true # raise if the command exits with an unexpected code
46
+
47
+ # Iterate through stdout (the same can be done for stderr with .err)
48
+ files = []
49
+ ls.out.lines.each do |line|
50
+ files.push line.split[-1]
51
+ end
52
+ log :info, files.join(', ')
53
+
54
+ # Get information about the process
55
+ log :info, "ls(#{ls.status.pid}) exited with #{ls.status.exitstatus}"
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'scriptster'
4
+ include Scriptster
5
+
6
+ args = parse_args <<DOCOPT
7
+ Usage:
8
+ #{File.basename __FILE__} [-h]
9
+
10
+ Options:
11
+ -h, --help Show this message.
12
+ DOCOPT
13
+
14
+ log :info, "Args received: #{args}"
15
+ ls = cmd 'ls', {show_out: true, out_level: :info}
@@ -1,4 +1,4 @@
1
1
  module Scriptster
2
2
  # The version of this library
3
- VERSION = "0.2.1"
3
+ VERSION = "0.2.2"
4
4
  end
data/lib/scriptster.rb CHANGED
@@ -20,6 +20,8 @@
20
20
  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
21
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
22
 
23
+ require "docopt"
24
+
23
25
  require "scriptster/version"
24
26
  require "scriptster/logger"
25
27
  require "scriptster/shellcmd"
@@ -29,6 +31,8 @@ require "scriptster/configuration"
29
31
  # functions, cmd and log. The module can be used directly or included
30
32
  # in your class.
31
33
  module Scriptster
34
+ Configuration.new.apply
35
+
32
36
  # Pass a message to the logger.
33
37
  #
34
38
  # @see Logger.log
@@ -40,7 +44,7 @@ module Scriptster
40
44
  #
41
45
  # @see .log
42
46
  def log(*args)
43
- Logger::log *args
47
+ Scriptster.log *args
44
48
  end
45
49
 
46
50
  # Execute a shell command
@@ -54,7 +58,7 @@ module Scriptster
54
58
  #
55
59
  # @see .cmd
56
60
  def cmd(*args)
57
- ShellCmd.new *args
61
+ Scriptster.cmd *args
58
62
  end
59
63
 
60
64
  # Use this method to reconfigure the library.
@@ -73,4 +77,30 @@ module Scriptster
73
77
  yield c
74
78
  c.apply
75
79
  end
80
+
81
+ # Process command line arguments using docopt and return
82
+ # the array of options.
83
+ #
84
+ # @param [String] docopt_string The interface spec to be passed to docopt.
85
+ # @return [Array] The processed CLI options, straight from docopt.
86
+ def self.parse_args(docopt_string, argv=nil)
87
+ do_parse_args docopt_string, argv
88
+ end
89
+
90
+ # The same as {Scriptster.parse_args}.
91
+ #
92
+ # @see .parse_args
93
+ def parse_args(docopt_string, argv=nil)
94
+ Scriptster.do_parse_args docopt_string, argv
95
+ end
96
+
97
+ private
98
+ def self.do_parse_args(docopt_string, argv=nil)
99
+ begin
100
+ return Docopt::docopt docopt_string, argv: argv
101
+ rescue Docopt::Exit => e
102
+ STDERR.puts e.message
103
+ exit 1
104
+ end
105
+ end
76
106
  end
data/scriptster.gemspec CHANGED
@@ -7,9 +7,9 @@ Gem::Specification.new do |spec|
7
7
  spec.name = "scriptster"
8
8
  spec.version = Scriptster::VERSION
9
9
  spec.authors = ["Radek Pazdera"]
10
- spec.email = ["radek@pazdera.co.uk"]
10
+ spec.email = ["me@radek.io"]
11
11
  spec.summary = %q{Making your Ruby scipts hip.}
12
- spec.description = %q{A simple library for making your scipts a bit nicer.}
12
+ spec.description = %q{A simple library for making your Ruby scipts nicer.}
13
13
  spec.homepage = "https://github.com/pazdera/scriptster"
14
14
  spec.license = "MIT"
15
15
 
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_runtime_dependency "tco", "~> 0.1"
22
+ spec.add_runtime_dependency "docopt", "~> 0.5"
22
23
 
23
24
  spec.add_development_dependency "bundler", "~> 1.6"
24
25
  spec.add_development_dependency "rake", "~> 10.1"
@@ -106,6 +106,16 @@ describe Scriptster do
106
106
  Scriptster::log :info, "Hello there"
107
107
  expect(@out.string.strip).to match /info Hello there/
108
108
  end
109
+
110
+ it "the instance version works" do
111
+ class Test
112
+ include Scriptster
113
+ end
114
+
115
+ t = Test.new
116
+ t.log :info, "Hello there"
117
+ expect(@out.string.strip).to match /info Hello there/
118
+ end
109
119
  end
110
120
 
111
121
  describe "#cmd" do
@@ -217,5 +227,55 @@ describe Scriptster do
217
227
  Scriptster::cmd "cat #{@dir}/nonexistent.txt", :expect => 10
218
228
  }.to raise_exception RuntimeError
219
229
  end
230
+
231
+ it "the instance version works" do
232
+ class Test
233
+ include Scriptster
234
+ end
235
+
236
+ t = Test.new
237
+ cat = t.cmd "cat #{@dir}/file.txt", :show_out => false
238
+ expect(cat.out).to eq "Multi-line\nFile contents\n"
239
+ end
240
+ end
241
+
242
+ describe "#parse_args" do
243
+ before :all do
244
+ @doc = <<DOCOPT
245
+ Usage: test [-ab]
246
+
247
+ Options:
248
+ -a, --aaa The first option
249
+ -b, --bbb The second option
250
+ DOCOPT
251
+
252
+ @args = Scriptster::parse_args @doc, ['-a']
253
+ end
254
+
255
+ it "returns a Hash" do
256
+ expect(@args).to be_a Hash
257
+ end
258
+
259
+ it "the Hash has two keys" do
260
+ expect(@args.length).to be(2)
261
+ end
262
+
263
+ it "--aaa is true" do
264
+ expect(@args['--aaa']).to be
265
+ end
266
+
267
+ it "--bbb is true" do
268
+ expect(@args['--bbb']).not_to be
269
+ end
270
+
271
+ it "the instance method works" do
272
+ class Test
273
+ include Scriptster
274
+ end
275
+
276
+ t = Test.new
277
+ args = t.parse_args @doc, ['-a']
278
+ expect(args).to be_a Hash
279
+ end
220
280
  end
221
281
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scriptster
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Radek Pazdera
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-06 00:00:00.000000000 Z
11
+ date: 2015-07-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tco
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: docopt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.5'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -66,9 +80,9 @@ dependencies:
66
80
  - - "~>"
67
81
  - !ruby/object:Gem::Version
68
82
  version: '3.1'
69
- description: A simple library for making your scipts a bit nicer.
83
+ description: A simple library for making your Ruby scipts nicer.
70
84
  email:
71
- - radek@pazdera.co.uk
85
+ - me@radek.io
72
86
  executables: []
73
87
  extensions: []
74
88
  extra_rdoc_files: []
@@ -80,8 +94,9 @@ files:
80
94
  - LICENSE.txt
81
95
  - README.md
82
96
  - Rakefile
97
+ - examples/documented-template.rb
98
+ - examples/minimal-template.rb
83
99
  - examples/rainbow.rb
84
- - examples/template.rb
85
100
  - examples/test.rb
86
101
  - gemrc
87
102
  - lib/scriptster.rb
data/examples/template.rb DELETED
@@ -1,45 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- # Docopt docs: http://bit.ly/1LT7Rch
4
- # Scriptster::configure docs: http://bit.ly/1H4X3kR
5
- # Scriptster::cmd docs: http://bit.ly/1LOuVrB
6
-
7
- require 'docopt'
8
- require 'scriptster'
9
-
10
- include Scriptster
11
-
12
- doc = <<DOCOPT
13
- Usage:
14
- #{File.basename __FILE__} [-h]
15
-
16
- Options:
17
- -h, --help Show this message.
18
- DOCOPT
19
-
20
- Scriptster::configure do |conf|
21
- conf.verbosity = :verbose
22
- conf.log_format = '%{timestamp} [%{name}] %{type} %{message}'
23
- end
24
-
25
- begin
26
- args = Docopt::docopt doc
27
- rescue Docopt::Exit => e
28
- log :err, e.message
29
- exit 1
30
- end
31
-
32
- ### >>> Example
33
- log :info, "Listing files:"
34
- ls = cmd 'ls -l | grep -v "^total"',
35
- show_out: true,
36
- out_level: :debug,
37
- tag: 'ls -l'
38
-
39
- files = []
40
- ls.out.lines.each do |line|
41
- files.push line.split[-1]
42
- end
43
-
44
- log :info, files.join(', ')
45
- # <<<