salish 0.0.2.dev

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.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in salish.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2016 shitake
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # Salish
2
+
3
+ 一个类似 `commander` 的ruby的命令行工具。用于快速生成命令。支持多级子命令创建。
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'salish'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install salish
18
+
19
+ ## Usage
20
+ #### 载入库并创建命令
21
+ ````
22
+ require 'salish'
23
+ cmd_obj = Salish.cmd
24
+ ````
25
+
26
+ #### 设置版本信息
27
+ ````
28
+ cmd_obj.version('0.0.1')
29
+ ````
30
+ #### 编写帮助信息(可选)
31
+ ````
32
+ cmd_obj.help do |help|
33
+ #设置帮助的标题
34
+ help.title = ''
35
+ #设置帮助的主体
36
+ help.body = ''
37
+ #设置帮助的尾部
38
+ help.footer = ''
39
+ ````
40
+ 子命令及参数的帮助会自动生成。关于帮助输出的顺序是:
41
+
42
+ title -> body -> 子命令和参数 -> footer
43
+
44
+ #### 设置option(可选、任意数目)
45
+ ````
46
+ cmd_obj.option(options, info, <param_info>, <&block>)
47
+ ````
48
+ options(`string`):描述短参数及长参数,会出现在自动生成的帮助信息里。
49
+ info(`string`):参数的描述信息。会出现在自动生成的帮助信息里。
50
+ param_info(`string`):固定参数描述。格式为: `param__type:param__name` ,多个参数用 `,` 分开。
51
+ block(`proc`):该参数执行的相应动作。如果有固定参数,则可以通过 `do |param| ... end` 的形式来获得。`param` 为一个哈希表,键为 `param_info` 里的 `param__name`,类型为 `symbol`。
52
+
53
+ #### 设置子命令(可选、任意数目)
54
+ ````
55
+ cmd_obj.command(cmd_name, info, version)
56
+ ````
57
+ cmd_name(`string`):子命令名
58
+ info(`string`):子命令说明,该说明会出现在父命令自动生成的帮助信息里。
59
+ version(`string`):可选,不填写时,版本信息等同于父命令。
60
+
61
+ #### 解析命令
62
+ ````
63
+ cmd_obj.parse(ARGV)
64
+ ````
65
+ 请确保该方法最后调用。处于该方法之后添加的命令/参数等无效。
66
+
67
+ ### 注意事项
68
+ 1. 对于 `version`/`option`/`help` 这些方法来说,最后的返回值均为对象本身。而 `command` 会返回创建的子命令对象。
69
+ 2. 如果 `version` 的调用在 `command` 之后,创建子命令时如果不传入版本参数,这时版本信息会出错(父命令的版本信息为空字符串)。
70
+
71
+ ## Contributing
72
+
73
+ 1. Fork it
74
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
75
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
76
+ 4. Push to the branch (`git push origin my-new-feature`)
77
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/salish.rb ADDED
@@ -0,0 +1,17 @@
1
+ #encoding: utf-8
2
+ #author: shitake
3
+ #data: 16-3-26
4
+
5
+ module Salish
6
+ require 'salish/help'
7
+ require 'salish/version'
8
+ require 'salish/command'
9
+ class << self
10
+
11
+ def version_str?(ver_str)
12
+ /\d*\.\d*\.\d*/ === ver_str
13
+ end
14
+
15
+ end
16
+
17
+ end
@@ -0,0 +1,151 @@
1
+ #encoding: utf-8
2
+ #author: shitake
3
+ #data: 16-3-26
4
+
5
+ class Salish::Command
6
+
7
+ attr_reader :name
8
+ attr_reader :commands
9
+ attr_reader :options
10
+ attr_accessor :parent
11
+ attr_accessor :description
12
+
13
+ def initialize(name = nil)
14
+ raise "Error: Command Name Error(#{name})!" unless name
15
+ @name = name
16
+ @help = Salish::Help.new(self)
17
+ @parent = self
18
+ @version = ''
19
+ @options = []
20
+ @commands = []
21
+ @description = ''
22
+ help_opt = {
23
+ :short => '-h',
24
+ :long => '--help',
25
+ :params => nil,
26
+ :description => 'Print help info',
27
+ :callback => lambda{ puts @help.to_string }
28
+ }
29
+ @options.push(help_opt)
30
+ end
31
+
32
+ def version(ver_str = nil)
33
+ return @version unless ver_str
34
+ raise "Error: Version Error(#{ver_str})!" unless Salish.version_str?(ver_str)
35
+ @version = ver_str
36
+ version_opt = {
37
+ :short => '-v',
38
+ :long => '--version',
39
+ :params => nil,
40
+ :description => 'Print version info',
41
+ :callback => lambda{ puts @version }
42
+ }
43
+ @options.push(version_opt)
44
+ self
45
+ end
46
+
47
+ def option(flags, description, params = nil, &callback)
48
+ list = []
49
+ flags.split(',').each{|flag| list << flag.strip}
50
+ flags_short = list[0]
51
+ flags_long = list[1]
52
+ if params
53
+ list = []
54
+ params.split(',').each do |param|
55
+ param = param.split(':')
56
+ if param.size == 2
57
+ type = param[0].strip
58
+ name = param[1].strip
59
+ else
60
+ type = 'string'
61
+ name = param[0].strip
62
+ end
63
+ list << {:type => type, :name => name}
64
+ end
65
+ else
66
+ list = nil
67
+ end
68
+ opt = {
69
+ :short => flags_short,
70
+ :long => flags_long,
71
+ :params => list,
72
+ :description => description,
73
+ :callback => callback
74
+ }
75
+ @options.push(opt)
76
+ self
77
+ end
78
+
79
+ def command(name, description, version = nil)
80
+ cmd = Salish::Command.new(name)
81
+ cmd.parent = self
82
+ cmd.description = description
83
+ cmd.version(version || @version)
84
+ @commands.push(cmd)
85
+ cmd
86
+ end
87
+
88
+ def help(&block)
89
+ yield @help
90
+ self
91
+ end
92
+
93
+ def parse(arvg)
94
+ opt = arvg[0]
95
+ param = arvg[1, arvg.size] || []
96
+ return call_opt('long', opt, param) if /--.*/ === opt
97
+ return call_opt('short', opt, param) if /-.*/ === opt
98
+ call_cmd(opt, param)
99
+ end
100
+
101
+ private
102
+ def call_opt(type, option, params)
103
+ @options.each do |opt|
104
+ opt_name = (type == 'long' ? opt[:long] : opt[:short])
105
+ if option == opt_name
106
+ if opt[:params]
107
+ return opt[:callback].call(opt_set_params(opt, params))
108
+ else
109
+
110
+ return opt[:callback].call
111
+ end
112
+ end
113
+ end
114
+ puts "ERROR: option error (#{option}) ! You can use -h or --help to get help info."
115
+ exit
116
+ end
117
+
118
+ def opt_set_params(opt, params)
119
+ param = {}
120
+ unless opt[:params].size <= params.size
121
+ puts "ERROR: wrong number of arguments (#{params.size} for #{opt[:params].size}) .\n#{opt[:short]}\s#{opt[:long]}\t#{opt[:description]}"
122
+ exit
123
+ end
124
+ opt[:params].each_with_index do |opt_par, i|
125
+ case opt_par[:type]
126
+ when 'string'
127
+ param[opt_par[:name].to_sym] = params[i]
128
+ when 'integer'
129
+ param[opt_par[:name].to_sym] = params[i].to_i
130
+ when 'float'
131
+ param[opt_par[:name].to_sym] = params[i].to_f
132
+ when 'symbol'
133
+ param[opt_par[:name].to_sym] = params[i].to_sym
134
+ else
135
+ raise "Error: Param Type Error(#{opt_par[:name]}:#{opt_par[:type]})!"
136
+ end
137
+ end
138
+ param
139
+ end
140
+
141
+ def call_cmd(command, arvg)
142
+ @commands.each do |cmd|
143
+ if command == cmd.name
144
+ return cmd.parse(arvg)
145
+ end
146
+ end
147
+ puts "ERROR: command not find (#{command}) ! You can use -h or --help to get help info."
148
+ exit
149
+ end
150
+
151
+ end
@@ -0,0 +1,60 @@
1
+ #encoding: utf-8
2
+ #author: shitake
3
+ #data: 16-3-27
4
+
5
+ class Salish::Help
6
+
7
+ attr_writer :title
8
+ attr_writer :body
9
+ attr_writer :footer
10
+
11
+ def initialize(obj)
12
+ @obj = obj
13
+ @title = @obj.name
14
+ @body = ''
15
+ @footer = ''
16
+ end
17
+
18
+ def title
19
+ @title != '' ? "#{@title}\n\n" : ''
20
+ end
21
+
22
+ def body
23
+ @body != '' ? "#{@body}\n\n" : ''
24
+ end
25
+
26
+ def footer
27
+ @footer != '' ? "#{@footer}\n\n" : ''
28
+ end
29
+
30
+ def parameters
31
+
32
+ if @obj.commands != []
33
+ obj = @obj.commands.max_by{ |o| o.name.size }
34
+ @commands = "command:\n"
35
+ @obj.commands.each do |cmd|
36
+ space = "\s" * (obj.name.size + 6 - cmd.name.size)
37
+ @commands += "\s\s#{cmd.name}#{space}\s#{cmd.description}\n"
38
+ end
39
+ else
40
+ @commands = ''
41
+ end
42
+ if @obj.options != []
43
+ obj = @obj.options.max_by{ |o| (o[:short] + o[:long]).size }
44
+ size = (obj[:short] + obj[:long]).size
45
+ @options = "option:\n"
46
+ @obj.options.each do |opt|
47
+ space = "\s" * (size + 10 - ("\s\s" + opt[:short] + "\s" + opt[:long]).size)
48
+ @options += "\s\s#{opt[:short]}\s#{opt[:long]}#{space}\s#{opt[:description]}\n"
49
+ end
50
+ else
51
+ @options = ''
52
+ end
53
+ @commands + @options != '' ? "#{@commands}\n#{@options}\n" : ''
54
+ end
55
+
56
+ def to_string
57
+ title + body + parameters + footer
58
+ end
59
+
60
+ end
@@ -0,0 +1,4 @@
1
+ module Salish
2
+ VERSION = '0.0.2.dev'
3
+ end
4
+
data/salish.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'salish/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "salish"
8
+ spec.version = Salish::VERSION
9
+ spec.authors = ["shitake"]
10
+ spec.email = ["z1522716486@hotmail.com"]
11
+ spec.description = %q{Ruby command-line tool kit}
12
+ spec.summary = %q{A Ruby command-line tool kit. Support for multiple subcommands created.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
24
+
data/test/cmd_test.rb ADDED
@@ -0,0 +1,36 @@
1
+ #encoding: utf-8
2
+ #author: shitake
3
+ #data: 16-3-26
4
+
5
+ require "../lib/salish.rb"
6
+
7
+ cmd = Salish::Command.new('test')
8
+ cmd.version('1.0.1')
9
+
10
+ cmd.help do |h|
11
+ h.body = '这是一个控制台命令的测试命令。你可以通过-h或者--help来查看帮助!'
12
+ h.footer = \
13
+ "用例:\n"\
14
+ " 载入模块:\n\trequire 'salish'\n"\
15
+ " 创建根命令:\n\tcmd = Salish.cmd\n"\
16
+ " 设置版本:\n\tcmd.version('1.1.1')\n"\
17
+ " 添加参数:\n\tcmd.option('-a, --add', '添加文件', 'string:path'){|param| puts '文件路径',param[:path] }\n"
18
+ end
19
+
20
+ cmd.option('-a, --add', '<s:path> 从指定路径载入文件。', 'string:path, string:file_name') do |param|
21
+ puts '文件路径:',param
22
+ end
23
+
24
+ cmd.option('-l, --list', '列出现有文件'){ puts "index.html\s\sconf.txt" }
25
+
26
+ push = cmd.command('push', '上传文件')
27
+ push.option('-a, --all', '是否上传全部') { puts '所有文件都已上传!' }
28
+
29
+ #短参数测试
30
+ cmd.parse(%w(-v))
31
+ #长参数测试
32
+ cmd.parse(%w'--add ssss sddsds')
33
+ #帮助信息输出测试
34
+ cmd.parse(%w(-h))
35
+ #子命令测试
36
+ cmd.parse(%w'push -h')
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: salish
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2.dev
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - shitake
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-03-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Ruby command-line tool kit
47
+ email:
48
+ - z1522716486@hotmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .idea/.name
55
+ - .idea/encodings.xml
56
+ - .idea/misc.xml
57
+ - .idea/modules.xml
58
+ - .idea/salish.iml
59
+ - .idea/vcs.xml
60
+ - .idea/workspace.xml
61
+ - Gemfile
62
+ - LICENSE.txt
63
+ - README.md
64
+ - Rakefile
65
+ - lib/salish.rb
66
+ - lib/salish/command.rb
67
+ - lib/salish/help.rb
68
+ - lib/salish/version.rb
69
+ - salish.gemspec
70
+ - test/cmd_test.rb
71
+ homepage: ''
72
+ licenses:
73
+ - MIT
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>'
88
+ - !ruby/object:Gem::Version
89
+ version: 1.3.1
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 1.8.23
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: A Ruby command-line tool kit. Support for multiple subcommands created.
96
+ test_files:
97
+ - test/cmd_test.rb