simple_commander 0.0.1
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.
- checksums.yaml +7 -0
- data/.byebug_history +19 -0
- data/DEVELOPMENT +15 -0
- data/Gemfile +3 -0
- data/History.rdoc +3 -0
- data/LICENSE +22 -0
- data/Manifest +109 -0
- data/README.md +1 -0
- data/Rakefile +13 -0
- data/bin/simple_commander +16 -0
- data/dir_glob.rb +16 -0
- data/ember_c +66 -0
- data/ideal_spec.rb +23 -0
- data/lib/simple_commander.rb +35 -0
- data/lib/simple_commander/blank.rb +7 -0
- data/lib/simple_commander/command.rb +224 -0
- data/lib/simple_commander/configure.rb +14 -0
- data/lib/simple_commander/core_ext.rb +2 -0
- data/lib/simple_commander/core_ext/array.rb +24 -0
- data/lib/simple_commander/core_ext/object.rb +8 -0
- data/lib/simple_commander/delegates.rb +25 -0
- data/lib/simple_commander/help_formatters.rb +49 -0
- data/lib/simple_commander/help_formatters/base.rb +24 -0
- data/lib/simple_commander/help_formatters/terminal.rb +19 -0
- data/lib/simple_commander/help_formatters/terminal/command_help.erb +35 -0
- data/lib/simple_commander/help_formatters/terminal/help.erb +36 -0
- data/lib/simple_commander/help_formatters/terminal_compact.rb +11 -0
- data/lib/simple_commander/help_formatters/terminal_compact/command_help.erb +27 -0
- data/lib/simple_commander/help_formatters/terminal_compact/help.erb +29 -0
- data/lib/simple_commander/import.rb +5 -0
- data/lib/simple_commander/methods.rb +11 -0
- data/lib/simple_commander/platform.rb +7 -0
- data/lib/simple_commander/runner.rb +477 -0
- data/lib/simple_commander/user_interaction.rb +527 -0
- data/lib/simple_commander/version.rb +3 -0
- data/simple_commander.gemspec +22 -0
- data/todo.yml +24 -0
- metadata +137 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c53e558ab6300e527b954dafaa1077b638383087
|
4
|
+
data.tar.gz: c4ad0caa9f87b07554bf386c6a2b2f6998e33f98
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1f20308f929f4bf542fab1309f10b7e7a4313c04758b9af7cf3edb262fc914b68ec22e945c52b035594e030be0c0d386d4fba509abfbbb0051cf621638ade6dd
|
7
|
+
data.tar.gz: c02f5ddae4504653c890aa693cd8e8a12981c6cedeadf363371ffd67e4abd82ff5f9e210cb5a3c93f38b87a375d462fd856003025da9b5f3aa69eddccf46b53c
|
data/.byebug_history
ADDED
data/DEVELOPMENT
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
To run the development rake tasks, you need bundler installed
|
2
|
+
|
3
|
+
Before you push any changes, run the RSpec suite:
|
4
|
+
|
5
|
+
$ rake spec
|
6
|
+
|
7
|
+
To build a new version of the gem:
|
8
|
+
|
9
|
+
$ rake build
|
10
|
+
|
11
|
+
To push the new version to Rubygems:
|
12
|
+
|
13
|
+
$ rake release
|
14
|
+
|
15
|
+
(http://rubygems.org/gems/simple_commander)
|
data/Gemfile
ADDED
data/History.rdoc
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Marcell Monteiro Cruz <marcellmonteiro.com>
|
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 included
|
14
|
+
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 NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
20
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
21
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
22
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
bin
|
2
|
+
bin/simple_commander
|
3
|
+
bin/simple_commander
|
4
|
+
DEVELOPMENT
|
5
|
+
dir_glob.rb
|
6
|
+
ember_c
|
7
|
+
Gemfile
|
8
|
+
History.rdoc
|
9
|
+
ideal_spec.rb
|
10
|
+
lib
|
11
|
+
lib/simple_commander
|
12
|
+
lib/simple_commander/blank.rb
|
13
|
+
lib/simple_commander/command.rb
|
14
|
+
lib/simple_commander/configure.rb
|
15
|
+
lib/simple_commander/core_ext
|
16
|
+
lib/simple_commander/core_ext/array.rb
|
17
|
+
lib/simple_commander/core_ext/object.rb
|
18
|
+
lib/simple_commander/core_ext.rb
|
19
|
+
lib/simple_commander/delegates.rb
|
20
|
+
lib/simple_commander/help_formatters
|
21
|
+
lib/simple_commander/help_formatters/base.rb
|
22
|
+
lib/simple_commander/help_formatters/terminal
|
23
|
+
lib/simple_commander/help_formatters/terminal/command_help.erb
|
24
|
+
lib/simple_commander/help_formatters/terminal/help.erb
|
25
|
+
lib/simple_commander/help_formatters/terminal.rb
|
26
|
+
lib/simple_commander/help_formatters/terminal_compact
|
27
|
+
lib/simple_commander/help_formatters/terminal_compact/command_help.erb
|
28
|
+
lib/simple_commander/help_formatters/terminal_compact/help.erb
|
29
|
+
lib/simple_commander/help_formatters/terminal_compact.rb
|
30
|
+
lib/simple_commander/help_formatters.rb
|
31
|
+
lib/simple_commander/import.rb
|
32
|
+
lib/simple_commander/methods.rb
|
33
|
+
lib/simple_commander/platform.rb
|
34
|
+
lib/simple_commander/runner.rb
|
35
|
+
lib/simple_commander/user_interaction.rb
|
36
|
+
lib/simple_commander/version.rb
|
37
|
+
lib/simple_commander.rb
|
38
|
+
lib/simple_commander
|
39
|
+
lib/simple_commander/blank.rb
|
40
|
+
lib/simple_commander/command.rb
|
41
|
+
lib/simple_commander/configure.rb
|
42
|
+
lib/simple_commander/core_ext
|
43
|
+
lib/simple_commander/core_ext/array.rb
|
44
|
+
lib/simple_commander/core_ext/object.rb
|
45
|
+
lib/simple_commander/core_ext.rb
|
46
|
+
lib/simple_commander/delegates.rb
|
47
|
+
lib/simple_commander/help_formatters
|
48
|
+
lib/simple_commander/help_formatters/base.rb
|
49
|
+
lib/simple_commander/help_formatters/terminal
|
50
|
+
lib/simple_commander/help_formatters/terminal/command_help.erb
|
51
|
+
lib/simple_commander/help_formatters/terminal/help.erb
|
52
|
+
lib/simple_commander/help_formatters/terminal.rb
|
53
|
+
lib/simple_commander/help_formatters/terminal_compact
|
54
|
+
lib/simple_commander/help_formatters/terminal_compact/command_help.erb
|
55
|
+
lib/simple_commander/help_formatters/terminal_compact/help.erb
|
56
|
+
lib/simple_commander/help_formatters/terminal_compact.rb
|
57
|
+
lib/simple_commander/help_formatters.rb
|
58
|
+
lib/simple_commander/import.rb
|
59
|
+
lib/simple_commander/methods.rb
|
60
|
+
lib/simple_commander/platform.rb
|
61
|
+
lib/simple_commander/runner.rb
|
62
|
+
lib/simple_commander/user_interaction.rb
|
63
|
+
lib/simple_commander/version.rb
|
64
|
+
lib/simple_commander/blank.rb
|
65
|
+
lib/simple_commander/command.rb
|
66
|
+
lib/simple_commander/configure.rb
|
67
|
+
lib/simple_commander/core_ext
|
68
|
+
lib/simple_commander/core_ext/array.rb
|
69
|
+
lib/simple_commander/core_ext/object.rb
|
70
|
+
lib/simple_commander/core_ext/array.rb
|
71
|
+
lib/simple_commander/core_ext/object.rb
|
72
|
+
lib/simple_commander/core_ext.rb
|
73
|
+
lib/simple_commander/delegates.rb
|
74
|
+
lib/simple_commander/help_formatters
|
75
|
+
lib/simple_commander/help_formatters/base.rb
|
76
|
+
lib/simple_commander/help_formatters/terminal
|
77
|
+
lib/simple_commander/help_formatters/terminal/command_help.erb
|
78
|
+
lib/simple_commander/help_formatters/terminal/help.erb
|
79
|
+
lib/simple_commander/help_formatters/terminal.rb
|
80
|
+
lib/simple_commander/help_formatters/terminal_compact
|
81
|
+
lib/simple_commander/help_formatters/terminal_compact/command_help.erb
|
82
|
+
lib/simple_commander/help_formatters/terminal_compact/help.erb
|
83
|
+
lib/simple_commander/help_formatters/terminal_compact.rb
|
84
|
+
lib/simple_commander/help_formatters/base.rb
|
85
|
+
lib/simple_commander/help_formatters/terminal
|
86
|
+
lib/simple_commander/help_formatters/terminal/command_help.erb
|
87
|
+
lib/simple_commander/help_formatters/terminal/help.erb
|
88
|
+
lib/simple_commander/help_formatters/terminal/command_help.erb
|
89
|
+
lib/simple_commander/help_formatters/terminal/help.erb
|
90
|
+
lib/simple_commander/help_formatters/terminal.rb
|
91
|
+
lib/simple_commander/help_formatters/terminal_compact
|
92
|
+
lib/simple_commander/help_formatters/terminal_compact/command_help.erb
|
93
|
+
lib/simple_commander/help_formatters/terminal_compact/help.erb
|
94
|
+
lib/simple_commander/help_formatters/terminal_compact/command_help.erb
|
95
|
+
lib/simple_commander/help_formatters/terminal_compact/help.erb
|
96
|
+
lib/simple_commander/help_formatters/terminal_compact.rb
|
97
|
+
lib/simple_commander/help_formatters.rb
|
98
|
+
lib/simple_commander/import.rb
|
99
|
+
lib/simple_commander/methods.rb
|
100
|
+
lib/simple_commander/platform.rb
|
101
|
+
lib/simple_commander/runner.rb
|
102
|
+
lib/simple_commander/user_interaction.rb
|
103
|
+
lib/simple_commander/version.rb
|
104
|
+
lib/simple_commander.rb
|
105
|
+
LICENSE
|
106
|
+
README.md
|
107
|
+
simple_commander.gemspec
|
108
|
+
spec
|
109
|
+
todo.yml
|
data/README.md
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Simple Commander
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
require 'rubocop/rake_task'
|
4
|
+
|
5
|
+
desc 'Run spec'
|
6
|
+
RSpec::Core::RakeTask.new do |t|
|
7
|
+
t.verboce = false
|
8
|
+
t.rspec_opts = '--color --order random'
|
9
|
+
end
|
10
|
+
|
11
|
+
RuboCop::RakeTask.new
|
12
|
+
|
13
|
+
task default: [:spec, :rubocop]
|
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'simple_commander/import'
|
5
|
+
|
6
|
+
program :name, 'scommander'
|
7
|
+
program :version, Commander::VERSION
|
8
|
+
program :description, 'Simple Commander utility program.'
|
9
|
+
|
10
|
+
command :init do
|
11
|
+
action do
|
12
|
+
# add init code here
|
13
|
+
puts "init!"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
data/dir_glob.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'byebug'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
FileUtils.rm('Manifest')
|
5
|
+
|
6
|
+
def glob(path)
|
7
|
+
return if !File.directory?(path) && path != "**"
|
8
|
+
path = "#{path}/*"
|
9
|
+
Dir.glob(path) do |file|
|
10
|
+
File.open("Manifest", 'a') {|f| f.write("#{file}\n") }
|
11
|
+
glob(file)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
glob("**")
|
16
|
+
|
data/ember_c
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'byebug'
|
3
|
+
require 'simple_commander/import'
|
4
|
+
|
5
|
+
|
6
|
+
program :name, 'ember-c'
|
7
|
+
program :version, '0.0.1'
|
8
|
+
program :description, 'implementing ember with modified commander'
|
9
|
+
|
10
|
+
command :new do
|
11
|
+
syntax = 'ember-c new <name>'
|
12
|
+
summary = 'create a new ember project'
|
13
|
+
description = 'Create a new ember project'
|
14
|
+
action do |args, options|
|
15
|
+
puts args[0]
|
16
|
+
global
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
command :this do
|
22
|
+
command :is do
|
23
|
+
command :ok do
|
24
|
+
command :dont do
|
25
|
+
command :worry do
|
26
|
+
action do
|
27
|
+
puts "this is ok dont worry"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
command :also do
|
33
|
+
action do
|
34
|
+
puts "this is ok also"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
command :parent do
|
42
|
+
command :child do
|
43
|
+
action do |args, options|
|
44
|
+
puts 'nested child'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
command 'do this' do
|
50
|
+
action do |args, options|
|
51
|
+
puts "do this!"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# new spec
|
56
|
+
#command :deploy do
|
57
|
+
# helpers :io, :http
|
58
|
+
#
|
59
|
+
# command :heroku do
|
60
|
+
# actions_ext './deploy/heroku'
|
61
|
+
# end
|
62
|
+
#
|
63
|
+
# command :s3 do
|
64
|
+
# actions_ext './deploy/s3'
|
65
|
+
# end
|
66
|
+
#end
|
data/ideal_spec.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'magni'
|
2
|
+
require 'magni_something'
|
3
|
+
|
4
|
+
cli :magni, '--version' do
|
5
|
+
helpers :io, :http
|
6
|
+
|
7
|
+
command :create, '-v' do
|
8
|
+
syntax = "commander init [option]"
|
9
|
+
command :cli, :name, '-a' do
|
10
|
+
arg[:name]
|
11
|
+
opts[:a]
|
12
|
+
task 'create folders' do
|
13
|
+
# do stuff here
|
14
|
+
end
|
15
|
+
|
16
|
+
task 'copy files' do
|
17
|
+
# do stuff here
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
include magni_something
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2016 Marcell Monteiro Cruz <marcellmonteiro.com>
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
# a copy of this software and associated documentation files (the
|
6
|
+
# "Software"), to deal in the Software without restriction, including
|
7
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
# the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be
|
13
|
+
# included in all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
#++
|
23
|
+
|
24
|
+
require 'highline/import'
|
25
|
+
require 'simple_commander/version'
|
26
|
+
require 'simple_commander/blank'
|
27
|
+
require 'simple_commander/user_interaction'
|
28
|
+
require 'simple_commander/core_ext'
|
29
|
+
require 'simple_commander/runner.rb'
|
30
|
+
require 'simple_commander/command'
|
31
|
+
require 'simple_commander/help_formatters'
|
32
|
+
require 'simple_commander/platform'
|
33
|
+
require 'simple_commander/delegates'
|
34
|
+
require 'simple_commander/methods'
|
35
|
+
require 'simple_commander/configure'
|
@@ -0,0 +1,224 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
|
3
|
+
module Commander
|
4
|
+
class Command
|
5
|
+
attr_accessor :name, :examples, :syntax, :description, :super_self
|
6
|
+
attr_accessor :summary, :proxy_options, :options
|
7
|
+
|
8
|
+
##
|
9
|
+
# Options struct.
|
10
|
+
|
11
|
+
class Options
|
12
|
+
include Blank
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
@table = {}
|
16
|
+
end
|
17
|
+
|
18
|
+
def __hash__
|
19
|
+
@table
|
20
|
+
end
|
21
|
+
|
22
|
+
def method_missing(meth, *args)
|
23
|
+
meth.to_s =~ /=$/ ? @table[meth.to_s.chop.to_sym] = args.first : @table[meth]
|
24
|
+
end
|
25
|
+
|
26
|
+
def default(defaults = {})
|
27
|
+
@table = defaults.merge! @table
|
28
|
+
end
|
29
|
+
|
30
|
+
def inspect
|
31
|
+
"<Commander::Command::Options #{ __hash__.map { |k, v| "#{k}=#{v.inspect}" }.join(', ') }>"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# when a method is missing inside a action block
|
37
|
+
# command will call the method in the runner instance context
|
38
|
+
|
39
|
+
def method_missing(method, *args, &block)
|
40
|
+
@super_self.send method, *args, &block
|
41
|
+
end
|
42
|
+
|
43
|
+
##
|
44
|
+
# Initialize new command with specified _name_.
|
45
|
+
|
46
|
+
def initialize(name)
|
47
|
+
@name, @examples, @when_called = name.to_s, [], []
|
48
|
+
@options, @proxy_options = [], []
|
49
|
+
end
|
50
|
+
|
51
|
+
##
|
52
|
+
# return the block if command have a when_called block
|
53
|
+
|
54
|
+
def has_no_action?
|
55
|
+
if @when_called.empty? then true else false end
|
56
|
+
end
|
57
|
+
|
58
|
+
##
|
59
|
+
# Add a usage example for this command.
|
60
|
+
#
|
61
|
+
# Usage examples are later displayed in help documentation
|
62
|
+
# created by the help formatters.
|
63
|
+
#
|
64
|
+
# === Examples
|
65
|
+
#
|
66
|
+
# command :something do |c|
|
67
|
+
# c.example "Should do something", "my_command something"
|
68
|
+
# end
|
69
|
+
#
|
70
|
+
|
71
|
+
def example(description, command)
|
72
|
+
@examples << [description, command]
|
73
|
+
end
|
74
|
+
|
75
|
+
##
|
76
|
+
# Add an option.
|
77
|
+
#
|
78
|
+
# Options are parsed via OptionParser so view it
|
79
|
+
# for additional usage documentation. A block may optionally be
|
80
|
+
# passed to handle the option, otherwise the _options_ struct seen below
|
81
|
+
# contains the results of this option. This handles common formats such as:
|
82
|
+
#
|
83
|
+
# -h, --help options.help # => bool
|
84
|
+
# --[no-]feature options.feature # => bool
|
85
|
+
# --large-switch options.large_switch # => bool
|
86
|
+
# --file FILE options.file # => file passed
|
87
|
+
# --list WORDS options.list # => array
|
88
|
+
# --date [DATE] options.date # => date or nil when optional argument not set
|
89
|
+
#
|
90
|
+
# === Examples
|
91
|
+
#
|
92
|
+
# command :something do |c|
|
93
|
+
# c.option '--recursive', 'Do something recursively'
|
94
|
+
# c.option '--file FILE', 'Specify a file'
|
95
|
+
# c.option('--info', 'Display info') { puts "handle with block" }
|
96
|
+
# c.option '--[no-]feature', 'With or without feature'
|
97
|
+
# c.option '--list FILES', Array, 'List the files specified'
|
98
|
+
#
|
99
|
+
# c.when_called do |args, options|
|
100
|
+
# do_something_recursively if options.recursive
|
101
|
+
# do_something_with_file options.file if options.file
|
102
|
+
# end
|
103
|
+
# end
|
104
|
+
#
|
105
|
+
# === Help Formatters
|
106
|
+
#
|
107
|
+
# This method also parses the arguments passed in order to determine
|
108
|
+
# which were switches, and which were descriptions for the
|
109
|
+
# option which can later be used within help formatters
|
110
|
+
# using option[:switches] and option[:description].
|
111
|
+
#
|
112
|
+
# === Input Parsing
|
113
|
+
#
|
114
|
+
# Since Commander utilizes OptionParser you can pre-parse and evaluate
|
115
|
+
# option arguments. Simply require 'optparse/time', or 'optparse/date', as these
|
116
|
+
# objects must respond to #parse.
|
117
|
+
#
|
118
|
+
# c.option '--time TIME', Time
|
119
|
+
# c.option '--date [DATE]', Date
|
120
|
+
#
|
121
|
+
|
122
|
+
def option(*args, &block)
|
123
|
+
switches, description = Runner.separate_switches_from_description(*args)
|
124
|
+
proc = block || option_proc(switches)
|
125
|
+
@options << {
|
126
|
+
args: args,
|
127
|
+
proc: proc,
|
128
|
+
switches: switches,
|
129
|
+
description: description,
|
130
|
+
}
|
131
|
+
end
|
132
|
+
|
133
|
+
##
|
134
|
+
# Handle execution of command. The handler may be a class,
|
135
|
+
# object, or block (see examples below).
|
136
|
+
#
|
137
|
+
# === Examples
|
138
|
+
#
|
139
|
+
# # Simple block handling
|
140
|
+
# c.when_called do |args, options|
|
141
|
+
# # do something
|
142
|
+
# end
|
143
|
+
#
|
144
|
+
# # Create inst of Something and pass args / options
|
145
|
+
# c.when_called MyLib::Command::Something
|
146
|
+
#
|
147
|
+
# # Create inst of Something and use arbitrary method
|
148
|
+
# c.when_called MyLib::Command::Something, :some_method
|
149
|
+
#
|
150
|
+
# # Pass an object to handle callback (requires method symbol)
|
151
|
+
# c.when_called SomeObject, :some_method
|
152
|
+
#
|
153
|
+
def when_called(*args, &block)
|
154
|
+
fail ArgumentError, 'must pass an object, class, or block.' if args.empty? && !block
|
155
|
+
@when_called = block ? [block] : args
|
156
|
+
end
|
157
|
+
alias_method :action, :when_called
|
158
|
+
|
159
|
+
##
|
160
|
+
# Run the command with _args_.
|
161
|
+
#
|
162
|
+
# * parses options, call option blocks
|
163
|
+
# * invokes when_called proc
|
164
|
+
#
|
165
|
+
|
166
|
+
def run(*args)
|
167
|
+
call parse_options_and_call_procs(*args)
|
168
|
+
end
|
169
|
+
|
170
|
+
#:stopdoc:
|
171
|
+
|
172
|
+
##
|
173
|
+
# Parses options and calls associated procs,
|
174
|
+
# returning the arguments remaining.
|
175
|
+
|
176
|
+
def parse_options_and_call_procs(*args)
|
177
|
+
return args if args.empty?
|
178
|
+
@options.each_with_object(OptionParser.new) do |option, opts|
|
179
|
+
opts.on(*option[:args], &option[:proc])
|
180
|
+
opts
|
181
|
+
end.parse! args
|
182
|
+
end
|
183
|
+
|
184
|
+
##
|
185
|
+
# Call the commands when_called block with _args_.
|
186
|
+
|
187
|
+
def call(args = [])
|
188
|
+
object = @when_called.shift
|
189
|
+
meth = @when_called.shift || :call
|
190
|
+
options = proxy_option_struct
|
191
|
+
case object
|
192
|
+
when Proc then object.call(args, options)
|
193
|
+
when Class then meth != :call ? object.new.send(meth, args, options) : object.new(args, options)
|
194
|
+
else object.send(meth, args, options) if object
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
##
|
199
|
+
# Creates an Options instance populated with the option values
|
200
|
+
# collected by the #option_proc.
|
201
|
+
|
202
|
+
def proxy_option_struct
|
203
|
+
proxy_options.each_with_object(Options.new) do |(option, value), options|
|
204
|
+
# options that are present will evaluate to true
|
205
|
+
value = true if value.nil?
|
206
|
+
options.__send__ :"#{option}=", value
|
207
|
+
options
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
##
|
212
|
+
# Option proxy proc used when a block is not explicitly passed
|
213
|
+
# via the #option method. This allows commander to auto-populate
|
214
|
+
# and work with option values.
|
215
|
+
|
216
|
+
def option_proc(switches)
|
217
|
+
->(value) { proxy_options << [Runner.switch_to_sym(switches.last), value] }
|
218
|
+
end
|
219
|
+
|
220
|
+
def inspect
|
221
|
+
"<Commander::Command:#{name}>"
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|