ap_command 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,6 @@
1
1
  # encoding:utf-8
2
2
 
3
+ require 'optparse'
3
4
  require 'json'
4
5
 
5
6
  require 'awesome_print'
@@ -17,20 +18,31 @@ module ApCommand
17
18
  # @param [Array] argv ARGV object.
18
19
  # @return [void]
19
20
  def run(argv)
21
+ opt = OptionParser.new
22
+ opt.on('-v', '--version') {|v| version}
23
+ opt.on('-h', '--help') {|v| usage}
24
+ opt.parse!(argv)
25
+
20
26
  path = argv.first.to_s
21
- version if(path == '--version' or path == '-v')
22
- usage if(path == '--help' or path == '-h')
23
- usage unless(File.exists?(path))
27
+ usage("File NotFound(path=#{path})") unless(File.exists?(path))
24
28
 
25
29
  begin
26
30
  awesome(JSON.parse(File.read(path)))
27
31
  rescue JSON::ParserError => e
28
- puts "Error: #{e.class} message => #{e.message}"
29
- puts
30
- usage
32
+ usage("Error: #{e.class} message => #{e.message}")
31
33
  end
32
34
  end
33
35
 
36
+ # execute application shortcut.
37
+ #
38
+ # @param [Array] argv ARGV object.
39
+ # @return [void]
40
+ def self.invoke(argv)
41
+ self.new.run(argv)
42
+ end
43
+
44
+ private
45
+
34
46
  # call awesome_print.
35
47
  #
36
48
  # @param [Hash] data awesome print object.
@@ -42,9 +54,10 @@ module ApCommand
42
54
  # show usage.
43
55
  #
44
56
  # @return [void]
45
- def usage
46
- puts "usage:"
47
- puts "% #{File.basename($0)} /path/to/file.json"
57
+ def usage(message = nil)
58
+ puts message if message
59
+ puts "" if message
60
+ puts "usage: #{File.basename($0)} /path/to/file.json"
48
61
  puts "json file awsome print for command line tool."
49
62
  exit
50
63
  end
@@ -57,14 +70,6 @@ module ApCommand
57
70
  exit
58
71
  end
59
72
 
60
-
61
- # execute application shortcut.
62
- #
63
- # @param [Array] argv ARGV object.
64
- # @return [void]
65
- def self.invoke(argv)
66
- self.new.run(argv)
67
- end
68
73
  end
69
74
  end
70
75
 
@@ -1,4 +1,4 @@
1
1
  module ApCommand
2
2
  # version
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ap_command
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-04 00:00:00.000000000 Z
12
+ date: 2012-10-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: awesome_print