Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commands from anywhere #109

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions features/todo.feature
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Feature: The todo app has a nice user interface
list - List things, such as tasks or contexts
ls - LS things, such as tasks or contexts
second -
third -
"""
Examples:
| help |
Expand Down Expand Up @@ -118,6 +119,7 @@ Feature: The todo app has a nice user interface
create, new - Create a new task or context
list - List things, such as tasks or contexts
ls - LS things, such as tasks or contexts
third -
first -
second -
chained -
Expand Down
28 changes: 19 additions & 9 deletions lib/gli/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@ module App
#
# path:: a path relative to somewhere in the <code>LOAD_PATH</code>, from which all <code>.rb</code> files will be required.
def commands_from(path)
$LOAD_PATH.each do |load_path|
commands_path = File.join(load_path,path)
if File.exists? commands_path
Dir.entries(commands_path).sort.each do |entry|
file = File.join(commands_path,entry)
if file =~ /\.rb$/
require file
end
end
if Pathname.new(path).absolute? and File.exists?(path)
load_commands(path)
else
$LOAD_PATH.each do |load_path|
commands_path = File.join(load_path,path)
load_commands(commands_path)
end
end
end
Expand Down Expand Up @@ -251,5 +248,18 @@ def program_name(override=nil) #:nodoc:
def default_command(command)
@default_command = command.to_sym
end

private

def load_commands(path)
if File.exists? path
Dir.entries(path).sort.each do |entry|
file = File.join(path,entry)
if file =~ /\.rb$/
require file
end
end
end
end
end
end
1 change: 1 addition & 0 deletions test/apps/todo/bin/todo
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ switch :otherswitch, :negatable => true
version Todo::VERSION

commands_from 'todo/commands'
commands_from File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'todo_plugins', 'commands'))

command :first do |c| c.action { |g,o,a| puts "first: #{a.join(',')}" } end
command :second do |c| c.action { |g,o,a| puts "second: #{a.join(',')}" } end
Expand Down
1 change: 1 addition & 0 deletions test/apps/todo_plugins/commands/third.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
command :third do |c| c.action { |g,o,a| puts "third: #{a.join(',')}" } end