Skip to content

Commit

Permalink
Merge pull request #64 from at-grandpa/add-all-args
Browse files Browse the repository at this point in the history
add all_args
  • Loading branch information
at-grandpa authored May 9, 2020
2 parents 410d05e + 27caed8 commit c036f16
Show file tree
Hide file tree
Showing 25 changed files with 815 additions and 20 deletions.
33 changes: 27 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Add this to your application's `shard.yml`:
dependencies:
clim:
github: at-grandpa/clim
version: 0.11.0
version: 0.11.1
```
## Samples
Expand Down Expand Up @@ -322,6 +322,8 @@ end
An alias for the command. It can be specified only for subcommand.

```crystal
require "clim"
class MyCli < Clim
main do
run do |opts, args|
Expand All @@ -335,6 +337,8 @@ class MyCli < Clim
end
end
end
MyCli.start(ARGV)
```

```console
Expand All @@ -351,6 +355,8 @@ sub_command run!!
You can specify the string to be displayed with `--version`.

```crystal
require "clim"
class MyCli < Clim
main do
version "mycli version: 1.0.1"
Expand All @@ -359,6 +365,8 @@ class MyCli < Clim
end
end
end
MyCli.start(ARGV)
```

```console
Expand All @@ -369,6 +377,8 @@ mycli version: 1.0.1
If you want to display it even with `-v`, add ` short: "-v" `.

```crystal
require "clim"
class MyCli < Clim
main do
version "mycli version: 1.0.1", short: "-v"
Expand All @@ -377,6 +387,8 @@ class MyCli < Clim
end
end
end
MyCli.start(ARGV)
```

```console
Expand All @@ -393,6 +405,8 @@ The short help option is not set by default. If you want help to appear by speci
(However, it should not conflict with other options.)

```crystal
require "clim"
class MyCli < Clim
main do
desc "help directive test."
Expand All @@ -403,6 +417,8 @@ class MyCli < Clim
end
end
end
MyCli.start(ARGV)
```

```console
Expand Down Expand Up @@ -540,7 +556,7 @@ You can specify multiple arguments for the command.
| `default` | default value | `default: "default value"` | false | `nil` |
| `required` | required flag | `required: true` | false | `false` |

The order of the arguments is related to the order in which they are defined. Also, when calling a method, hyphens in the method name of the argument are converted to underscores. There are also `unknown_args` and `argv` methods.
The order of the arguments is related to the order in which they are defined. Also, when calling a method, hyphens in the method name of the argument are converted to underscores. There are also `all_args`, `unknown_args` and `argv` methods.

```crystal
require "clim"
Expand Down Expand Up @@ -568,6 +584,8 @@ class MyCli < Clim
puts " args.first_arg => #{args.first_arg}"
puts "typeof(args.second_arg) => #{typeof(args.second_arg)}"
puts " args.second_arg => #{args.second_arg}"
puts "typeof(args.all_args) => #{typeof(args.all_args)}"
puts " args.all_args => #{args.all_args}"
puts "typeof(args.unknown_args) => #{typeof(args.unknown_args)}"
puts " args.unknown_args => #{args.unknown_args}"
puts "typeof(args.argv) => #{typeof(args.argv)}"
Expand All @@ -576,7 +594,6 @@ class MyCli < Clim
end
end
MyCli.start(ARGV)
```

```console
Expand All @@ -598,15 +615,17 @@ $ crystal run src/argument.cr -- --help
01. first-arg first argument! [type:String] [default:"default value"]
02. second-arg second argument! [type:Int32] [default:999]

$ crystal run src/argument.cr -- 000 111 222 333
$ crystal run src/argument.cr -- 000 111 --dummy dummy_words 222 333
typeof(args.first_arg) => String
args.first_arg => 000
typeof(args.second_arg) => Int32
args.second_arg => 111
typeof(args.all_args) => Array(String)
args.all_args => ["000", "111", "222", "333"]
typeof(args.unknown_args) => Array(String)
args.unknown_args => ["222", "333"]
typeof(args.argv) => Array(String)
args.argv => ["000", "111", "222", "333"]
args.argv => ["000", "111", "--dummy", "dummy_words", "222", "333"]

