sanultari-commander 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rvmrc ADDED
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 1.9.3" > .rvmrc
9
+ environment_id="ruby-1.9.3-p286@sancommander"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.16.15 (stable)" # 1.10.1 seams as a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
27
+ \. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
28
+ else
29
+ # If the environment file has not yet been created, use the RVM CLI to select.
30
+ rvm --create "$environment_id" || {
31
+ echo "Failed to create RVM environment '${environment_id}'."
32
+ return 1
33
+ }
34
+ fi
35
+
36
+ # If you use bundler, this might be useful to you:
37
+ # if [[ -s Gemfile ]] && {
38
+ # ! builtin command -v bundle >/dev/null ||
39
+ # builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
40
+ # }
41
+ # then
42
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
43
+ # gem install bundler
44
+ # fi
45
+ # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
46
+ # then
47
+ # bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
48
+ # fi
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem "rspec"
5
+ gem "yard"
6
+ gem "rdoc"
7
+ gem "bundler"
8
+ gem "jeweler"
9
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ git (1.2.5)
6
+ jeweler (1.8.4)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rdoc
11
+ json (1.7.5)
12
+ rake (0.9.2.2)
13
+ rdoc (3.12)
14
+ json (~> 1.4)
15
+ rspec (2.11.0)
16
+ rspec-core (~> 2.11.0)
17
+ rspec-expectations (~> 2.11.0)
18
+ rspec-mocks (~> 2.11.0)
19
+ rspec-core (2.11.1)
20
+ rspec-expectations (2.11.3)
21
+ diff-lcs (~> 1.1.3)
22
+ rspec-mocks (2.11.3)
23
+ yard (0.8.3)
24
+
25
+ PLATFORMS
26
+ ruby
27
+ x86-mingw32
28
+
29
+ DEPENDENCIES
30
+ bundler
31
+ jeweler
32
+ rdoc
33
+ rspec
34
+ yard
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Jeong, Jiung
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = sanultari-commander
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to sanultari-commander
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 Jeong, Jiung. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "sanultari-commander"
18
+ gem.homepage = "http://github.com/sanultari/commander"
19
+ gem.license = "BSD"
20
+ gem.summary = %Q{REPL representable command line tool framework}
21
+ gem.description = %Q{REPL representable command line tool framework.
22
+ its goal is composable, gem distributable, descriptable, expandable CUI tool framework.
23
+ }
24
+ gem.email = "ethernuiel@sanultari.com"
25
+ gem.authors = ["Jeong, Jiung"]
26
+ # dependencies defined in Gemfile
27
+ end
28
+ Jeweler::RubygemsDotOrgTasks.new
29
+
30
+ require 'rspec/core'
31
+ require 'rspec/core/rake_task'
32
+ RSpec::Core::RakeTask.new(:spec) do |spec|
33
+ spec.pattern = FileList['spec/**/*_spec.rb']
34
+ end
35
+
36
+ task :default => :spec
37
+
38
+ require 'yard'
39
+ YARD::Rake::YardocTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/bin/sucmd ADDED
File without changes
@@ -0,0 +1,4 @@
1
+ module SanUltari end
2
+ require 'sanultari/command_parameter'
3
+ require 'sanultari/command_wrapper'
4
+ require 'sanultari/command_descriptor'
@@ -0,0 +1,91 @@
1
+ require 'sanultari/command_wrapper'
2
+
3
+ module SanUltari::CommandDescriptor
4
+ def self.included target
5
+ target.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ @registry = {}
10
+
11
+ def map command, clazz, options = nil
12
+ @registry ||= {}
13
+ @registry[command.to_sym] = SanUltari::CommandWrapper.new command, clazz, nil, options
14
+ end
15
+
16
+ def desc command, description
17
+ end
18
+
19
+ def param command, param, options = nil
20
+ @registry[command.to_sym].add_param param, options
21
+ end
22
+
23
+ def option command, option, options = nil
24
+ end
25
+
26
+ def import clazz, operation = nil
27
+ targets = operation
28
+ targets ||= clazz.available_commands
29
+ unless targets.kind_of? Array
30
+ targets = [targets]
31
+ end
32
+
33
+ targets.each do |cmd|
34
+ command = cmd.to_sym
35
+ wrapper = clazz.get command
36
+ @registry[command] = wrapper
37
+ end
38
+ end
39
+
40
+ def group group_name, clazz, operation = nil
41
+ end
42
+
43
+ def default command
44
+ @default_command = command.to_sym
45
+ end
46
+
47
+ def get command
48
+ @registry[command.to_sym]
49
+ end
50
+
51
+ def available_commands
52
+ @registry.keys
53
+ end
54
+
55
+ def list
56
+
57
+ end
58
+
59
+ def run argv
60
+ selected_command = nil
61
+ options = []
62
+ args = []
63
+ argument_list = argv.clone
64
+ argv.each do |arg|
65
+ if arg.start_with? '-'
66
+ value = argument_list.shift
67
+ args.push value
68
+ options.push value
69
+ next
70
+ end
71
+
72
+ unless @registry.include? arg.to_sym
73
+ args.push argument_list.shift
74
+ next
75
+ end
76
+
77
+ selected_command = @registry[argument_list.shift.to_sym]
78
+ end
79
+
80
+ args += argument_list
81
+ if selected_command == nil
82
+ options.clear
83
+ else
84
+ args -= options
85
+ end
86
+
87
+ selected_command ||= @registry[@default_command] unless @default_command == nil
88
+ selected_command.run(args, options) if selected_command != nil
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,22 @@
1
+ class SanUltari::CommandParameter
2
+ attr_reader :name, :default, :type, :order
3
+
4
+ def initialize name, options = nil
5
+ if options
6
+ @default = options[:default]
7
+ @type = options[:type]
8
+ @type ||= :accessor
9
+ @require = options[:require]
10
+ @require ||= true
11
+ if @type == :parameter
12
+ @order = options[:order]
13
+ @order ||= -1
14
+ end
15
+ end
16
+ @name = name
17
+ end
18
+
19
+ def require?
20
+ @require
21
+ end
22
+ end
@@ -0,0 +1,126 @@
1
+ require 'sanultari/command_parameter'
2
+
3
+ class SanUltari::CommandWrapper
4
+ attr_reader :clazz, :params, :options
5
+
6
+ def initialize name, clazz, params = nil, options = nil
7
+ @name = name
8
+ @clazz = clazz
9
+ @params = params
10
+ @params ||= []
11
+ @options = options
12
+ @options ||= {}
13
+ @freeze = false
14
+ @required_param_count = 0
15
+
16
+ # TODO : deprecate
17
+ @args = []
18
+ end
19
+
20
+ def add_param param_name, options
21
+ param = SanUltari::CommandParameter.new(param_name, options)
22
+ if param.require? && !param.default
23
+ @required_param_count += 1
24
+ end
25
+ @params.push param
26
+ end
27
+
28
+ def params= value
29
+ @clazz = value if !@freeze
30
+ end
31
+
32
+ def clazz= value
33
+ @clazz = value if !@freeze
34
+ end
35
+
36
+ def freeze?
37
+ @freeze
38
+ end
39
+
40
+ def freeze!
41
+ @freeze = true
42
+ end
43
+
44
+ def run args = nil, options = nil
45
+ args ||= []
46
+ options ||= []
47
+ unless args.length >= @required_param_count
48
+ # TODO: standard output change
49
+ puts "this command has #{@required_param_count} parameters at least"
50
+ return
51
+ end
52
+
53
+ runner = @clazz.new
54
+ # TODO options parsing
55
+ options, param_configs = set_values runner, args, options
56
+ set_defaults runner, param_configs
57
+
58
+ if runner.public_method(@name).parameters.length > 0
59
+ runner.public_send @name, *@args
60
+ else
61
+ runner.public_send @name
62
+ end
63
+ end
64
+
65
+ private
66
+ def set_value object, name, value
67
+ object.public_send "#{name}=".to_sym, value
68
+ end
69
+
70
+ def handle_args param_config, value
71
+ if param_config.order < 0
72
+ @args.push value
73
+ else
74
+ if @args[order] == nil
75
+ @args[order] = value
76
+ else
77
+ puts "this command is mis-configured. some arguments have same order."
78
+ end
79
+ end
80
+ end
81
+
82
+ def set_defaults object, param_configs
83
+ param_configs.each do |param_config|
84
+ value = param_config.default
85
+
86
+ if param_config.require? && !value
87
+ puts "required parameter(#{param_config.name}) is missing"
88
+ return
89
+ end
90
+ set_value(object, param_config.name, value) unless value == nil
91
+ param_configs.shift
92
+ end
93
+ end
94
+
95
+ def set_values object, args, options
96
+ param_configs = @params.clone
97
+ args.each do |arg|
98
+ if param_configs.empty?
99
+ index = args.index arg
100
+ options += args[index..-1]
101
+ break
102
+ end
103
+
104
+ current_param_config = param_configs[0]
105
+ if arg.start_with? '-'
106
+ options.push arg
107
+ next
108
+ end
109
+
110
+ # set params
111
+ if current_param_config.type == :parameter
112
+ handle_args current_param_config, arg
113
+ else
114
+ unless object.respond_to? "#{current_param_config.name}=".to_sym
115
+ next
116
+ end
117
+ set_value object, current_param_config.name, arg
118
+ end
119
+ param_configs.shift
120
+ end
121
+
122
+ return options, param_configs
123
+ end
124
+
125
+ private :set_defaults, :set_value, :set_values
126
+ end
data/spec/sample.rb ADDED
@@ -0,0 +1,52 @@
1
+ # coding: utf-8
2
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
+
4
+ class Hello
5
+ attr_accessor :name
6
+
7
+ def hello
8
+ puts "Hello, #{self.name}"
9
+ end
10
+ end
11
+
12
+ class Goodbye
13
+ def bye name
14
+ puts "Goodbye #{name}"
15
+ end
16
+
17
+ def bye2
18
+ puts "잘가라"
19
+ end
20
+ end
21
+
22
+ class AnotherRunner
23
+ include SanUltari::CommandDescriptor
24
+
25
+ #, desc: 'goodbye command'
26
+ map :bye, Goodbye
27
+ param :bye, :name, type: :parameter
28
+
29
+ map :bye2, Goodbye
30
+ end
31
+
32
+ class Runner
33
+ include SanUltari::CommandDescriptor
34
+
35
+ default :hello
36
+
37
+ map :hello, Hello, desc: 'hello command'
38
+ desc :hello, 'hello'
39
+ # type: :attribute is default
40
+ param :hello, :name, require: true, default: 'World'
41
+ # second parameter is optional. second parameter can remove for importing all commands
42
+ import AnotherRunner
43
+ # another importing method. imported commands has group. commands having same name can be distinguish
44
+ # last optional parameter is same functionally with above method
45
+ group :say, AnotherRunner
46
+ end
47
+
48
+ Runner.run ARGV
49
+
50
+
51
+ # ruby sample.rb -v -h --help hello --test tim
52
+ # > Hello, tim
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "SanultariCommander" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'sanultari-commander'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sanultari-commander
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jeong, Jiung
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
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: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: yard
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
+ - !ruby/object:Gem::Dependency
47
+ name: rdoc
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: bundler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: jeweler
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: ! "REPL representable command line tool framework.\n its goal is composable,
95
+ gem distributable, descriptable, expandable CUI tool framework.\n "
96
+ email: ethernuiel@sanultari.com
97
+ executables:
98
+ - sucmd
99
+ extensions: []
100
+ extra_rdoc_files:
101
+ - LICENSE.txt
102
+ - README.rdoc
103
+ files:
104
+ - .document
105
+ - .rspec
106
+ - .rvmrc
107
+ - Gemfile
108
+ - Gemfile.lock
109
+ - LICENSE.txt
110
+ - README.rdoc
111
+ - Rakefile
112
+ - VERSION
113
+ - bin/sucmd
114
+ - lib/sanultari-commander.rb
115
+ - lib/sanultari/command_descriptor.rb
116
+ - lib/sanultari/command_parameter.rb
117
+ - lib/sanultari/command_wrapper.rb
118
+ - spec/sample.rb
119
+ - spec/sanultari-commander_spec.rb
120
+ - spec/spec_helper.rb
121
+ homepage: http://github.com/sanultari/commander
122
+ licenses:
123
+ - BSD
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ segments:
135
+ - 0
136
+ hash: 3489927732922680914
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ! '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubyforge_project:
145
+ rubygems_version: 1.8.24
146
+ signing_key:
147
+ specification_version: 3
148
+ summary: REPL representable command line tool framework
149
+ test_files: []