summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyrylo Silin <kyrylosilin@gmail.com>2012-08-18 17:20:31 +0300
committerKyrylo Silin <kyrylosilin@gmail.com>2012-08-18 17:20:31 +0300
commitc1af853339d46b7043499d7eb5c47cb42291238a (patch)
tree965978fbf28cc50ae9eb4999f8efacabf8fd3c5b
parent5fb8ae814747737884406de68ebeebbc3f7897e7 (diff)
downloadslop-c1af853339d46b7043499d7eb5c47cb42291238a.tar.gz
Implement basic support for command arguments
Example: cmds.parse %w( install geronimo banzai --outdir dir ) cmds.arguments #=> ["geronimo", "banzai"] Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
-rw-r--r--lib/slop/commands.rb10
-rw-r--r--test/commands_test.rb12
2 files changed, 18 insertions, 4 deletions
diff --git a/lib/slop/commands.rb b/lib/slop/commands.rb
index d15980e..839b48f 100644
--- a/lib/slop/commands.rb
+++ b/lib/slop/commands.rb
@@ -2,7 +2,7 @@ class Slop
class Commands
include Enumerable
- attr_reader :config, :commands
+ attr_reader :config, :commands, :arguments
attr_writer :banner
# Create a new instance of Slop::Commands and optionally build
@@ -164,6 +164,7 @@ class Slop
def parse_items(items, bang = false)
if opts = commands[items[0].to_s]
@triggered_command = items.shift
+ execute_arguments(items, bang)
bang ? opts.parse!(items) : opts.parse(items)
execute_global_opts(items, bang)
else
@@ -179,6 +180,13 @@ class Slop
items
end
+ # Returns nothing.
+ def execute_arguments(items, bang)
+ @arguments = items.take_while { |arg| !arg.start_with?('-') }
+ items.shift(@arguments.size) if bang
+ end
+
+ # Returns nothing.
def execute_global_opts(items, bang)
if global_opts = commands['global']
bang ? global_opts.parse!(items) : global_opts.parse(items)
diff --git a/test/commands_test.rb b/test/commands_test.rb
index 36798dc..ac609ac 100644
--- a/test/commands_test.rb
+++ b/test/commands_test.rb
@@ -83,14 +83,20 @@ class CommandsTest < TestCase
items = %w( foo bar baz )
assert_equal items, @commands.parse(items)
- items = %w( new --force )
+ items = %w( new file --force )
assert_equal items, @commands.parse(items)
end
test "parse! removes options/arguments" do
- items = %w( new --outdir foo )
+ items = %w( new file --outdir foo )
@commands.parse!(items)
assert_equal [], items
end
-end \ No newline at end of file
+ test "command arguments" do
+ items = %w( new file1 file2 --outdir foo )
+ @commands.parse(items)
+ assert_equal %w( file1 file2 ), @commands.arguments
+ end
+
+end