```

Expand Down Expand Up @@ -861,6 +880,8 @@ end
You can receive `io` in a run block by passing it as the second argument to the start method.

```crystal
require "clim"
class IoCommand < Clim
main do
run do |opts, args, io|
Expand All @@ -871,7 +892,7 @@ end
io = IO::Memory.new
IoCommand.start([] of String, io: io)
io.to_s # => "in main_command\n"
puts io.to_s # => "in main_command\n"
```

## Development
Expand Down
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: clim
version: 0.11.0
version: 0.11.1

authors:
- at-grandpa <@at_grandpa>
Expand Down
12 changes: 8 additions & 4 deletions spec/clim/compile_time_error_spec/files/run_block_execution.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,29 @@ class MyCli < Clim
run do |opts, args|
puts "option : #{opts.option}"
puts "argument : #{args.arg}"
puts "argv : #{args.argv}"
puts "all_args : #{args.all_args}"
puts "unknown_args : #{args.unknown_args}"
puts "argv : #{args.argv}"
end
sub "sub_1" do
option "-o ARG", "--option=ARG", type: String
argument "arg", type: String
run do |opts, args|
puts "sub_1 option : #{opts.option}"
puts "sub_1 argument : #{args.arg}"
puts "sub_1 argv : #{args.argv}"
puts "sub_1 all_args : #{args.all_args}"
puts "sub_1 unknown_args : #{args.unknown_args}"
puts "sub_1 argv : #{args.argv}"
end
sub "sub_sub_1" do
option "-o ARG", "--option=ARG", type: String
argument "arg", type: String
run do |opts, args|
puts "sub_sub_1 option : #{opts.option}"
puts "sub_sub_1 argument : #{args.arg}"
puts "sub_sub_1 argv : #{args.argv}"
puts "sub_sub_1 all_args : #{args.all_args}"
puts "sub_sub_1 unknown_args : #{args.unknown_args}"
puts "sub_sub_1 argv : #{args.argv}"
end
end
end
Expand All @@ -36,8 +39,9 @@ class MyCli < Clim
run do |opts, args|
puts "sub_2 option : #{opts.option}"
puts "sub_2 argument : #{args.arg}"
puts "sub_2 argv : #{args.argv}"
puts "sub_2 all_args : #{args.all_args}"
puts "sub_2 unknown_args : #{args.unknown_args}"
puts "sub_2 argv : #{args.argv}"
end
end
end
Expand Down
12 changes: 8 additions & 4 deletions spec/clim/compile_time_error_spec/stdout_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -142,35 +142,39 @@ describe "STDOUT spec, " do
`crystal run spec/clim/compile_time_error_spec/files/run_block_execution.cr --no-color -- --option option_value argument_value unknown_value1 unknown_value2`.should eq <<-DISPLAY
option : option_value
argument : argument_value
argv : ["--option", "option_value", "argument_value", "unknown_value1", "unknown_value2"]
all_args : ["argument_value", "unknown_value1", "unknown_value2"]
unknown_args : ["unknown_value1", "unknown_value2"]
argv : ["--option", "option_value", "argument_value", "unknown_value1", "unknown_value2"]
DISPLAY
end
it "display STDOUT of the run block execution. (sub_1)" do
`crystal run spec/clim/compile_time_error_spec/files/run_block_execution.cr --no-color -- sub_1 --option option_value argument_value unknown_value1 unknown_value2`.should eq <<-DISPLAY
sub_1 option : option_value
sub_1 argument : argument_value
sub_1 argv : ["--option", "option_value", "argument_value", "unknown_value1", "unknown_value2"]
sub_1 all_args : ["argument_value", "unknown_value1", "unknown_value2"]
sub_1 unknown_args : ["unknown_value1", "unknown_value2"]
sub_1 argv : ["--option", "option_value", "argument_value", "unknown_value1", "unknown_value2"]
DISPLAY
end
it "display STDOUT of the run block execution. (sub_sub_1)" do
`crystal run spec/clim/compile_time_error_spec/files/run_block_execution.cr --no-color -- sub_1 sub_sub_1 --option option_value argument_value unknown_value1 unknown_value2`.should eq <<-DISPLAY
sub_sub_1 option : option_value
sub_sub_1 argument : argument_value
sub_sub_1 argv : ["--option", "option_value", "argument_value", "unknown_value1", "unknown_value2"]
sub_sub_1 all_args : ["argument_value", "unknown_value1", "unknown_value2"]
sub_sub_1 unknown_args : ["unknown_value1", "unknown_value2"]
sub_sub_1 argv : ["--option", "option_value", "argument_value", "unknown_value1", "unknown_value2"]
DISPLAY
end
it "display STDOUT of the run block execution. (sub_2)" do
`crystal run spec/clim/compile_time_error_spec/files/run_block_execution.cr --no-color -- sub_2 --option option_value argument_value unknown_value1 unknown_value2`.should eq <<-DISPLAY
sub_2 option : option_value
sub_2 argument : argument_value
sub_2 argv : ["--option", "option_value", "argument_value", "unknown_value1", "unknown_value2"]
sub_2 all_args : ["argument_value", "unknown_value1", "unknown_value2"]
sub_2 unknown_args : ["unknown_value1", "unknown_value2"]
sub_2 argv : ["--option", "option_value", "argument_value", "unknown_value1", "unknown_value2"]
DISPLAY
end
Expand Down
30 changes: 30 additions & 0 deletions spec/clim/dsl_spec/arguments/default_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ spec(
"method" => "arg3",
"expect_value" => nil,
},
{
"type" => Array(String),
"method" => "all_args",
"expect_value" => [] of String,
},
{
"type" => Array(String),
"method" => "unknown_args",
Expand Down Expand Up @@ -85,6 +90,11 @@ spec(
"method" => "arg3",
"expect_value" => nil,
},
{
"type" => Array(String),
"method" => "all_args",
"expect_value" => ["value1"],
},
{
"type" => Array(String),
"method" => "unknown_args",
Expand Down Expand Up @@ -116,6 +126,11 @@ spec(
"method" => "arg3",
"expect_value" => nil,
},
{
"type" => Array(String),
"method" => "all_args",
"expect_value" => ["value1", "value2"],
},
{
"type" => Array(String),
"method" => "unknown_args",
Expand Down Expand Up @@ -147,6 +162,11 @@ spec(
"method" => "arg3",
"expect_value" => "value3",
},
{
"type" => Array(String),
"method" => "all_args",
"expect_value" => ["value1", "value2", "value3"],
},
{
"type" => Array(String),
"method" => "unknown_args",
Expand Down Expand Up @@ -178,6 +198,11 @@ spec(
"method" => "arg3",
"expect_value" => "value3",
},
{
"type" => Array(String),
"method" => "all_args",
"expect_value" => ["value1", "value2", "value3", "value4"],
},
{
"type" => Array(String),
"method" => "unknown_args",
Expand Down Expand Up @@ -209,6 +234,11 @@ spec(
"method" => "arg3",
"expect_value" => "value3",
},
{
"type" => Array(String),
"method" => "all_args",
"expect_value" => ["value1", "value2", "value3", "value4", "value5"],
},
{
"type" => Array(String),
"method" => "unknown_args",
Expand Down
15 changes: 15 additions & 0 deletions spec/clim/dsl_spec/arguments/required_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ spec(
"method" => "arg5",
"expect_value" => nil,
},
{
"type" => Array(String),
"method" => "all_args",
"expect_value" => ["value1", "value2", "value3", "value4"],
},
{
"type" => Array(String),
"method" => "unknown_args",
Expand Down Expand Up @@ -125,6 +130,11 @@ spec(
"method" => "arg5",
"expect_value" => "value5",
},
{
"type" => Array(String),
"method" => "all_args",
"expect_value" => ["value1", "value2", "value3", "value4", "value5"],
},
{
"type" => Array(String),
"method" => "unknown_args",
Expand Down Expand Up @@ -166,6 +176,11 @@ spec(
"method" => "arg5",
"expect_value" => "value5",
},
{
"type" => Array(String),
"method" => "all_args",
"expect_value" => ["value1", "value2", "value3", "value4", "value5", "value6", "value7"],
},
{
"type" => Array(String),
"method" => "unknown_args",
Expand Down
Loading

0 comments on commit c036f16

Please sign in to comment.