From 85bac7f64b42cb16459e77051c9799f2c456ce2a Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Fri, 23 Nov 2018 18:04:43 +0900 Subject: [PATCH 1/5] ... --- .../compile_time_error_spec.cr | 2 +- src/clim.cr | 6 ++++++ src/clim/command.cr | 13 +++++++++++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/spec/clim/compile_time_error_spec/compile_time_error_spec.cr b/spec/clim/compile_time_error_spec/compile_time_error_spec.cr index 3f7cec5a..b21778b5 100644 --- a/spec/clim/compile_time_error_spec/compile_time_error_spec.cr +++ b/spec/clim/compile_time_error_spec/compile_time_error_spec.cr @@ -33,7 +33,7 @@ describe "Compile time spec, " do end it "duplicate 'main_command' in sub command." do `crystal run spec/clim/compile_time_error_spec/files/duplicate_main_command_in_sub_command.cr --no-color 2>&1`.should eq <<-ERROR - Error in spec/clim/compile_time_error_spec/files/duplicate_main_command_in_sub_command.cr:7: Can not be declared 'main_command' as sub command. + Error in spec/clim/compile_time_error_spec/files/duplicate_main_command_in_sub_command.cr:7: Can not be declared 'main_command' or 'main' as sub command. main_command do ^~~~~~~~~~~~ diff --git a/src/clim.cr b/src/clim.cr index 28e85c42..d5b8b580 100644 --- a/src/clim.cr +++ b/src/clim.cr @@ -47,6 +47,12 @@ class Clim end end + macro main(&block) + main_command do + {{ yield }} + end + end + macro main_command(&block) Clim::Command.command "main_command_of_clim_library" do diff --git a/src/clim/command.cr b/src/clim/command.cr index ba199f72..51b84584 100644 --- a/src/clim/command.cr +++ b/src/clim/command.cr @@ -3,7 +3,6 @@ require "./command/*" class Clim abstract class Command - property name : String = "" property alias_name : Array(String) = [] of String property parser : OptionParser = OptionParser.new @@ -62,8 +61,18 @@ class Clim def define_version(parser) end + macro main + main_command + end + macro main_command - {% raise "Can not be declared 'main_command' as sub command." if @type.superclass.id.stringify == "Clim::Command" %} + {% raise "Can not be declared 'main_command' or 'main' as sub command." if @type.superclass.id.stringify == "Clim::Command" %} + end + + macro sub(name, &block) + sub_command({{name}}) do + {{ yield }} + end end macro sub_command(name, &block) From 4cc10bfc012d823cd9bbdcc49754cfacb6a1e2a7 Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Fri, 23 Nov 2018 19:19:45 +0900 Subject: [PATCH 2/5] ... --- spec/clim/dsl_spec/main_spec.cr | 686 ++++++++++++++++++++++++++++++++ 1 file changed, 686 insertions(+) create mode 100644 spec/clim/dsl_spec/main_spec.cr diff --git a/spec/clim/dsl_spec/main_spec.cr b/spec/clim/dsl_spec/main_spec.cr new file mode 100644 index 00000000..b2eb88c9 --- /dev/null +++ b/spec/clim/dsl_spec/main_spec.cr @@ -0,0 +1,686 @@ +require "../dsl_spec" + +macro spec_for_main(spec_class_name, spec_desc, spec_cases, spec_dsl_lines = [] of StringLiteral, spec_class_define_lines = [] of StringLiteral, spec_sub_command_lines = [] of StringLiteral) + {% for spec_case, index in spec_cases %} + {% class_name = (spec_class_name.stringify + index.stringify).id %} + + # define dsl + class {{class_name}} < Clim + expand_lines({{spec_class_define_lines}}) + main do + expand_lines({{spec_dsl_lines}}) + run do |opts, args| + assert_opts_and_args({{spec_case}}) + end + expand_lines({{spec_sub_command_lines}}) + end + end + + # spec + describe {{spec_desc}} do + describe "if dsl is [" + {{spec_dsl_lines.join(", ")}} + "]," do + describe "if argv is " + {{spec_case["argv"].stringify}} + "," do + it_blocks({{class_name}}, {{spec_case}}) + end + end + end + {% end %} +end + +{% begin %} +{% + main_help_message = <<-HELP_MESSAGE + + Command Line Interface Tool. + + Usage: + + main_command_of_clim_library [options] [arguments] + + Options: + + --help Show this help. + + + HELP_MESSAGE +%} + +spec_for_main( + spec_class_name: MainCommandOnly, + spec_dsl_lines: [] of String, + spec_desc: "main command,", + spec_cases: [ + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_args: [] of String, + }, + { + argv: ["arg1"], + expect_help: {{main_help_message}}, + expect_args: ["arg1"], + }, + { + argv: ["arg1", "arg2"], + expect_help: {{main_help_message}}, + expect_args: ["arg1", "arg2"], + }, + { + argv: ["arg1", "arg2", "arg3"], + expect_help: {{main_help_message}}, + expect_args: ["arg1", "arg2", "arg3"], + }, + { + argv: ["-h"], + exception_message: "Undefined option. \"-h\"", + }, + { + argv: ["--help", "-ignore-option"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["-ignore-option", "--help"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["--missing-option"], + exception_message: "Undefined option. \"--missing-option\"", + }, + { + argv: ["-m", "arg1"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["arg1", "-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["-m", "-d"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["--help"], + expect_help: {{main_help_message}}, + }, + { + argv: ["--help", "ignore-arg"], + expect_help: {{main_help_message}}, + }, + { + argv: ["ignore-arg", "--help"], + expect_help: {{main_help_message}}, + }, + ] +) +{% end %} + +{% begin %} +{% + main_help_message = <<-HELP_MESSAGE + + Main command with desc. + + Usage: + + main_command_of_clim_library [options] [arguments] + + Options: + + --help Show this help. + + + HELP_MESSAGE +%} + +spec_for_main( + spec_class_name: MainCommandWithDesc, + spec_dsl_lines: [ + "desc \"Main command with desc.\"", + ], + spec_desc: "main command,", + spec_cases: [ + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_args: [] of String, + }, + { + argv: ["arg1"], + expect_help: {{main_help_message}}, + expect_args: ["arg1"], + }, + { + argv: ["arg1", "arg2"], + expect_help: {{main_help_message}}, + expect_args: ["arg1", "arg2"], + }, + { + argv: ["arg1", "arg2", "arg3"], + expect_help: {{main_help_message}}, + expect_args: ["arg1", "arg2", "arg3"], + }, + { + argv: ["-h"], + exception_message: "Undefined option. \"-h\"", + }, + { + argv: ["--help", "-ignore-option"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["-ignore-option", "--help"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["--missing-option"], + exception_message: "Undefined option. \"--missing-option\"", + }, + { + argv: ["-m", "arg1"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["arg1", "-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["-m", "-d"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["--help"], + expect_help: {{main_help_message}}, + }, + { + argv: ["--help", "ignore-arg"], + expect_help: {{main_help_message}}, + }, + { + argv: ["ignore-arg", "--help"], + expect_help: {{main_help_message}}, + }, + ] +) +{% end %} + +{% begin %} +{% + main_help_message = <<-HELP_MESSAGE + + Main command with desc. + + Usage: + + main_command_of_clim_library [options] [arguments] + + Options: + + --help Show this help. + + + HELP_MESSAGE +%} + +spec_for_main( + spec_class_name: MainCommandWithDescConst, + spec_class_define_lines: [ + "DESC_CONST = \"Main command with desc.\"", + ], + spec_dsl_lines: [ + "desc DESC_CONST", + ], + spec_desc: "main command,", + spec_cases: [ + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_args: [] of String, + }, + { + argv: ["arg1"], + expect_help: {{main_help_message}}, + expect_args: ["arg1"], + }, + { + argv: ["arg1", "arg2"], + expect_help: {{main_help_message}}, + expect_args: ["arg1", "arg2"], + }, + { + argv: ["arg1", "arg2", "arg3"], + expect_help: {{main_help_message}}, + expect_args: ["arg1", "arg2", "arg3"], + }, + { + argv: ["-h"], + exception_message: "Undefined option. \"-h\"", + }, + { + argv: ["--help", "-ignore-option"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["-ignore-option", "--help"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["--missing-option"], + exception_message: "Undefined option. \"--missing-option\"", + }, + { + argv: ["-m", "arg1"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["arg1", "-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["-m", "-d"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["--help"], + expect_help: {{main_help_message}}, + }, + { + argv: ["--help", "ignore-arg"], + expect_help: {{main_help_message}}, + }, + { + argv: ["ignore-arg", "--help"], + expect_help: {{main_help_message}}, + }, + ] +) +{% end %} +{% begin %} +{% + main_help_message = <<-HELP_MESSAGE + + Main command with desc. + + Usage: + + main_command with usage [options] [arguments] + + Options: + + --help Show this help. + + + HELP_MESSAGE +%} + +spec_for_main( + spec_class_name: MainCommandWithUsage, + spec_dsl_lines: [ + "desc \"Main command with desc.\"", + "usage \"main_command with usage [options] [arguments]\"", + ], + spec_desc: "main command,", + spec_cases: [ + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_args: [] of String, + }, + { + argv: ["arg1"], + expect_help: {{main_help_message}}, + expect_args: ["arg1"], + }, + { + argv: ["arg1", "arg2"], + expect_help: {{main_help_message}}, + expect_args: ["arg1", "arg2"], + }, + { + argv: ["arg1", "arg2", "arg3"], + expect_help: {{main_help_message}}, + expect_args: ["arg1", "arg2", "arg3"], + }, + { + argv: ["-h"], + exception_message: "Undefined option. \"-h\"", + }, + { + argv: ["--help", "-ignore-option"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["-ignore-option", "--help"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["--missing-option"], + exception_message: "Undefined option. \"--missing-option\"", + }, + { + argv: ["-m", "arg1"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["arg1", "-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["-m", "-d"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["--help"], + expect_help: {{main_help_message}}, + }, + { + argv: ["--help", "ignore-arg"], + expect_help: {{main_help_message}}, + }, + { + argv: ["ignore-arg", "--help"], + expect_help: {{main_help_message}}, + }, + ] +) +{% end %} + +{% begin %} +{% + main_help_message = <<-HELP_MESSAGE + + Main command with desc. + + Usage: + + main_command with usage [options] [arguments] + + Options: + + --help Show this help. + + + HELP_MESSAGE +%} + +spec_for_main( + spec_class_name: MainCommandWithUsageConst, + spec_class_define_lines: [ + "USAGE_CONST = \"main_command with usage [options] [arguments]\"", + ], + spec_dsl_lines: [ + "desc \"Main command with desc.\"", + "usage USAGE_CONST", + ], + spec_desc: "main command,", + spec_cases: [ + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_args: [] of String, + }, + { + argv: ["arg1"], + expect_help: {{main_help_message}}, + expect_args: ["arg1"], + }, + { + argv: ["arg1", "arg2"], + expect_help: {{main_help_message}}, + expect_args: ["arg1", "arg2"], + }, + { + argv: ["arg1", "arg2", "arg3"], + expect_help: {{main_help_message}}, + expect_args: ["arg1", "arg2", "arg3"], + }, + { + argv: ["-h"], + exception_message: "Undefined option. \"-h\"", + }, + { + argv: ["--help", "-ignore-option"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["-ignore-option", "--help"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["--missing-option"], + exception_message: "Undefined option. \"--missing-option\"", + }, + { + argv: ["-m", "arg1"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["arg1", "-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["-m", "-d"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["--help"], + expect_help: {{main_help_message}}, + }, + { + argv: ["--help", "ignore-arg"], + expect_help: {{main_help_message}}, + }, + { + argv: ["ignore-arg", "--help"], + expect_help: {{main_help_message}}, + }, + ] +) +{% end %} + +{% begin %} +{% + main_help_message = <<-HELP_MESSAGE + + Main command with desc. + + Usage: + + main_command with usage [options] [arguments] + + Options: + + --help Show this help. + --version Show version. + + + HELP_MESSAGE +%} + +spec_for_main( + spec_class_name: MainCommandWithVersion, + spec_dsl_lines: [ + "version \"Version 0.9.9\"", + "desc \"Main command with desc.\"", + "usage \"main_command with usage [options] [arguments]\"", + ], + spec_desc: "main command,", + spec_cases: [ + { + argv: ["--help"], + expect_help: {{main_help_message}}, + }, + { + argv: ["--help", "ignore-arg"], + expect_help: {{main_help_message}}, + }, + { + argv: ["ignore-arg", "--help"], + expect_help: {{main_help_message}}, + }, + { + argv: ["--version"], + expect_output: "Version 0.9.9\n", + }, + { + argv: ["--version", "arg"], + expect_output: "Version 0.9.9\n", + }, + { + argv: ["arg", "--version"], + expect_output: "Version 0.9.9\n", + }, + { + argv: ["-v"], + exception_message: "Undefined option. \"-v\"", + }, + ] +) +{% end %} + +{% begin %} +{% + main_help_message = <<-HELP_MESSAGE + + Main command with desc. + + Usage: + + main_command with usage [options] [arguments] + + Options: + + --help Show this help. + --version Show version. + + + HELP_MESSAGE +%} + +spec_for_main( + spec_class_name: MainCommandWithVersionConst, + spec_dsl_lines: [ + "version Clim::VERSION", + "desc \"Main command with desc.\"", + "usage \"main_command with usage [options] [arguments]\"", + ], + spec_desc: "main command,", + spec_cases: [ + { + argv: ["--help"], + expect_help: {{main_help_message}}, + }, + { + argv: ["--help", "ignore-arg"], + expect_help: {{main_help_message}}, + }, + { + argv: ["ignore-arg", "--help"], + expect_help: {{main_help_message}}, + }, + { + argv: ["--version"], + expect_output: Clim::VERSION + "\n", + }, + { + argv: ["--version", "arg"], + expect_output: Clim::VERSION + "\n", + }, + { + argv: ["arg", "--version"], + expect_output: Clim::VERSION + "\n", + }, + { + argv: ["-v"], + exception_message: "Undefined option. \"-v\"", + }, + ] +) +{% end %} + +{% begin %} +{% + main_help_message = <<-HELP_MESSAGE + + Main command with desc. + + Usage: + + main_command with usage [options] [arguments] + + Options: + + --help Show this help. + -v, --version Show version. + + + HELP_MESSAGE +%} + +spec_for_main( + spec_class_name: MainCommandWithVersionShort, + spec_dsl_lines: [ + "version \"Version 0.9.9\", short: \"-v\"", + "desc \"Main command with desc.\"", + "usage \"main_command with usage [options] [arguments]\"", + ], + spec_desc: "main command,", + spec_cases: [ + { + argv: ["--help"], + expect_help: {{main_help_message}}, + }, + { + argv: ["--help", "ignore-arg"], + expect_help: {{main_help_message}}, + }, + { + argv: ["ignore-arg", "--help"], + expect_help: {{main_help_message}}, + }, + { + argv: ["--version"], + expect_output: "Version 0.9.9\n", + }, + { + argv: ["--version", "arg"], + expect_output: "Version 0.9.9\n", + }, + { + argv: ["arg", "--version"], + expect_output: "Version 0.9.9\n", + }, + { + argv: ["-v"], + expect_output: "Version 0.9.9\n", + }, + { + argv: ["-v", "arg"], + expect_output: "Version 0.9.9\n", + }, + { + argv: ["arg", "-v"], + expect_output: "Version 0.9.9\n", + }, + ] +) +{% end %} From 3bd0b184e3d6eb27603137537ad877dc8a165d31 Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Fri, 23 Nov 2018 19:26:33 +0900 Subject: [PATCH 3/5] ... --- spec/clim/dsl_spec/sub_1_spec.cr | 679 +++++++++++++++++++++++++++++++ spec/clim/dsl_spec/sub_2_spec.cr | 517 +++++++++++++++++++++++ 2 files changed, 1196 insertions(+) create mode 100644 spec/clim/dsl_spec/sub_1_spec.cr create mode 100644 spec/clim/dsl_spec/sub_2_spec.cr diff --git a/spec/clim/dsl_spec/sub_1_spec.cr b/spec/clim/dsl_spec/sub_1_spec.cr new file mode 100644 index 00000000..f877550c --- /dev/null +++ b/spec/clim/dsl_spec/sub_1_spec.cr @@ -0,0 +1,679 @@ +require "../dsl_spec" + +{% begin %} +{% + main_help_message = <<-HELP_MESSAGE + + Command Line Interface Tool. + + Usage: + + main_command_of_clim_library [options] [arguments] + + Options: + + --help Show this help. + + Sub Commands: + + sub_command Sub command with desc. + + + HELP_MESSAGE + + sub_help_message = <<-HELP_MESSAGE + + Sub command with desc. + + Usage: + + sub_command with usage [options] [arguments] + + Options: + + --help Show this help. + + + HELP_MESSAGE +%} + +spec( + spec_class_name: SubCommandWithDescAndUsage, + spec_sub_command_lines: [ + <<-SUB_COMMAND, + sub "sub_command" do + desc "Sub command with desc." + usage "sub_command with usage [options] [arguments]" + run do |options, arguments| + end + end + SUB_COMMAND + ], + spec_desc: "option type spec,", + spec_cases: [ + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_args: [] of String, + }, + { + argv: ["arg1"], + expect_help: {{main_help_message}}, + expect_args: ["arg1"], + }, + { + argv: ["arg1", "arg2"], + expect_help: {{main_help_message}}, + expect_args: ["arg1", "arg2"], + }, + { + argv: ["arg1", "arg2", "arg3"], + expect_help: {{main_help_message}}, + expect_args: ["arg1", "arg2", "arg3"], + }, + { + argv: ["-h"], + exception_message: "Undefined option. \"-h\"", + }, + { + argv: ["--help", "-ignore-option"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["-ignore-option", "--help"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["--help"], + expect_help: {{main_help_message}}, + }, + { + argv: ["--help", "ignore-arg"], + expect_help: {{main_help_message}}, + }, + { + argv: ["ignore-arg", "--help"], + expect_help: {{main_help_message}}, + }, + { + argv: ["sub_command"], + expect_help: {{sub_help_message}}, + expect_args: [] of String, + }, + { + argv: ["sub_command", "arg1"], + expect_help: {{sub_help_message}}, + expect_args: ["arg1"], + }, + { + argv: ["sub_command", "arg1", "arg2"], + expect_help: {{sub_help_message}}, + expect_args: ["arg1", "arg2"], + }, + { + argv: ["sub_command", "arg1", "arg2", "arg3"], + expect_help: {{sub_help_message}}, + expect_args: ["arg1", "arg2", "arg3"], + }, + { + argv: ["sub_command", "--help", "-ignore-option"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["sub_command", "-ignore-option", "--help"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["sub_command", "-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["sub_command", "--missing-option"], + exception_message: "Undefined option. \"--missing-option\"", + }, + { + argv: ["sub_command", "-m", "arg1"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["sub_command", "arg1", "-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["sub_command", "-m", "-d"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["sub_command", "--help"], + expect_help: {{sub_help_message}}, + }, + { + argv: ["sub_command", "--help", "ignore-arg"], + expect_help: {{sub_help_message}}, + }, + { + argv: ["sub_command", "ignore-arg", "--help"], + expect_help: {{sub_help_message}}, + }, + ] +) +{% end %} + +macro spec_for_sub_sub_commands(spec_class_name, spec_cases) + {% for spec_case, index in spec_cases %} + {% class_name = (spec_class_name.stringify + index.stringify).id %} + + # define dsl + class {{class_name}} < Clim + main_command do + run do |opts, args| + assert_opts_and_args({{spec_case}}) + end + sub "sub_command" do + run do |opts, args| + assert_opts_and_args({{spec_case}}) + end + sub "sub_sub_command" do + run do |opts, args| + assert_opts_and_args({{spec_case}}) + end + end + end + end + end + + # spec + describe "sub sub command," do + describe "if argv is " + {{spec_case["argv"].stringify}} + "," do + it_blocks({{class_name}}, {{spec_case}}) + end + end + {% end %} +end + +{% begin %} +{% + main_help_message = <<-HELP_MESSAGE + + Command Line Interface Tool. + + Usage: + + main_command_of_clim_library [options] [arguments] + + Options: + + --help Show this help. + + Sub Commands: + + sub_command Command Line Interface Tool. + + + HELP_MESSAGE + + sub_help_message = <<-HELP_MESSAGE + + Command Line Interface Tool. + + Usage: + + sub_command [options] [arguments] + + Options: + + --help Show this help. + + Sub Commands: + + sub_sub_command Command Line Interface Tool. + + + HELP_MESSAGE + + sub_sub_help_message = <<-HELP_MESSAGE + + Command Line Interface Tool. + + Usage: + + sub_sub_command [options] [arguments] + + Options: + + --help Show this help. + + + HELP_MESSAGE +%} + +spec_for_sub_sub_commands( + spec_class_name: SubSubCommandOnly, + spec_cases: [ + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_args: [] of String, + }, + { + argv: ["arg1"], + expect_help: {{main_help_message}}, + expect_args: ["arg1"], + }, + { + argv: ["arg1", "arg2"], + expect_help: {{main_help_message}}, + expect_args: ["arg1", "arg2"], + }, + { + argv: ["arg1", "arg2", "arg3"], + expect_help: {{main_help_message}}, + expect_args: ["arg1", "arg2", "arg3"], + }, + { + argv: ["-h"], + exception_message: "Undefined option. \"-h\"", + }, + { + argv: ["--help", "-ignore-option"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["-ignore-option", "--help"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["--help"], + expect_help: {{main_help_message}}, + }, + { + argv: ["--help", "ignore-arg"], + expect_help: {{main_help_message}}, + }, + { + argv: ["ignore-arg", "--help"], + expect_help: {{main_help_message}}, + }, + { + argv: ["sub_command"], + expect_help: {{sub_help_message}}, + expect_args: [] of String, + }, + { + argv: ["sub_command", "arg1"], + expect_help: {{sub_help_message}}, + expect_args: ["arg1"], + }, + { + argv: ["sub_command", "arg1", "arg2"], + expect_help: {{sub_help_message}}, + expect_args: ["arg1", "arg2"], + }, + { + argv: ["sub_command", "arg1", "arg2", "arg3"], + expect_help: {{sub_help_message}}, + expect_args: ["arg1", "arg2", "arg3"], + }, + { + argv: ["sub_command", "--help", "-ignore-option"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["sub_command", "-ignore-option", "--help"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["sub_command", "-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["sub_command", "--missing-option"], + exception_message: "Undefined option. \"--missing-option\"", + }, + { + argv: ["sub_command", "-m", "arg1"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["sub_command", "arg1", "-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["sub_command", "-m", "-d"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["sub_command", "--help"], + expect_help: {{sub_help_message}}, + }, + { + argv: ["sub_command", "--help", "ignore-arg"], + expect_help: {{sub_help_message}}, + }, + { + argv: ["sub_command", "ignore-arg", "--help"], + expect_help: {{sub_help_message}}, + }, + { + argv: ["sub_command", "sub_sub_command"], + expect_help: {{sub_sub_help_message}}, + expect_args: [] of String, + }, + { + argv: ["sub_command", "sub_sub_command", "arg1"], + expect_help: {{sub_sub_help_message}}, + expect_args: ["arg1"], + }, + { + argv: ["sub_command", "sub_sub_command", "arg1", "arg2"], + expect_help: {{sub_sub_help_message}}, + expect_args: ["arg1", "arg2"], + }, + { + argv: ["sub_command", "sub_sub_command", "arg1", "arg2", "arg3"], + expect_help: {{sub_sub_help_message}}, + expect_args: ["arg1", "arg2", "arg3"], + }, + { + argv: ["sub_command", "sub_sub_command", "--help", "-ignore-option"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["sub_command", "sub_sub_command", "-ignore-option", "--help"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["sub_command", "sub_sub_command", "-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["sub_command", "sub_sub_command", "--missing-option"], + exception_message: "Undefined option. \"--missing-option\"", + }, + { + argv: ["sub_command", "sub_sub_command", "-m", "arg1"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["sub_command", "sub_sub_command", "arg1", "-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["sub_command", "sub_sub_command", "-m", "-d"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["sub_command", "sub_sub_command", "--help"], + expect_help: {{sub_sub_help_message}}, + }, + { + argv: ["sub_command", "sub_sub_command", "--help", "ignore-arg"], + expect_help: {{sub_sub_help_message}}, + }, + { + argv: ["sub_command", "sub_sub_command", "ignore-arg", "--help"], + expect_help: {{sub_sub_help_message}}, + }, + ] +) +{% end %} + +macro spec_for_jump_over_sub_sub_command(spec_class_name, spec_cases) + {% for spec_case, index in spec_cases %} + {% class_name = (spec_class_name.stringify + index.stringify).id %} + + # define dsl + class {{class_name}} < Clim + main_command do + run do |opts, args| + assert_opts_and_args({{spec_case}}) + end + command "sub_command" do + run do |opts, args| + end + command "sub_sub_command" do + run do |opts, args| + assert_opts_and_args({{spec_case}}) + end + end + end + command "jump_over_sub_sub_command" do + run do |opts, args| + assert_opts_and_args({{spec_case}}) + end + end + end + end + + # spec + describe "jump over sub_sub command," do + describe "if argv is " + {{spec_case["argv"].stringify}} + "," do + it_blocks({{class_name}}, {{spec_case}}) + end + end + {% end %} +end + +{% begin %} +{% + main_help_message = <<-HELP_MESSAGE + + Command Line Interface Tool. + + Usage: + + main_command_of_clim_library [options] [arguments] + + Options: + + --help Show this help. + + Sub Commands: + + sub_command Command Line Interface Tool. + jump_over_sub_sub_command Command Line Interface Tool. + + + HELP_MESSAGE + + sub_sub_help_message = <<-HELP_MESSAGE + + Command Line Interface Tool. + + Usage: + + sub_sub_command [options] [arguments] + + Options: + + --help Show this help. + + + HELP_MESSAGE + + jump_over_sub_sub_help_message = <<-HELP_MESSAGE + + Command Line Interface Tool. + + Usage: + + jump_over_sub_sub_command [options] [arguments] + + Options: + + --help Show this help. + + + HELP_MESSAGE +%} + +spec_for_jump_over_sub_sub_command( + spec_class_name: JumpOverSubSubCommand, + spec_cases: [ + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_args: [] of String, + }, + { + argv: ["arg1"], + expect_help: {{main_help_message}}, + expect_args: ["arg1"], + }, + { + argv: ["arg1", "arg2"], + expect_help: {{main_help_message}}, + expect_args: ["arg1", "arg2"], + }, + { + argv: ["arg1", "arg2", "arg3"], + expect_help: {{main_help_message}}, + expect_args: ["arg1", "arg2", "arg3"], + }, + { + argv: ["-h"], + exception_message: "Undefined option. \"-h\"", + }, + { + argv: ["--help", "-ignore-option"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["-ignore-option", "--help"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["--help"], + expect_help: {{main_help_message}}, + }, + { + argv: ["--help", "ignore-arg"], + expect_help: {{main_help_message}}, + }, + { + argv: ["ignore-arg", "--help"], + expect_help: {{main_help_message}}, + }, + { + argv: ["sub_command", "sub_sub_command"], + expect_help: {{sub_sub_help_message}}, + expect_args: [] of String, + }, + { + argv: ["sub_command", "sub_sub_command", "arg1"], + expect_help: {{sub_sub_help_message}}, + expect_args: ["arg1"], + }, + { + argv: ["sub_command", "sub_sub_command", "arg1", "arg2"], + expect_help: {{sub_sub_help_message}}, + expect_args: ["arg1", "arg2"], + }, + { + argv: ["sub_command", "sub_sub_command", "arg1", "arg2", "arg3"], + expect_help: {{sub_sub_help_message}}, + expect_args: ["arg1", "arg2", "arg3"], + }, + { + argv: ["sub_command", "sub_sub_command", "--help", "-ignore-option"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["sub_command", "sub_sub_command", "-ignore-option", "--help"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["sub_command", "sub_sub_command", "-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["sub_command", "sub_sub_command", "--missing-option"], + exception_message: "Undefined option. \"--missing-option\"", + }, + { + argv: ["sub_command", "sub_sub_command", "-m", "arg1"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["sub_command", "sub_sub_command", "arg1", "-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["sub_command", "sub_sub_command", "-m", "-d"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["sub_command", "sub_sub_command", "--help"], + expect_help: {{sub_sub_help_message}}, + }, + { + argv: ["sub_command", "sub_sub_command", "--help", "ignore-arg"], + expect_help: {{sub_sub_help_message}}, + }, + { + argv: ["sub_command", "sub_sub_command", "ignore-arg", "--help"], + expect_help: {{sub_sub_help_message}}, + }, + { + argv: ["jump_over_sub_sub_command"], + expect_help: {{jump_over_sub_sub_help_message}}, + expect_args: [] of String, + }, + { + argv: ["jump_over_sub_sub_command", "arg1"], + expect_help: {{jump_over_sub_sub_help_message}}, + expect_args: ["arg1"], + }, + { + argv: ["jump_over_sub_sub_command", "arg1", "arg2"], + expect_help: {{jump_over_sub_sub_help_message}}, + expect_args: ["arg1", "arg2"], + }, + { + argv: ["jump_over_sub_sub_command", "arg1", "arg2", "arg3"], + expect_help: {{jump_over_sub_sub_help_message}}, + expect_args: ["arg1", "arg2", "arg3"], + }, + { + argv: ["jump_over_sub_sub_command", "--help", "-ignore-option"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["jump_over_sub_sub_command", "-ignore-option", "--help"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["jump_over_sub_sub_command", "-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["jump_over_sub_sub_command", "--missing-option"], + exception_message: "Undefined option. \"--missing-option\"", + }, + { + argv: ["jump_over_sub_sub_command", "-m", "arg1"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["jump_over_sub_sub_command", "arg1", "-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["jump_over_sub_sub_command", "-m", "-d"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["jump_over_sub_sub_command", "--help"], + expect_help: {{jump_over_sub_sub_help_message}}, + }, + { + argv: ["jump_over_sub_sub_command", "--help", "ignore-arg"], + expect_help: {{jump_over_sub_sub_help_message}}, + }, + { + argv: ["jump_over_sub_sub_command", "ignore-arg", "--help"], + expect_help: {{jump_over_sub_sub_help_message}}, + }, + ] +) +{% end %} diff --git a/spec/clim/dsl_spec/sub_2_spec.cr b/spec/clim/dsl_spec/sub_2_spec.cr new file mode 100644 index 00000000..cde666a0 --- /dev/null +++ b/spec/clim/dsl_spec/sub_2_spec.cr @@ -0,0 +1,517 @@ +require "../dsl_spec" + +macro spec_for_alias_name(spec_class_name, spec_cases) + {% for spec_case, index in spec_cases %} + {% class_name = (spec_class_name.stringify + index.stringify).id %} + + # define dsl + class {{class_name}} < Clim + main_command do + run do |opts, args| + assert_opts_and_args({{spec_case}}) + end + sub "sub_command_1" do + alias_name "alias_sub_command_1" + run do |opts, args| + assert_opts_and_args({{spec_case}}) + end + sub "sub_sub_command_1" do + run do |opts, args| + end + end + end + 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 + end + end + end + + # spec + describe "alias name case," do + describe "if argv is " + {{spec_case["argv"].stringify}} + "," do + it_blocks({{class_name}}, {{spec_case}}) + end + end + {% end %} +end + +{% begin %} +{% + main_help_message = <<-HELP_MESSAGE + + Command Line Interface Tool. + + Usage: + + main_command_of_clim_library [options] [arguments] + + Options: + + --help Show this help. + + 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. + + + HELP_MESSAGE + + sub_1_help_message = <<-HELP_MESSAGE + + Command Line Interface Tool. + + Usage: + + sub_command_1 [options] [arguments] + + Options: + + --help Show this help. + + Sub Commands: + + sub_sub_command_1 Command Line Interface Tool. + + + HELP_MESSAGE + + sub_2_help_message = <<-HELP_MESSAGE + + Command Line Interface Tool. + + Usage: + + sub_command_2 [options] [arguments] + + Options: + + --help Show this help. + + + HELP_MESSAGE +%} + +spec_for_alias_name( + spec_class_name: SubCommandWithAliasName, + spec_cases: [ + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_args: [] of String, + }, + { + argv: ["arg1"], + expect_help: {{main_help_message}}, + expect_args: ["arg1"], + }, + { + argv: ["arg1", "arg2"], + expect_help: {{main_help_message}}, + expect_args: ["arg1", "arg2"], + }, + { + argv: ["arg1", "arg2", "arg3"], + expect_help: {{main_help_message}}, + expect_args: ["arg1", "arg2", "arg3"], + }, + { + argv: ["-h"], + exception_message: "Undefined option. \"-h\"", + }, + { + argv: ["--help", "-ignore-option"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["-ignore-option", "--help"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["--help"], + expect_help: {{main_help_message}}, + }, + { + argv: ["--help", "ignore-arg"], + expect_help: {{main_help_message}}, + }, + { + argv: ["ignore-arg", "--help"], + expect_help: {{main_help_message}}, + }, + { + argv: ["sub_command_1"], + expect_help: {{sub_1_help_message}}, + expect_args: [] of String, + }, + { + argv: ["alias_sub_command_1"], + expect_help: {{sub_1_help_message}}, + expect_args: [] of String, + }, + { + argv: ["sub_command_1", "arg1"], + expect_help: {{sub_1_help_message}}, + expect_args: ["arg1"], + }, + { + argv: ["alias_sub_command_1", "arg1"], + expect_help: {{sub_1_help_message}}, + expect_args: ["arg1"], + }, + { + argv: ["sub_command_1", "arg1", "arg2"], + expect_help: {{sub_1_help_message}}, + expect_args: ["arg1", "arg2"], + }, + { + argv: ["alias_sub_command_1", "arg1", "arg2"], + expect_help: {{sub_1_help_message}}, + expect_args: ["arg1", "arg2"], + }, + { + argv: ["sub_command_1", "arg1", "arg2", "arg3"], + expect_help: {{sub_1_help_message}}, + expect_args: ["arg1", "arg2", "arg3"], + }, + { + argv: ["alias_sub_command_1", "arg1", "arg2", "arg3"], + expect_help: {{sub_1_help_message}}, + expect_args: ["arg1", "arg2", "arg3"], + }, + { + argv: ["sub_command_1", "--help", "-ignore-option"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["alias_sub_command_1", "--help", "-ignore-option"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["sub_command_1", "-ignore-option", "--help"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["alias_sub_command_1", "-ignore-option", "--help"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["sub_command_1", "-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["alias_sub_command_1", "-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["sub_command_1", "--missing-option"], + exception_message: "Undefined option. \"--missing-option\"", + }, + { + argv: ["alias_sub_command_1", "--missing-option"], + exception_message: "Undefined option. \"--missing-option\"", + }, + { + argv: ["sub_command_1", "-m", "arg1"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["alias_sub_command_1", "-m", "arg1"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["sub_command_1", "arg1", "-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["alias_sub_command_1", "arg1", "-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["sub_command_1", "-m", "-d"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["alias_sub_command_1", "-m", "-d"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["sub_command_1", "--help"], + expect_help: {{sub_1_help_message}}, + }, + { + argv: ["alias_sub_command_1", "--help"], + expect_help: {{sub_1_help_message}}, + }, + { + argv: ["sub_command_1", "--help", "ignore-arg"], + expect_help: {{sub_1_help_message}}, + }, + { + argv: ["alias_sub_command_1", "--help", "ignore-arg"], + expect_help: {{sub_1_help_message}}, + }, + { + argv: ["sub_command_1", "ignore-arg", "--help"], + expect_help: {{sub_1_help_message}}, + }, + { + argv: ["alias_sub_command_1", "ignore-arg", "--help"], + expect_help: {{sub_1_help_message}}, + }, + { + argv: ["sub_command_2"], + expect_help: {{sub_2_help_message}}, + expect_args: [] of String, + }, + { + argv: ["alias_sub_command_2"], + expect_help: {{sub_2_help_message}}, + expect_args: [] of String, + }, + { + argv: ["alias_sub_command_2_second"], + expect_help: {{sub_2_help_message}}, + expect_args: [] of String, + }, + { + argv: ["sub_command_2", "arg1"], + expect_help: {{sub_2_help_message}}, + expect_args: ["arg1"], + }, + { + argv: ["alias_sub_command_2", "arg1"], + expect_help: {{sub_2_help_message}}, + expect_args: ["arg1"], + }, + { + argv: ["alias_sub_command_2_second", "arg1"], + expect_help: {{sub_2_help_message}}, + expect_args: ["arg1"], + }, + { + argv: ["sub_command_2", "arg1", "arg2"], + expect_help: {{sub_2_help_message}}, + expect_args: ["arg1", "arg2"], + }, + { + argv: ["alias_sub_command_2", "arg1", "arg2"], + expect_help: {{sub_2_help_message}}, + expect_args: ["arg1", "arg2"], + }, + { + argv: ["alias_sub_command_2_second", "arg1", "arg2"], + expect_help: {{sub_2_help_message}}, + expect_args: ["arg1", "arg2"], + }, + { + argv: ["sub_command_2", "arg1", "arg2", "arg3"], + expect_help: {{sub_2_help_message}}, + expect_args: ["arg1", "arg2", "arg3"], + }, + { + argv: ["alias_sub_command_2", "arg1", "arg2", "arg3"], + expect_help: {{sub_2_help_message}}, + expect_args: ["arg1", "arg2", "arg3"], + }, + { + argv: ["alias_sub_command_2_second", "arg1", "arg2", "arg3"], + expect_help: {{sub_2_help_message}}, + expect_args: ["arg1", "arg2", "arg3"], + }, + { + argv: ["sub_command_2", "--help", "-ignore-option"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["alias_sub_command_2", "--help", "-ignore-option"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["alias_sub_command_2_second", "--help", "-ignore-option"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["sub_command_2", "-ignore-option", "--help"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["alias_sub_command_2", "-ignore-option", "--help"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["alias_sub_command_2_second", "-ignore-option", "--help"], + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: ["sub_command_2", "-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["alias_sub_command_2", "-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["alias_sub_command_2_second", "-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["sub_command_2", "--missing-option"], + exception_message: "Undefined option. \"--missing-option\"", + }, + { + argv: ["alias_sub_command_2", "--missing-option"], + exception_message: "Undefined option. \"--missing-option\"", + }, + { + argv: ["alias_sub_command_2_second", "--missing-option"], + exception_message: "Undefined option. \"--missing-option\"", + }, + { + argv: ["sub_command_2", "-m", "arg1"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["alias_sub_command_2", "-m", "arg1"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["alias_sub_command_2_second", "-m", "arg1"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["sub_command_2", "arg1", "-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["alias_sub_command_2", "arg1", "-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["alias_sub_command_2_second", "arg1", "-m"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["sub_command_2", "-m", "-d"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["alias_sub_command_2", "-m", "-d"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["alias_sub_command_2_second", "-m", "-d"], + exception_message: "Undefined option. \"-m\"", + }, + { + argv: ["sub_command_2", "--help"], + expect_help: {{sub_2_help_message}}, + }, + { + argv: ["alias_sub_command_2", "--help"], + expect_help: {{sub_2_help_message}}, + }, + { + argv: ["alias_sub_command_2_second", "--help"], + expect_help: {{sub_2_help_message}}, + }, + { + argv: ["sub_command_2", "--help", "ignore-arg"], + expect_help: {{sub_2_help_message}}, + }, + { + argv: ["alias_sub_command_2", "--help", "ignore-arg"], + expect_help: {{sub_2_help_message}}, + }, + { + argv: ["alias_sub_command_2_second", "--help", "ignore-arg"], + expect_help: {{sub_2_help_message}}, + }, + { + argv: ["sub_command_2", "ignore-arg", "--help"], + expect_help: {{sub_2_help_message}}, + }, + { + argv: ["alias_sub_command_2", "ignore-arg", "--help"], + expect_help: {{sub_2_help_message}}, + }, + { + argv: ["alias_sub_command_2_second", "ignore-arg", "--help"], + expect_help: {{sub_2_help_message}}, + }, + ] +) +{% end %} + +class SubCommandWhenDuplicateAliasNameCase1 < Clim + main_command do + run do |opts, args| + end + sub "sub_command" do + alias_name "sub_command" # duplicate + run do |opts, args| + end + end + end +end + +describe "Call the command." do + it "raises an Exception when duplicate command name (case1)." do + expect_raises(Exception, "There are duplicate registered commands. [sub_command]") do + SubCommandWhenDuplicateAliasNameCase1.start_parse([] of String) + end + end +end + +class SubCommandWhenDuplicateAliasNameCase2 < Clim + main_command do + run do |opts, args| + end + sub "sub_command1" do + alias_name "sub_command1", "sub_command2", "sub_command2" # duplicate "sub_command1" and "sub_command2" + run do |opts, args| + end + end + end +end + +describe "Call the command." do + it "raises an Exception when duplicate command name (case2)." do + expect_raises(Exception, "There are duplicate registered commands. [sub_command1,sub_command2]") do + SubCommandWhenDuplicateAliasNameCase2.start_parse([] of String) + end + end +end + +class SubCommandWhenDuplicateAliasNameCase3 < Clim + main_command do + run do |opts, args| + end + sub "sub_command1" do + alias_name "alias_name1" + run do |opts, args| + end + end + sub "sub_command2" do + alias_name "alias_name2" + run do |opts, args| + end + end + sub "sub_command3" do + alias_name "sub_command1", "sub_command2", "alias_name1", "alias_name2" + run do |opts, args| + end + end + end +end + +describe "Call the command." do + it "raises an Exception when duplicate command name (case3)." do + expect_raises(Exception, "There are duplicate registered commands. [sub_command1,sub_command2,alias_name1,alias_name2]") do + SubCommandWhenDuplicateAliasNameCase3.start_parse([] of String) + end + end +end From 2ab521b91606775936fa63aa8b2400be7462defb Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Tue, 27 Nov 2018 00:08:07 +0900 Subject: [PATCH 4/5] fix readme --- README.md | 90 +++++++++++++++++++++++++++---------------------------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index 846ec1a2..b117e559 100644 --- a/README.md +++ b/README.md @@ -65,9 +65,9 @@ dependencies: require "clim" class MyCli < Clim - main_command do - run do |options, arguments| - puts "#{arguments.join(", ")}!" + main do + run do |opts, args| + puts "#{args.join(", ")}!" end end end @@ -90,15 +90,15 @@ require "clim" module Hello class Cli < Clim - main_command do + main do desc "Hello CLI tool." usage "hello [options] [arguments] ..." version "Version 0.1.0" option "-g WORDS", "--greeting=WORDS", type: String, desc: "Words of greetings.", default: "Hello" option "-n NAME", "--name=NAME", type: Array(String), desc: "Target name.", default: ["Taro"] - run do |options, arguments| - print "#{options.greeting}, " - print "#{options.name.join(", ")}!" + run do |opts, args| + print "#{opts.greeting}, " + print "#{opts.name.join(", ")}!" print "\n" end end @@ -138,30 +138,30 @@ require "clim" module FakeCrystalCommand class Cli < Clim - main_command do + main do desc "Fake Crystal command." usage "fcrystal [sub_command] [arguments]" - run do |options, arguments| - puts options.help # => help string. + run do |opts, args| + puts opts.help # => help string. end - sub_command "tool" do + sub "tool" do desc "run a tool" usage "fcrystal tool [tool] [arguments]" - run do |options, arguments| + run do |opts, args| puts "Fake Crystal tool!!" end - sub_command "format" do + sub "format" do desc "format project, directories and/or files" usage "fcrystal tool format [options] [file or directory]" - run do |options, arguments| + run do |opts, args| puts "Fake Crystal tool format!!" end end end - sub_command "spec" do + sub "spec" do desc "build and run specs" usage "fcrystal spec [options] [files]" - run do |options, arguments| + run do |opts, args| puts "Fake Crystal spec!!" end end @@ -245,9 +245,9 @@ Description of the command. It is displayed in Help. ```crystal class MyCli < Clim - main_command do + main do desc "My Command Line Interface." - run do |options, arguments| + run do |opts, args| # ... end end @@ -260,9 +260,9 @@ Usage of the command. It is displayed in Help. ```crystal class MyCli < Clim - main_command do + main do usage "mycli [sub-command] [options] ..." - run do |options, arguments| + run do |opts, args| # ... end end @@ -275,13 +275,13 @@ An alias for the command. It can be specified only for subcommand. ```crystal class MyCli < Clim - main_command do - run do |options, arguments| + main do + run do |opts, args| # ... end - sub_command "sub" do + sub "sub" do alias_name "alias1", "alias2" - run do |options, arguments| + run do |opts, args| puts "sub_command run!!" end end @@ -304,9 +304,9 @@ You can specify the string to be displayed with `--version`. ```crystal class MyCli < Clim - main_command do + main do version "mycli version: 1.0.1" - run do |options, arguments| + run do |opts, args| # ... end end @@ -322,9 +322,9 @@ If you want to display it even with `-v`, add ` short: "-v" `. ```crystal class MyCli < Clim - main_command do + main do version "mycli version: 1.0.1", short: "-v" - run do |options, arguments| + run do |opts, args| # ... end end @@ -353,12 +353,12 @@ You can specify multiple options for the command. ```crystal class MyCli < Clim - main_command do + main do option "--greeting=WORDS", desc: "Words of greetings.", default: "Hello" option "-n NAME", "--name=NAME", type: Array(String), desc: "Target name.", default: ["Taro"] - run do |options, arguments| - puts typeof(options.greeting) # => String - puts typeof(options.name) # => Array(String) + run do |opts, args| + puts typeof(opts.greeting) # => String + puts typeof(opts.name) # => Array(String) end end end @@ -408,10 +408,10 @@ For Bool, you do not need to specify arguments for short or long. ```crystal class MyCli < Clim - main_command do + main do option "-v", "--verbose", type: Bool, desc: "Verbose." - run do |options, arguments| - puts typeof(options.verbose) # => Bool + run do |opts, args| + puts typeof(opts.verbose) # => Bool end end end @@ -421,12 +421,12 @@ Option method names are long name if there is a long, and short name if there is ```crystal class MyCli < Clim - main_command do + main do option "-n", type: String, desc: "name." # => short name only. option "--my-age", type: Int32, desc: "age." # => long name only. - run do |options, arguments| - puts typeof(options.n) # => (String | Nil) - puts typeof(options.my_age) # => (Int32 | Nil) + run do |opts, args| + puts typeof(opts.n) # => (String | Nil) + puts typeof(opts.my_age) # => (Int32 | Nil) end end end @@ -434,7 +434,7 @@ end ### help_template -You can customize the help message. The `help_template` block must be placed before `main_command`. Also it needs to return `String`. Block arguments are `desc : String`, `usage : String`, `options_help : String` and `sub_commands_help : String`. +You can customize the help message. The `help_template` block must be placed before `main`. Also it needs to return `String`. Block arguments are `desc : String`, `usage : String`, `options_help : String` and `sub_commands_help : String`. ```crystal class MyCli < Clim @@ -453,13 +453,13 @@ class MyCli < Clim MY_HELP end - main_command do + main do desc "foo command." usage "foo [options] [arguments]" option "--site=SITE", type: String, desc: "Site URL." run do |opts, args| end - sub_command "sub_command" do + sub "sub_command" do desc "this is sub_comand." option "-n NUM", type: Int32, desc: "Number.", default: 0 run do |opts, args| @@ -488,9 +488,9 @@ $ crystal run src/help_template_test.cr -- --help ```crystal class MyCli < Clim - main_command do - run do |options, arguments| - options.help # => help string + main do + run do |opts, args| + opts.help # => help string end end end From 2ab14f79f569a06dc9af6a36e5521d2b229a7dcc Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Tue, 27 Nov 2018 00:15:33 +0900 Subject: [PATCH 5/5] fix version --- README.md | 2 +- shard.yml | 2 +- src/clim/version.cr | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b117e559..9cba0347 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ Add this to your application's `shard.yml`: dependencies: clim: github: at-grandpa/clim - version: 0.4.0 + version: 0.4.1 ``` ## Minimum sample diff --git a/shard.yml b/shard.yml index 2a5c6006..606d573f 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: clim -version: 0.4.0 +version: 0.4.1 authors: - at-grandpa <@at_grandpa> diff --git a/src/clim/version.cr b/src/clim/version.cr index 44f2437b..862ec050 100644 --- a/src/clim/version.cr +++ b/src/clim/version.cr @@ -1,3 +1,3 @@ class Clim - VERSION = "0.4.0" + VERSION = "0.4.1" end