clactive 0.0.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/clactive.rb +101 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b69ed8d27f8ce0badb2a91681e734f6e2aa3288d
4
+ data.tar.gz: d077607df59787872be4fd017cdb85d78ccb4f75
5
+ SHA512:
6
+ metadata.gz: 3a33bdd9fe39b9a5167753e4bbe66e9f33f5b8d453d442a51435c1ac5bfb245eeb4892f084ea88832210cf5306c87bdb3e2015de3a6f54ad9a986b1f9bfa816f
7
+ data.tar.gz: 8347c572c23cf5ed02deb2750587b65e372652f6e4c0d1ed8d3079caa9502d4948e96a5af7b75d4a4abe17c8d9d35251fdb4c613a3ccfebf5c2cbe64060b9b2b
data/lib/clactive.rb ADDED
@@ -0,0 +1,101 @@
1
+ require 'optparse'
2
+
3
+ module CLActive
4
+ tos = Module.new do
5
+ def [](key)
6
+ self.fetch(key.to_s, nil)
7
+ end
8
+
9
+ def []=(key, value)
10
+ self.store(key.to_s, value)
11
+ end
12
+ end
13
+
14
+ parser = ->(cmd, args) do
15
+ queue = [cmd]
16
+
17
+ until args.empty?
18
+ begin
19
+ subcmds = cmd.parse(args)
20
+ rescue OptionParser::ParseError => e
21
+ puts e.message
22
+ return
23
+ end
24
+
25
+ break unless name = args.shift
26
+
27
+ unless cmd = subcmds[name]
28
+ puts "invalid option: " << name
29
+ return
30
+ end
31
+
32
+ queue << cmd
33
+ end
34
+
35
+ queue.each(&:run)
36
+ end
37
+
38
+ klass_subcmd = Class.new do
39
+ define_method :initialize do
40
+ @options = {}.extend(tos)
41
+ @subcmds = {}.extend(tos)
42
+ @usropts = {}.extend(tos)
43
+ end
44
+
45
+ def parse(args)
46
+ OptionParser.new do |opt|
47
+ @options.each do |key, argv|
48
+ opt.on(*argv) do |val|
49
+ @usropts[key] = val
50
+ end
51
+ end
52
+ end.order!(args)
53
+ @subcmds
54
+ end
55
+
56
+ def run
57
+ @action.call(@usropts) if @action
58
+ end
59
+
60
+ def subcmd(name)
61
+ if block_given?
62
+ yield cmd = self.class.new
63
+ @subcmds[name] = cmd
64
+ end
65
+ self
66
+ end
67
+
68
+ def option(name, *argv)
69
+ @options[name] = argv if argv
70
+ self
71
+ end
72
+
73
+ def action(&block)
74
+ @action = block if block
75
+ self
76
+ end
77
+ end
78
+
79
+ klass_delegate = Class.new do
80
+ attr_reader :cmd
81
+
82
+ define_method :initialize do
83
+ @args = ARGV.dup
84
+ @cmd = klass_subcmd.new
85
+
86
+ at_exit do
87
+ parser.call(@cmd, @args.dup)
88
+ end
89
+ end
90
+ end
91
+
92
+ delegate = klass_delegate.new
93
+
94
+ self.instance_eval do
95
+ [:subcmd, :option, :action].each do |api|
96
+ define_singleton_method api do |*args, &block|
97
+ delegate.cmd.send(api, *args, &block)
98
+ end
99
+ end
100
+ end
101
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: clactive
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Tang Tianyong
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: CLActive help you to build command quickly
14
+ email: tang3w@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/clactive.rb
20
+ homepage: http://tang3w.com
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.0.3
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Command line helper
44
+ test_files: []