Skip to content

Commit

Permalink
Merge pull request #77 from at-grandpa/hyphen-subcommands
Browse files Browse the repository at this point in the history
Hyphen subcommands
  • Loading branch information
at-grandpa authored Jan 3, 2021
2 parents 892c4bb + c284392 commit 90bae83
Show file tree
Hide file tree
Showing 17 changed files with 142 additions and 111 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Add this to your application's `shard.yml`:
dependencies:
clim:
github: at-grandpa/clim
version: 0.14.0
version: 0.15.0
```
## Samples
Expand Down Expand Up @@ -550,13 +550,13 @@ end

You can specify multiple arguments for the command.

| Argument | Description | Example | Required | Default |
| --------------- | -------------------- | ------------------------------- | -------- | ------------------------- |
| First argument | name | `my_argument` | true | - |
| `type` | argument type | `type: String` | false | `String` |
| `desc` | argument description | `desc: "argument description."` | false | `"Argument description."` |
| `default` | default value | `default: "default value"` | false | `nil` |
| `required` | required flag | `required: true` | false | `false` |
| Argument | Description | Example | Required | Default |
| -------------- | -------------------- | ------------------------------- | -------- | ------------------------- |
| First argument | name | `my_argument` | true | - |
| `type` | argument type | `type: String` | false | `String` |
| `desc` | argument description | `desc: "argument description."` | false | `"Argument description."` |
| `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 `all_args`, `unknown_args` and `argv` methods.

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.14.0
version: 0.15.0

authors:
- at-grandpa <@at_grandpa>
Expand Down
13 changes: 11 additions & 2 deletions spec/clim/command_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ class SpecCommand < Clim
run do |opts, args|
end
end
sub "hyphen-command" do
desc "hyphen command."
usage "main hyphen-command [options] [files]"
alias_name "hyphen-command2", "hyphen-command3"
run do |opts, args|
end
end
end
end

