Skip to content

Commit

Permalink
Merge pull request #47 from at-grandpa/add-io-to-run-block
Browse files Browse the repository at this point in the history
Add io to run block
  • Loading branch information
at-grandpa authored Oct 11, 2019
2 parents 0f48c6a + 91dbe20 commit 4908068
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 11 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Add this to your application's `shard.yml`:
dependencies:
clim:
github: at-grandpa/clim
version: 0.8.2
version: 0.9.0
```
## Minimum sample
Expand Down Expand Up @@ -644,6 +644,24 @@ class MyCli < Clim
end
```

### `io` in run block

You can receive `io` in a run block by passing it as the second argument to the start method.

```crystal
class IoCommand < Clim
main do
run do |opts, args, io|
io.puts "in main_command"
end
end
end
io = IO::Memory.new
IoCommand.start([] of String, io: io)
io.to_s # => "in main_command\n"
```

## Development

```
Expand Down
4 changes: 2 additions & 2 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: clim
version: 0.8.2
version: 0.9.0

authors:
- at-grandpa <@at_grandpa>
Expand All @@ -8,6 +8,6 @@ targets:
clim:
main: src/clim.cr

crystal: 0.31.0
crystal: 0.31.1

license: MIT
50 changes: 45 additions & 5 deletions spec/clim_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,57 @@ class IoCommand < Clim
desc "main command."
usage "main [sub_command] [arguments]"
run do |opts, args, io|
io.puts "Test"
io.puts "in main_command"
end
end
end

class IoSubCommand < Clim
main do
run do |opts, args|
end
sub "sub_command" do
desc "sub command."
usage "sub_command [arguments]"
run do |opts, args, io|
io.puts "in sub_command"
end
end
end
end

class IoSubSubCommand < Clim
main do
run do |opts, args|
end
sub "sub_command" do
run do |opts, args|
end
sub "sub_sub_command" do
run do |opts, args, io|
io.puts "in sub_sub_command"
end
end
end
end
end

describe Clim do
describe "#start_parse" do
context "with custom IO - memory" do
describe "#start" do
it "with IO::Memory in main command" do
io = IO::Memory.new
IoCommand.start([] of String, io: io)
io.to_s.should eq "in main_command\n"
end
it "with IO::Memory in sub command" do
io = IO::Memory.new
IoSubCommand.start(["sub_command"], io: io)
io.to_s.should eq "in sub_command\n"
end
it "with IO::Memory in sub sub command" do
io = IO::Memory.new
IoCommand.start_parse([] of String, io: io)
io.to_s.should eq "Test\n"
IoSubSubCommand.start(["sub_command", "sub_sub_command"], io: io)
io.to_s.should eq "in sub_sub_command\n"
end
end
end
4 changes: 2 additions & 2 deletions src/clim.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class Clim
command.parse(argv).run(io)
end

def self.start(argv)
start_parse(argv)
def self.start(argv, io : IO = STDOUT)
start_parse(argv, io)
rescue ex : ClimException
puts "ERROR: #{ex.message}"
rescue ex : ClimInvalidOptionException | ClimInvalidTypeCastException
Expand Down
2 changes: 1 addition & 1 deletion src/clim/version.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Clim
VERSION = "0.8.2"
VERSION = "0.9.0"
end

0 comments on commit 4908068

Please sign in to comment.