Expand Down Expand Up @@ -59,8 +66,9 @@ describe Clim::Command do
Sub Commands:
abc, def, ghi abc command.
abcdef, ghijkl, mnopqr abcdef command.
abc, def, ghi abc command.
abcdef, ghijkl, mnopqr abcdef command.
hyphen-command, hyphen-command2, hyphen-command3 hyphen command.
OPTIONS
Expand Down Expand Up @@ -108,6 +116,7 @@ describe Clim::Command do
"--help",
"abc", "def", "ghi",
"abcdef", "ghijkl", "mnopqr",
"hyphen-command", "hyphen-command2", "hyphen-command3",
]
SpecCommand.command.@sub_commands.to_a[0].opts_and_subcommands.should eq [
"-f", "--foo",
Expand Down
12 changes: 6 additions & 6 deletions spec/clim/compile_time_error_spec/compile_time_error_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ describe "Compile time spec, " do
`crystal run spec/clim/compile_time_error_spec/files/main_with_alias_name.cr --no-color 2>&1`.should eq <<-ERROR
Showing last frame. Use --error-trace for full trace.
In spec/clim/compile_time_error_spec/files/main_with_alias_name.cr:59:1
In spec/clim/compile_time_error_spec/files/main_with_alias_name.cr:60:1
Error: 'alias_name' is not supported on main command.
ERROR
Expand All @@ -173,7 +173,7 @@ describe "Compile time spec, " do
`crystal run spec/clim/compile_time_error_spec/files/not_supported_option_type.cr --no-color 2>&1`.should eq <<-ERROR
Showing last frame. Use --error-trace for full trace.
In spec/clim/compile_time_error_spec/files/not_supported_option_type.cr:59:1
In spec/clim/compile_time_error_spec/files/not_supported_option_type.cr:60:1
Error: Type [BigInt] is not supported on option.
ERROR
Expand All @@ -182,7 +182,7 @@ describe "Compile time spec, " do
`crystal run spec/clim/compile_time_error_spec/files/not_supported_argument_type.cr --no-color 2>&1`.should eq <<-ERROR
Showing last frame. Use --error-trace for full trace.
In spec/clim/compile_time_error_spec/files/not_supported_argument_type.cr:59:1
In spec/clim/compile_time_error_spec/files/not_supported_argument_type.cr:60:1
Error: Type [BigInt] is not supported on argument.
ERROR
Expand All @@ -191,7 +191,7 @@ describe "Compile time spec, " do
`crystal run spec/clim/compile_time_error_spec/files/empty_option_name.cr --no-color 2>&1`.should eq <<-ERROR
Showing last frame. Use --error-trace for full trace.
In spec/clim/compile_time_error_spec/files/empty_option_name.cr:59:1
In spec/clim/compile_time_error_spec/files/empty_option_name.cr:60:1
Error: Empty option name.
ERROR
Expand All @@ -200,7 +200,7 @@ describe "Compile time spec, " do
`crystal run spec/clim/compile_time_error_spec/files/empty_argument_name.cr --no-color 2>&1`.should eq <<-ERROR
Showing last frame. Use --error-trace for full trace.
In spec/clim/compile_time_error_spec/files/empty_argument_name.cr:59:1
In spec/clim/compile_time_error_spec/files/empty_argument_name.cr:60:1
Error: Empty argument name.
ERROR
Expand All @@ -221,7 +221,7 @@ describe "Compile time spec, " do
`crystal run spec/clim/compile_time_error_spec/files/help_directive_does_not_have_a_short_argument_for_main.cr --no-color 2>&1`.should eq <<-ERROR
Showing last frame. Use --error-trace for full trace.
In spec/clim/compile_time_error_spec/files/help_directive_does_not_have_a_short_argument_for_main.cr:59:1
In spec/clim/compile_time_error_spec/files/help_directive_does_not_have_a_short_argument_for_main.cr:60:1
Error: The 'help' directive requires the 'short' argument. (ex 'help short: "-h"'
ERROR
Expand Down
8 changes: 4 additions & 4 deletions spec/clim/dsl_spec/sub_command/sub_02_01_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ macro spec_for_alias_name(spec_class_name, spec_cases)
end
end
end
sub "sub_command_2" do
alias_name "alias_sub_command_2", "alias_sub_command_2_second"
sub "sub-command-2" do
alias_name "alias_sub-command-2", "alias-sub-command-2-second"
run do |opts, args|
assert_opts_and_args({{spec_case}})
end
Expand Down Expand Up @@ -55,7 +55,7 @@ end
Sub Commands:
sub_command_1, alias_sub_command_1 Command Line Interface Tool.
sub_command_2, alias_sub_command_2, alias_sub_command_2_second Command Line Interface Tool.
sub-command-2, alias_sub-command-2, alias-sub-command-2-second Command Line Interface Tool.
HELP_MESSAGE
Expand Down Expand Up @@ -85,7 +85,7 @@ end
Usage:
sub_command_2 [options] [arguments]
sub-command-2 [options] [arguments]
Options:
Expand Down
8 changes: 4 additions & 4 deletions spec/clim/dsl_spec/sub_command/sub_02_02_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ macro spec_for_alias_name(spec_class_name, spec_cases)
end
end
end
sub "sub_command_2" do
alias_name "alias_sub_command_2", "alias_sub_command_2_second"
sub "sub-command-2" do
alias_name "alias-sub-command-2", "alias-sub-command-2-second"
run do |opts, args|
assert_opts_and_args({{spec_case}})
end
Expand Down Expand Up @@ -55,7 +55,7 @@ end
Sub Commands:
sub_command_1, alias_sub_command_1 Command Line Interface Tool.
sub_command_2, alias_sub_command_2, alias_sub_command_2_second Command Line Interface Tool.
sub-command-2, alias-sub-command-2, alias-sub-command-2-second Command Line Interface Tool.
HELP_MESSAGE
Expand Down Expand Up @@ -85,7 +85,7 @@ end
Usage:
sub_command_2 [options] [arguments]
sub-command-2 [options] [arguments]
Options:
Expand Down
32 changes: 16 additions & 16 deletions spec/clim/dsl_spec/sub_command/sub_02_03_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ macro spec_for_alias_name(spec_class_name, spec_cases)
end
end
end
sub "sub_command_2" do
alias_name "alias_sub_command_2", "alias_sub_command_2_second"
sub "sub-command-2" do
alias_name "alias-sub-command-2", "alias-sub-command-2-second"
run do |opts, args|
assert_opts_and_args({{spec_case}})
end
Expand Down Expand Up @@ -55,7 +55,7 @@ end
Sub Commands:
sub_command_1, alias_sub_command_1 Command Line Interface Tool.
sub_command_2, alias_sub_command_2, alias_sub_command_2_second Command Line Interface Tool.
sub-command-2, alias-sub-command-2, alias-sub-command-2-second Command Line Interface Tool.
HELP_MESSAGE
Expand Down Expand Up @@ -85,7 +85,7 @@ end
Usage:
sub_command_2 [options] [arguments]
sub-command-2 [options] [arguments]
Options:
Expand Down Expand Up @@ -123,62 +123,62 @@ spec_for_alias_name(
expect_help: {{sub_1_help_message}},
},
{
argv: ["sub_command_2"],
argv: ["sub-command-2"],
expect_help: {{sub_2_help_message}},
expect_args_value: [] of String,
},
{
argv: ["alias_sub_command_2"],
argv: ["alias-sub-command-2"],
expect_help: {{sub_2_help_message}},
expect_args_value: [] of String,
},
{
argv: ["alias_sub_command_2_second"],
argv: ["alias-sub-command-2-second"],
expect_help: {{sub_2_help_message}},
expect_args_value: [] of String,
},
{
argv: ["sub_command_2", "arg1"],
argv: ["sub-command-2", "arg1"],
expect_help: {{sub_2_help_message}},
expect_args_value: ["arg1"],
},
{
argv: ["alias_sub_command_2", "arg1"],
argv: ["alias-sub-command-2", "arg1"],
expect_help: {{sub_2_help_message}},
expect_args_value: ["arg1"],
},
{
argv: ["alias_sub_command_2_second", "arg1"],
argv: ["alias-sub-command-2-second", "arg1"],
expect_help: {{sub_2_help_message}},
expect_args_value: ["arg1"],
},
{
argv: ["sub_command_2", "arg1", "arg2"],
argv: ["sub-command-2", "arg1", "arg2"],
expect_help: {{sub_2_help_message}},
expect_args_value: ["arg1", "arg2"],
},
{
argv: ["alias_sub_command_2", "arg1", "arg2"],
argv: ["alias-sub-command-2", "arg1", "arg2"],
expect_help: {{sub_2_help_message}},
expect_args_value: ["arg1", "arg2"],
},
{
argv: ["alias_sub_command_2_second", "arg1", "arg2"],
argv: ["alias-sub-command-2-second", "arg1", "arg2"],
expect_help: {{sub_2_help_message}},
expect_args_value: ["arg1", "arg2"],
},
{
argv: ["sub_command_2", "arg1", "arg2", "arg3"],
argv: ["sub-command-2", "arg1", "arg2", "arg3"],
expect_help: {{sub_2_help_message}},
expect_args_value: ["arg1", "arg2", "arg3"],
},
{
argv: ["alias_sub_command_2", "arg1", "arg2", "arg3"],
argv: ["alias-sub-command-2", "arg1", "arg2", "arg3"],
expect_help: {{sub_2_help_message}},
expect_args_value: ["arg1", "arg2", "arg3"],
},
{
argv: ["alias_sub_command_2_second", "arg1", "arg2", "arg3"],
argv: ["alias-sub-command-2-second", "arg1", "arg2", "arg3"],
expect_help: {{sub_2_help_message}},
expect_args_value: ["arg1", "arg2", "arg3"],
},
Expand Down
Loading

0 comments on commit 90bae83

Please sign in to comment.