diff --git a/README.md b/README.md index cf6aa4e5..6f8092e2 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,8 @@ Clim supports the following. - [x] string - [x] bool - [x] array +- [x] Default values for options +- [x] Required flag for options - [x] Nested sub commands - [x] `--help` option diff --git a/spec/clim/command_spec.cr b/spec/clim/command_spec.cr index 19b96ce6..0165dccb 100644 --- a/spec/clim/command_spec.cr +++ b/spec/clim/command_spec.cr @@ -84,55 +84,6 @@ describe Clim::Command do cmd.max_name_length.should eq(0) end end - describe "#duplicate_sub_command_name?" do - it "returns false when name not found in sub commands. (The same name as the main command.)" do - main_cmd = Command.new("main_cmd") - main_cmd.duplicate_sub_command_name?("main_cmd").should be_falsey - end - it "returns true when name found in sub commands." do - main_cmd = Command.new("main_cmd") - - sub_cmd1 = Command.new("sub_cmd1") - main_cmd.sub_cmds << sub_cmd1 - sub_cmd2 = Command.new("sub_cmd2") - main_cmd.sub_cmds << sub_cmd2 - - main_cmd.duplicate_sub_command_name?("sub_cmd1").should be_truthy - end - it "returns false when name not found in sub commands." do - main_cmd = Command.new("main_cmd") - - sub_cmd1 = Command.new("sub_cmd1") - main_cmd.sub_cmds << sub_cmd1 - sub_cmd2 = Command.new("sub_cmd2") - main_cmd.sub_cmds << sub_cmd2 - - main_cmd.duplicate_sub_command_name?("sub_cmd_not_duplicated").should be_falsey - end - it "returns false when name not found in sub commands. (It exists in the sub sub command.)" do - main_cmd = Command.new("main_cmd") - - sub_cmd = Command.new("sub_cmd") - main_cmd.sub_cmds << sub_cmd - - sub_sub_cmd = Command.new("sub_sub_cmd") - sub_cmd.sub_cmds << sub_sub_cmd - - main_cmd.duplicate_sub_command_name?("sub_sub_cmd").should be_falsey - end - it "returns true when name found in sub commands from sub_cmd to sub_sub_cmd." do - main_cmd = Command.new("main_cmd") - - sub_cmd = Command.new("sub_cmd") - main_cmd.sub_cmds << sub_cmd - - sub_sub_cmd = Command.new("sub_sub_cmd") - sub_cmd.sub_cmds << sub_sub_cmd - - sub_cmd.duplicate_sub_command_name?("sub_sub_cmd").should be_truthy - end - end - describe "#add_sub_commands?" do it "add sub command when command name is not duplicated." do main_cmd = Command.new("main_cmd") @@ -155,7 +106,6 @@ describe Clim::Command do end end end - describe "#parse" do main_cmd = Command.new("init") Spec.before_each do diff --git a/spec/clim/dsl_spec.cr b/spec/clim/dsl_spec.cr index 822de4c4..1fee7ee2 100644 --- a/spec/clim/dsl_spec.cr +++ b/spec/clim/dsl_spec.cr @@ -1,7 +1,7 @@ require "./../spec_helper" -def create_values(other : Hash(String, String | Bool | Array(String) | Nil) = {} of String => String | Bool | Array(String) | Nil) - values = Options::Values.new - values.hash.merge!(other) - values.hash +def create_opts_hash(other : Hash(String, String | Bool | Array(String) | Nil) = {} of String => String | Bool | Array(String) | Nil) + hash = Clim::ReturnOptsType.new + hash.merge!(other) + hash end diff --git a/spec/clim/dsl_spec/array_spec.cr b/spec/clim/dsl_spec/array_spec.cr index 74a41aea..c3ea90fc 100644 --- a/spec/clim/dsl_spec/array_spec.cr +++ b/spec/clim/dsl_spec/array_spec.cr @@ -22,12 +22,6 @@ describe "main command with array." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithArray.run_proc_arguments(spec_case[:argv]) @@ -55,52 +49,52 @@ describe "main command with array." do [ { argv: %w(), - expect_opts: create_values({"array" => nil}), + expect_opts: create_opts_hash({"array" => nil}), expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values({"array" => nil}), + expect_opts: create_opts_hash({"array" => nil}), expect_args: ["arg1"], }, { argv: %w(-a array1), - expect_opts: create_values({"array" => ["array1"]}), + expect_opts: create_opts_hash({"array" => ["array1"]}), expect_args: [] of String, }, { argv: %w(-aarray1), - expect_opts: create_values({"array" => ["array1"]}), + expect_opts: create_opts_hash({"array" => ["array1"]}), expect_args: [] of String, }, { argv: %w(--array array1), - expect_opts: create_values({"array" => ["array1"]}), + expect_opts: create_opts_hash({"array" => ["array1"]}), expect_args: [] of String, }, { argv: %w(--array=array1), - expect_opts: create_values({"array" => ["array1"]}), + expect_opts: create_opts_hash({"array" => ["array1"]}), expect_args: [] of String, }, { argv: %w(-a array1 arg1), - expect_opts: create_values({"array" => ["array1"]}), + expect_opts: create_opts_hash({"array" => ["array1"]}), expect_args: ["arg1"], }, { argv: %w(arg1 -a array1), - expect_opts: create_values({"array" => ["array1"]}), + expect_opts: create_opts_hash({"array" => ["array1"]}), expect_args: ["arg1"], }, { argv: %w(-array), # Unintended case. - expect_opts: create_values({"array" => ["rray"]}), + expect_opts: create_opts_hash({"array" => ["rray"]}), expect_args: [] of String, }, { argv: %w(-a=array1), # Unintended case. - expect_opts: create_values({"array" => ["=array1"]}), + expect_opts: create_opts_hash({"array" => ["=array1"]}), expect_args: [] of String, }, ].each do |spec_case| @@ -118,6 +112,14 @@ describe "main command with array." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(-a), exception_message: "Option that requires an argument. \"-a\"", @@ -165,12 +167,6 @@ describe "main command with array only short option." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithArrayOnlyShortOption.run_proc_arguments(spec_case[:argv]) @@ -198,42 +194,42 @@ describe "main command with array only short option." do [ { argv: %w(), - expect_opts: create_values({"a" => nil}), + expect_opts: create_opts_hash({"a" => nil}), expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values({"a" => nil}), + expect_opts: create_opts_hash({"a" => nil}), expect_args: ["arg1"], }, { argv: %w(-a array1), - expect_opts: create_values({"a" => ["array1"]}), + expect_opts: create_opts_hash({"a" => ["array1"]}), expect_args: [] of String, }, { argv: %w(-aarray1), - expect_opts: create_values({"a" => ["array1"]}), + expect_opts: create_opts_hash({"a" => ["array1"]}), expect_args: [] of String, }, { argv: %w(-a array1 arg1), - expect_opts: create_values({"a" => ["array1"]}), + expect_opts: create_opts_hash({"a" => ["array1"]}), expect_args: ["arg1"], }, { argv: %w(arg1 -a array1), - expect_opts: create_values({"a" => ["array1"]}), + expect_opts: create_opts_hash({"a" => ["array1"]}), expect_args: ["arg1"], }, { argv: %w(-array), # Unintended case. - expect_opts: create_values({"a" => ["rray"]}), + expect_opts: create_opts_hash({"a" => ["rray"]}), expect_args: [] of String, }, { argv: %w(-a=array1), # Unintended case. - expect_opts: create_values({"a" => ["=array1"]}), + expect_opts: create_opts_hash({"a" => ["=array1"]}), expect_args: [] of String, }, ].each do |spec_case| @@ -251,6 +247,14 @@ describe "main command with array only short option." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(-a), exception_message: "Option that requires an argument. \"-a\"", @@ -306,12 +310,6 @@ describe "main command with array only long option." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithArrayOnlyLongOption.run_proc_arguments(spec_case[:argv]) @@ -339,32 +337,32 @@ describe "main command with array only long option." do [ { argv: %w(), - expect_opts: create_values({"array" => nil}), + expect_opts: create_opts_hash({"array" => nil}), expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values({"array" => nil}), + expect_opts: create_opts_hash({"array" => nil}), expect_args: ["arg1"], }, { argv: %w(--array array1), - expect_opts: create_values({"array" => ["array1"]}), + expect_opts: create_opts_hash({"array" => ["array1"]}), expect_args: [] of String, }, { argv: %w(--array=array1), - expect_opts: create_values({"array" => ["array1"]}), + expect_opts: create_opts_hash({"array" => ["array1"]}), expect_args: [] of String, }, { argv: %w(--array array1 arg1), - expect_opts: create_values({"array" => ["array1"]}), + expect_opts: create_opts_hash({"array" => ["array1"]}), expect_args: ["arg1"], }, { argv: %w(arg1 --array array1), - expect_opts: create_values({"array" => ["array1"]}), + expect_opts: create_opts_hash({"array" => ["array1"]}), expect_args: ["arg1"], }, ].each do |spec_case| @@ -382,6 +380,14 @@ describe "main command with array only long option." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(--array), exception_message: "Option that requires an argument. \"--array\"", @@ -437,12 +443,6 @@ describe "main command with array desc." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithArrayDesc.run_proc_arguments(spec_case[:argv]) @@ -489,12 +489,6 @@ describe "main command with array default." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithArrayDefault.run_proc_arguments(spec_case[:argv]) @@ -522,52 +516,52 @@ describe "main command with array default." do [ { argv: %w(), - expect_opts: create_values({"array" => ["default value"]}), + expect_opts: create_opts_hash({"array" => ["default value"]}), expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values({"array" => ["default value"]}), + expect_opts: create_opts_hash({"array" => ["default value"]}), expect_args: ["arg1"], }, { argv: %w(-a array1), - expect_opts: create_values({"array" => ["default value", "array1"]}), + expect_opts: create_opts_hash({"array" => ["default value", "array1"]}), expect_args: [] of String, }, { argv: %w(-aarray1), - expect_opts: create_values({"array" => ["default value", "array1"]}), + expect_opts: create_opts_hash({"array" => ["default value", "array1"]}), expect_args: [] of String, }, { argv: %w(--array array1), - expect_opts: create_values({"array" => ["default value", "array1"]}), + expect_opts: create_opts_hash({"array" => ["default value", "array1"]}), expect_args: [] of String, }, { argv: %w(--array=array1), - expect_opts: create_values({"array" => ["default value", "array1"]}), + expect_opts: create_opts_hash({"array" => ["default value", "array1"]}), expect_args: [] of String, }, { argv: %w(-a array1 arg1), - expect_opts: create_values({"array" => ["default value", "array1"]}), + expect_opts: create_opts_hash({"array" => ["default value", "array1"]}), expect_args: ["arg1"], }, { argv: %w(arg1 -a array1), - expect_opts: create_values({"array" => ["default value", "array1"]}), + expect_opts: create_opts_hash({"array" => ["default value", "array1"]}), expect_args: ["arg1"], }, { argv: %w(-array), # Unintended case. - expect_opts: create_values({"array" => ["default value", "rray"]}), + expect_opts: create_opts_hash({"array" => ["default value", "rray"]}), expect_args: [] of String, }, { argv: %w(-a=array1), # Unintended case. - expect_opts: create_values({"array" => ["default value", "=array1"]}), + expect_opts: create_opts_hash({"array" => ["default value", "=array1"]}), expect_args: [] of String, }, ].each do |spec_case| @@ -585,6 +579,14 @@ describe "main command with array default." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(-a), exception_message: "Option that requires an argument. \"-a\"", @@ -632,12 +634,6 @@ describe "main command with array required true and default exists." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithArrayRequiredTrueAndDefaultExists.run_proc_arguments(spec_case[:argv]) @@ -665,52 +661,52 @@ describe "main command with array required true and default exists." do [ { argv: %w(), - expect_opts: create_values({"array" => ["default value"]}), + expect_opts: create_opts_hash({"array" => ["default value"]}), expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values({"array" => ["default value"]}), + expect_opts: create_opts_hash({"array" => ["default value"]}), expect_args: ["arg1"], }, { argv: %w(-a array1), - expect_opts: create_values({"array" => ["default value", "array1"]}), + expect_opts: create_opts_hash({"array" => ["default value", "array1"]}), expect_args: [] of String, }, { argv: %w(-aarray1), - expect_opts: create_values({"array" => ["default value", "array1"]}), + expect_opts: create_opts_hash({"array" => ["default value", "array1"]}), expect_args: [] of String, }, { argv: %w(--array array1), - expect_opts: create_values({"array" => ["default value", "array1"]}), + expect_opts: create_opts_hash({"array" => ["default value", "array1"]}), expect_args: [] of String, }, { argv: %w(--array=array1), - expect_opts: create_values({"array" => ["default value", "array1"]}), + expect_opts: create_opts_hash({"array" => ["default value", "array1"]}), expect_args: [] of String, }, { argv: %w(-a array1 arg1), - expect_opts: create_values({"array" => ["default value", "array1"]}), + expect_opts: create_opts_hash({"array" => ["default value", "array1"]}), expect_args: ["arg1"], }, { argv: %w(arg1 -a array1), - expect_opts: create_values({"array" => ["default value", "array1"]}), + expect_opts: create_opts_hash({"array" => ["default value", "array1"]}), expect_args: ["arg1"], }, { argv: %w(-array), # Unintended case. - expect_opts: create_values({"array" => ["default value", "rray"]}), + expect_opts: create_opts_hash({"array" => ["default value", "rray"]}), expect_args: [] of String, }, { argv: %w(-a=array1), # Unintended case. - expect_opts: create_values({"array" => ["default value", "=array1"]}), + expect_opts: create_opts_hash({"array" => ["default value", "=array1"]}), expect_args: [] of String, }, ].each do |spec_case| @@ -728,6 +724,14 @@ describe "main command with array required true and default exists." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(-a), exception_message: "Option that requires an argument. \"-a\"", @@ -775,12 +779,6 @@ describe "main command with array required true only." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithArrayRequiredTrueOnly.run_proc_arguments(spec_case[:argv]) @@ -808,42 +806,42 @@ describe "main command with array required true only." do [ { argv: %w(-a array1), - expect_opts: create_values({"array" => ["array1"]}), + expect_opts: create_opts_hash({"array" => ["array1"]}), expect_args: [] of String, }, { argv: %w(-aarray1), - expect_opts: create_values({"array" => ["array1"]}), + expect_opts: create_opts_hash({"array" => ["array1"]}), expect_args: [] of String, }, { argv: %w(--array array1), - expect_opts: create_values({"array" => ["array1"]}), + expect_opts: create_opts_hash({"array" => ["array1"]}), expect_args: [] of String, }, { argv: %w(--array=array1), - expect_opts: create_values({"array" => ["array1"]}), + expect_opts: create_opts_hash({"array" => ["array1"]}), expect_args: [] of String, }, { argv: %w(-a array1 arg1), - expect_opts: create_values({"array" => ["array1"]}), + expect_opts: create_opts_hash({"array" => ["array1"]}), expect_args: ["arg1"], }, { argv: %w(arg1 -a array1), - expect_opts: create_values({"array" => ["array1"]}), + expect_opts: create_opts_hash({"array" => ["array1"]}), expect_args: ["arg1"], }, { argv: %w(-array), # Unintended case. - expect_opts: create_values({"array" => ["rray"]}), + expect_opts: create_opts_hash({"array" => ["rray"]}), expect_args: [] of String, }, { argv: %w(-a=array1), # Unintended case. - expect_opts: create_values({"array" => ["=array1"]}), + expect_opts: create_opts_hash({"array" => ["=array1"]}), expect_args: [] of String, }, ].each do |spec_case| @@ -865,6 +863,14 @@ describe "main command with array required true only." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(arg1), exception_message: "Required options. \"-a ARG\"", @@ -916,12 +922,6 @@ describe "main command with array required false and default exists." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithArrayRequiredFalseAndDefaultExists.run_proc_arguments(spec_case[:argv]) @@ -949,52 +949,52 @@ describe "main command with array required false and default exists." do [ { argv: %w(), - expect_opts: create_values({"array" => ["default value"]}), + expect_opts: create_opts_hash({"array" => ["default value"]}), expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values({"array" => ["default value"]}), + expect_opts: create_opts_hash({"array" => ["default value"]}), expect_args: ["arg1"], }, { argv: %w(-a array1), - expect_opts: create_values({"array" => ["default value", "array1"]}), + expect_opts: create_opts_hash({"array" => ["default value", "array1"]}), expect_args: [] of String, }, { argv: %w(-aarray1), - expect_opts: create_values({"array" => ["default value", "array1"]}), + expect_opts: create_opts_hash({"array" => ["default value", "array1"]}), expect_args: [] of String, }, { argv: %w(--array array1), - expect_opts: create_values({"array" => ["default value", "array1"]}), + expect_opts: create_opts_hash({"array" => ["default value", "array1"]}), expect_args: [] of String, }, { argv: %w(--array=array1), - expect_opts: create_values({"array" => ["default value", "array1"]}), + expect_opts: create_opts_hash({"array" => ["default value", "array1"]}), expect_args: [] of String, }, { argv: %w(-a array1 arg1), - expect_opts: create_values({"array" => ["default value", "array1"]}), + expect_opts: create_opts_hash({"array" => ["default value", "array1"]}), expect_args: ["arg1"], }, { argv: %w(arg1 -a array1), - expect_opts: create_values({"array" => ["default value", "array1"]}), + expect_opts: create_opts_hash({"array" => ["default value", "array1"]}), expect_args: ["arg1"], }, { argv: %w(-array), # Unintended case. - expect_opts: create_values({"array" => ["default value", "rray"]}), + expect_opts: create_opts_hash({"array" => ["default value", "rray"]}), expect_args: [] of String, }, { argv: %w(-a=array1), # Unintended case. - expect_opts: create_values({"array" => ["default value", "=array1"]}), + expect_opts: create_opts_hash({"array" => ["default value", "=array1"]}), expect_args: [] of String, }, ].each do |spec_case| @@ -1012,6 +1012,14 @@ describe "main command with array required false and default exists." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(-a), exception_message: "Option that requires an argument. \"-a\"", @@ -1059,12 +1067,6 @@ describe "main command with array required false only." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithArrayRequiredFalseOnly.run_proc_arguments(spec_case[:argv]) @@ -1092,52 +1094,52 @@ describe "main command with array required false only." do [ { argv: %w(), - expect_opts: create_values({"array" => nil}), + expect_opts: create_opts_hash({"array" => nil}), expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values({"array" => nil}), + expect_opts: create_opts_hash({"array" => nil}), expect_args: ["arg1"], }, { argv: %w(-a array1), - expect_opts: create_values({"array" => ["array1"]}), + expect_opts: create_opts_hash({"array" => ["array1"]}), expect_args: [] of String, }, { argv: %w(-aarray1), - expect_opts: create_values({"array" => ["array1"]}), + expect_opts: create_opts_hash({"array" => ["array1"]}), expect_args: [] of String, }, { argv: %w(--array array1), - expect_opts: create_values({"array" => ["array1"]}), + expect_opts: create_opts_hash({"array" => ["array1"]}), expect_args: [] of String, }, { argv: %w(--array=array1), - expect_opts: create_values({"array" => ["array1"]}), + expect_opts: create_opts_hash({"array" => ["array1"]}), expect_args: [] of String, }, { argv: %w(-a array1 arg1), - expect_opts: create_values({"array" => ["array1"]}), + expect_opts: create_opts_hash({"array" => ["array1"]}), expect_args: ["arg1"], }, { argv: %w(arg1 -a array1), - expect_opts: create_values({"array" => ["array1"]}), + expect_opts: create_opts_hash({"array" => ["array1"]}), expect_args: ["arg1"], }, { argv: %w(-array), # Unintended case. - expect_opts: create_values({"array" => ["rray"]}), + expect_opts: create_opts_hash({"array" => ["rray"]}), expect_args: [] of String, }, { argv: %w(-a=array1), # Unintended case. - expect_opts: create_values({"array" => ["=array1"]}), + expect_opts: create_opts_hash({"array" => ["=array1"]}), expect_args: [] of String, }, ].each do |spec_case| @@ -1155,6 +1157,14 @@ describe "main command with array required false only." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(-a), exception_message: "Option that requires an argument. \"-a\"", diff --git a/spec/clim/dsl_spec/bool_spec.cr b/spec/clim/dsl_spec/bool_spec.cr index df13c987..0afe76de 100644 --- a/spec/clim/dsl_spec/bool_spec.cr +++ b/spec/clim/dsl_spec/bool_spec.cr @@ -21,12 +21,6 @@ describe "main command with bool." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithBool.run_proc_arguments(spec_case[:argv]) @@ -54,42 +48,42 @@ describe "main command with bool." do [ { argv: %w(), - expect_opts: create_values({"bool" => nil}), + expect_opts: create_opts_hash({"bool" => nil}), expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values({"bool" => nil}), + expect_opts: create_opts_hash({"bool" => nil}), expect_args: ["arg1"], }, { argv: %w(-b), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: [] of String, }, { argv: %w(--bool), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: [] of String, }, { argv: %w(-b arg1), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(--bool arg1), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(arg1 -b), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(arg1 --bool), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, ].each do |spec_case| @@ -107,6 +101,14 @@ describe "main command with bool." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(--b), exception_message: "Undefined option. \"--b\"", @@ -146,12 +148,6 @@ describe "main command with bool only short option." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithBoolOnlyShortOption.run_proc_arguments(spec_case[:argv]) @@ -179,27 +175,27 @@ describe "main command with bool only short option." do [ { argv: %w(), - expect_opts: create_values({"b" => nil}), + expect_opts: create_opts_hash({"b" => nil}), expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values({"b" => nil}), + expect_opts: create_opts_hash({"b" => nil}), expect_args: ["arg1"], }, { argv: %w(-b), - expect_opts: create_values({"b" => true}), + expect_opts: create_opts_hash({"b" => true}), expect_args: [] of String, }, { argv: %w(-b arg1), - expect_opts: create_values({"b" => true}), + expect_opts: create_opts_hash({"b" => true}), expect_args: ["arg1"], }, { argv: %w(arg1 -b), - expect_opts: create_values({"b" => true}), + expect_opts: create_opts_hash({"b" => true}), expect_args: ["arg1"], }, ].each do |spec_case| @@ -217,6 +213,14 @@ describe "main command with bool only short option." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(--bool), exception_message: "Undefined option. \"--bool\"", @@ -264,12 +268,6 @@ describe "main command with bool only long option." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithBoolOnlyLongOption.run_proc_arguments(spec_case[:argv]) @@ -297,27 +295,27 @@ describe "main command with bool only long option." do [ { argv: %w(), - expect_opts: create_values({"bool" => nil}), + expect_opts: create_opts_hash({"bool" => nil}), expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values({"bool" => nil}), + expect_opts: create_opts_hash({"bool" => nil}), expect_args: ["arg1"], }, { argv: %w(--bool), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: [] of String, }, { argv: %w(--bool arg1), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(arg1 --bool), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, ].each do |spec_case| @@ -335,6 +333,14 @@ describe "main command with bool only long option." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(-b), exception_message: "Undefined option. \"-b\"", @@ -378,12 +384,6 @@ describe "main command with bool arguments." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithBoolArguments.run_proc_arguments(spec_case[:argv]) @@ -411,32 +411,32 @@ describe "main command with bool arguments." do [ { argv: %w(), - expect_opts: create_values({"bool" => nil}), + expect_opts: create_opts_hash({"bool" => nil}), expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values({"bool" => nil}), + expect_opts: create_opts_hash({"bool" => nil}), expect_args: ["arg1"], }, { argv: %w(-b true), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: [] of String, }, { argv: %w(-b false), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: [] of String, }, { argv: %w(--bool true), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: [] of String, }, { argv: %w(--bool false), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: [] of String, }, ].each do |spec_case| @@ -454,6 +454,14 @@ describe "main command with bool arguments." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(-b), exception_message: "Option that requires an argument. \"-b\"", @@ -517,12 +525,6 @@ describe "main command with bool." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithBoolArgumentsOnlyShortOption.run_proc_arguments(spec_case[:argv]) @@ -550,22 +552,22 @@ describe "main command with bool." do [ { argv: %w(), - expect_opts: create_values({"b" => nil}), + expect_opts: create_opts_hash({"b" => nil}), expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values({"b" => nil}), + expect_opts: create_opts_hash({"b" => nil}), expect_args: ["arg1"], }, { argv: %w(-b true), - expect_opts: create_values({"b" => true}), + expect_opts: create_opts_hash({"b" => true}), expect_args: [] of String, }, { argv: %w(-b false), - expect_opts: create_values({"b" => false}), + expect_opts: create_opts_hash({"b" => false}), expect_args: [] of String, }, ].each do |spec_case| @@ -583,6 +585,14 @@ describe "main command with bool." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(-b), exception_message: "Option that requires an argument. \"-b\"", @@ -654,12 +664,6 @@ describe "main command with bool desc." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithBoolDesc.run_proc_arguments(spec_case[:argv]) @@ -706,12 +710,6 @@ describe "main command with bool default." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithBoolDefault.run_proc_arguments(spec_case[:argv]) @@ -739,32 +737,32 @@ describe "main command with bool default." do [ { argv: %w(), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: ["arg1"], }, { argv: %w(-b), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: [] of String, }, { argv: %w(--bool), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: [] of String, }, { argv: %w(-b arg1), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(arg1 -b), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, ].each do |spec_case| @@ -782,6 +780,14 @@ describe "main command with bool default." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(--b), exception_message: "Undefined option. \"--b\"", @@ -821,12 +827,6 @@ describe "main command with bool required true and default exists." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithBoolRequiredTrueAndDefaultExists.run_proc_arguments(spec_case[:argv]) @@ -854,42 +854,42 @@ describe "main command with bool required true and default exists." do [ { argv: %w(), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: ["arg1"], }, { argv: %w(-b), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: [] of String, }, { argv: %w(--bool), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: [] of String, }, { argv: %w(-b arg1), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(arg1 -b), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(--bool arg1), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(arg1 --bool), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, ].each do |spec_case| @@ -924,12 +924,6 @@ describe "main command with bool arguments required true and default exists." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithBoolArgumentsRequiredTrueAndDefaultExists.run_proc_arguments(spec_case[:argv]) @@ -957,72 +951,72 @@ describe "main command with bool arguments required true and default exists." do [ { argv: %w(), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: ["arg1"], }, { argv: %w(-b true), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: [] of String, }, { argv: %w(-b true arg1), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(arg1 -b true), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(-b false), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: [] of String, }, { argv: %w(-b false arg1), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: ["arg1"], }, { argv: %w(arg1 -b false), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: ["arg1"], }, { argv: %w(--bool true), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: [] of String, }, { argv: %w(--bool true arg1), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(arg1 --bool true), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(--bool false), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: [] of String, }, { argv: %w(--bool false arg1), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: ["arg1"], }, { argv: %w(arg1 --bool false), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: ["arg1"], }, ].each do |spec_case| @@ -1040,6 +1034,14 @@ describe "main command with bool arguments required true and default exists." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(-b), exception_message: "Option that requires an argument. \"-b\"", @@ -1103,12 +1105,6 @@ describe "main command with bool required true only." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithBoolRequiredTrueOnly.run_proc_arguments(spec_case[:argv]) @@ -1136,32 +1132,32 @@ describe "main command with bool required true only." do [ { argv: %w(-b), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: [] of String, }, { argv: %w(--bool), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: [] of String, }, { argv: %w(-b arg1), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(arg1 -b), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(--bool arg1), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(arg1 --bool), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, ].each do |spec_case| @@ -1179,6 +1175,14 @@ describe "main command with bool required true only." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(), exception_message: "Required options. \"-b\"", @@ -1218,12 +1222,6 @@ describe "main command with bool arguments required true only." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithBoolArgumentsRequiredTrueOnly.run_proc_arguments(spec_case[:argv]) @@ -1251,62 +1249,62 @@ describe "main command with bool arguments required true only." do [ { argv: %w(-b true), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: [] of String, }, { argv: %w(-b true arg1), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(arg1 -b true), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(-b false), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: [] of String, }, { argv: %w(-b false arg1), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: ["arg1"], }, { argv: %w(arg1 -b false), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: ["arg1"], }, { argv: %w(--bool true), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: [] of String, }, { argv: %w(--bool true arg1), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(arg1 --bool true), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(--bool false), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: [] of String, }, { argv: %w(--bool false arg1), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: ["arg1"], }, { argv: %w(arg1 --bool false), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: ["arg1"], }, ].each do |spec_case| @@ -1328,6 +1326,14 @@ describe "main command with bool arguments required true only." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(arg1), exception_message: "Required options. \"-b ARG\"", @@ -1395,12 +1401,6 @@ describe "main command with bool required false and default exists." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithBoolRequiredFalseAndDefaultExists.run_proc_arguments(spec_case[:argv]) @@ -1428,42 +1428,42 @@ describe "main command with bool required false and default exists." do [ { argv: %w(), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: ["arg1"], }, { argv: %w(-b), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: [] of String, }, { argv: %w(--bool), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: [] of String, }, { argv: %w(-b arg1), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(arg1 -b), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(--bool arg1), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(arg1 --bool), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, ].each do |spec_case| @@ -1501,12 +1501,6 @@ describe "main command with bool arguments required false and default exists." d { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithBoolArgumentsRequiredFalseAndDefaultExists.run_proc_arguments(spec_case[:argv]) @@ -1534,72 +1528,72 @@ describe "main command with bool arguments required false and default exists." d [ { argv: %w(), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: ["arg1"], }, { argv: %w(-b true), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: [] of String, }, { argv: %w(-b true arg1), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(arg1 -b true), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(-b false), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: [] of String, }, { argv: %w(-b false arg1), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: ["arg1"], }, { argv: %w(arg1 -b false), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: ["arg1"], }, { argv: %w(--bool true), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: [] of String, }, { argv: %w(--bool true arg1), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(arg1 --bool true), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(--bool false), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: [] of String, }, { argv: %w(--bool false arg1), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: ["arg1"], }, { argv: %w(arg1 --bool false), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: ["arg1"], }, ].each do |spec_case| @@ -1617,6 +1611,14 @@ describe "main command with bool arguments required false and default exists." d argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(-b), exception_message: "Option that requires an argument. \"-b\"", @@ -1680,12 +1682,6 @@ describe "main command with bool required false only." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithBoolRequiredFalseOnly.run_proc_arguments(spec_case[:argv]) @@ -1713,42 +1709,42 @@ describe "main command with bool required false only." do [ { argv: %w(), - expect_opts: create_values({"bool" => nil}), + expect_opts: create_opts_hash({"bool" => nil}), expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values({"bool" => nil}), + expect_opts: create_opts_hash({"bool" => nil}), expect_args: ["arg1"], }, { argv: %w(-b), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: [] of String, }, { argv: %w(--bool), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: [] of String, }, { argv: %w(-b arg1), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(arg1 -b), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(--bool arg1), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(arg1 --bool), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, ].each do |spec_case| @@ -1786,12 +1782,6 @@ describe "main command with bool arguments required false only." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithBoolArgumentsRequiredFalseOnly.run_proc_arguments(spec_case[:argv]) @@ -1819,72 +1809,72 @@ describe "main command with bool arguments required false only." do [ { argv: %w(), - expect_opts: create_values({"bool" => nil}), + expect_opts: create_opts_hash({"bool" => nil}), expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values({"bool" => nil}), + expect_opts: create_opts_hash({"bool" => nil}), expect_args: ["arg1"], }, { argv: %w(-b true), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: [] of String, }, { argv: %w(-b true arg1), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(arg1 -b true), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(-b false), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: [] of String, }, { argv: %w(-b false arg1), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: ["arg1"], }, { argv: %w(arg1 -b false), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: ["arg1"], }, { argv: %w(--bool true), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: [] of String, }, { argv: %w(--bool true arg1), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(arg1 --bool true), - expect_opts: create_values({"bool" => true}), + expect_opts: create_opts_hash({"bool" => true}), expect_args: ["arg1"], }, { argv: %w(--bool false), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: [] of String, }, { argv: %w(--bool false arg1), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: ["arg1"], }, { argv: %w(arg1 --bool false), - expect_opts: create_values({"bool" => false}), + expect_opts: create_opts_hash({"bool" => false}), expect_args: ["arg1"], }, ].each do |spec_case| @@ -1902,6 +1892,14 @@ describe "main command with bool arguments required false only." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(-b), exception_message: "Option that requires an argument. \"-b\"", diff --git a/spec/clim/dsl_spec/main_command_spec.cr b/spec/clim/dsl_spec/main_command_spec.cr index e99af340..b4594606 100644 --- a/spec/clim/dsl_spec/main_command_spec.cr +++ b/spec/clim/dsl_spec/main_command_spec.cr @@ -18,12 +18,6 @@ describe "main command only." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandOnly.run_proc_arguments(spec_case[:argv]) @@ -50,22 +44,22 @@ describe "main command only." do [ { argv: %w(), - expect_opts: create_values, + expect_opts: create_opts_hash, expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values, + expect_opts: create_opts_hash, expect_args: ["arg1"], }, { argv: %w(arg1 arg2), - expect_opts: create_values, + expect_opts: create_opts_hash, expect_args: ["arg1", "arg2"], }, { argv: %w(arg1 arg2 arg3), - expect_opts: create_values, + expect_opts: create_opts_hash, expect_args: ["arg1", "arg2", "arg3"], }, ].each do |spec_case| @@ -83,6 +77,14 @@ describe "main command only." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(-m), exception_message: "Undefined option. \"-m\"", @@ -132,12 +134,6 @@ describe "main command with desc." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithDesc.run_proc_arguments(spec_case[:argv]) @@ -164,22 +160,22 @@ describe "main command with desc." do [ { argv: %w(), - expect_opts: create_values, + expect_opts: create_opts_hash, expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values, + expect_opts: create_opts_hash, expect_args: ["arg1"], }, { argv: %w(arg1 arg2), - expect_opts: create_values, + expect_opts: create_opts_hash, expect_args: ["arg1", "arg2"], }, { argv: %w(arg1 arg2 arg3), - expect_opts: create_values, + expect_opts: create_opts_hash, expect_args: ["arg1", "arg2", "arg3"], }, ].each do |spec_case| @@ -197,6 +193,14 @@ describe "main command with desc." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(-m), exception_message: "Undefined option. \"-m\"", @@ -247,12 +251,6 @@ describe "main command with usage." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithUsage.run_proc_arguments(spec_case[:argv]) @@ -279,22 +277,22 @@ describe "main command with usage." do [ { argv: %w(), - expect_opts: create_values, + expect_opts: create_opts_hash, expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values, + expect_opts: create_opts_hash, expect_args: ["arg1"], }, { argv: %w(arg1 arg2), - expect_opts: create_values, + expect_opts: create_opts_hash, expect_args: ["arg1", "arg2"], }, { argv: %w(arg1 arg2 arg3), - expect_opts: create_values, + expect_opts: create_opts_hash, expect_args: ["arg1", "arg2", "arg3"], }, ].each do |spec_case| @@ -312,6 +310,14 @@ describe "main command with usage." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(-m), exception_message: "Undefined option. \"-m\"", @@ -639,7 +645,7 @@ describe "Call the main command." do it "raises an Exception because execute help block." do main_command = Command.new("main_command") main_command.help_proc = SpecMainCommandExecuteRunBlock::RunProc.new { raise "Help block was executed." } - main_command.run_proc = SpecMainCommandExecuteRunBlock::RunProc.new { raise "Run block was executed." } # This should not be called. + main_command.run_proc = SpecMainCommandExecuteRunBlock::RunProc.new { raise "Run block was executed." } # This should not be called. expect_raises(Exception, "Help block was executed.") do SpecMainCommandExecuteRunBlock.start_main(%w(--help), main_command) end diff --git a/spec/clim/dsl_spec/string_spec.cr b/spec/clim/dsl_spec/string_spec.cr index 817d79eb..91d873a5 100644 --- a/spec/clim/dsl_spec/string_spec.cr +++ b/spec/clim/dsl_spec/string_spec.cr @@ -21,12 +21,6 @@ describe "main command with string." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithString.run_proc_arguments(spec_case[:argv]) @@ -54,52 +48,52 @@ describe "main command with string." do [ { argv: %w(), - expect_opts: create_values({"string" => nil}), + expect_opts: create_opts_hash({"string" => nil}), expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values({"string" => nil}), + expect_opts: create_opts_hash({"string" => nil}), expect_args: ["arg1"], }, { argv: %w(-s string1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: [] of String, }, { argv: %w(-sstring1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: [] of String, }, { argv: %w(--string string1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: [] of String, }, { argv: %w(--string=string1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: [] of String, }, { argv: %w(-s string1 arg1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: ["arg1"], }, { argv: %w(arg1 -s string1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: ["arg1"], }, { argv: %w(-string), # Unintended case. - expect_opts: create_values({"string" => "tring"}), + expect_opts: create_opts_hash({"string" => "tring"}), expect_args: [] of String, }, { argv: %w(-s=string1), # Unintended case. - expect_opts: create_values({"string" => "=string1"}), + expect_opts: create_opts_hash({"string" => "=string1"}), expect_args: [] of String, }, ].each do |spec_case| @@ -117,6 +111,14 @@ describe "main command with string." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(-s), exception_message: "Option that requires an argument. \"-s\"", @@ -164,12 +166,6 @@ describe "main command with string only short option." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithStringOnlyShortOption.run_proc_arguments(spec_case[:argv]) @@ -197,42 +193,42 @@ describe "main command with string only short option." do [ { argv: %w(), - expect_opts: create_values({"s" => nil}), + expect_opts: create_opts_hash({"s" => nil}), expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values({"s" => nil}), + expect_opts: create_opts_hash({"s" => nil}), expect_args: ["arg1"], }, { argv: %w(-s string1), - expect_opts: create_values({"s" => "string1"}), + expect_opts: create_opts_hash({"s" => "string1"}), expect_args: [] of String, }, { argv: %w(-sstring1), - expect_opts: create_values({"s" => "string1"}), + expect_opts: create_opts_hash({"s" => "string1"}), expect_args: [] of String, }, { argv: %w(-s string1 arg1), - expect_opts: create_values({"s" => "string1"}), + expect_opts: create_opts_hash({"s" => "string1"}), expect_args: ["arg1"], }, { argv: %w(arg1 -s string1), - expect_opts: create_values({"s" => "string1"}), + expect_opts: create_opts_hash({"s" => "string1"}), expect_args: ["arg1"], }, { argv: %w(-string), # Unintended case. - expect_opts: create_values({"s" => "tring"}), + expect_opts: create_opts_hash({"s" => "tring"}), expect_args: [] of String, }, { argv: %w(-s=string1), # Unintended case. - expect_opts: create_values({"s" => "=string1"}), + expect_opts: create_opts_hash({"s" => "=string1"}), expect_args: [] of String, }, ].each do |spec_case| @@ -250,6 +246,14 @@ describe "main command with string only short option." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(-s), exception_message: "Option that requires an argument. \"-s\"", @@ -305,12 +309,6 @@ describe "main command with string only long option." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithStringOnlyLongOption.run_proc_arguments(spec_case[:argv]) @@ -338,32 +336,32 @@ describe "main command with string only long option." do [ { argv: %w(), - expect_opts: create_values({"string" => nil}), + expect_opts: create_opts_hash({"string" => nil}), expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values({"string" => nil}), + expect_opts: create_opts_hash({"string" => nil}), expect_args: ["arg1"], }, { argv: %w(--string=string1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: [] of String, }, { argv: %w(--string string1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: [] of String, }, { argv: %w(--string string1 arg1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: ["arg1"], }, { argv: %w(arg1 --string string1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: ["arg1"], }, ].each do |spec_case| @@ -381,6 +379,14 @@ describe "main command with string only long option." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(--string), exception_message: "Option that requires an argument. \"--string\"", @@ -432,12 +438,6 @@ describe "main command with string desc." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithStringDesc.run_proc_arguments(spec_case[:argv]) @@ -484,12 +484,6 @@ describe "main command with string default." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithStringDefault.run_proc_arguments(spec_case[:argv]) @@ -517,52 +511,52 @@ describe "main command with string default." do [ { argv: %w(), - expect_opts: create_values({"string" => "default value"}), + expect_opts: create_opts_hash({"string" => "default value"}), expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values({"string" => "default value"}), + expect_opts: create_opts_hash({"string" => "default value"}), expect_args: ["arg1"], }, { argv: %w(-s string1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: [] of String, }, { argv: %w(-sstring1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: [] of String, }, { argv: %w(--string string1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: [] of String, }, { argv: %w(--string=string1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: [] of String, }, { argv: %w(-s string1 arg1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: ["arg1"], }, { argv: %w(arg1 -s string1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: ["arg1"], }, { argv: %w(-string), # Unintended case. - expect_opts: create_values({"string" => "tring"}), + expect_opts: create_opts_hash({"string" => "tring"}), expect_args: [] of String, }, { argv: %w(-s=string1), # Unintended case. - expect_opts: create_values({"string" => "=string1"}), + expect_opts: create_opts_hash({"string" => "=string1"}), expect_args: [] of String, }, ].each do |spec_case| @@ -580,6 +574,14 @@ describe "main command with string default." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(-s), exception_message: "Option that requires an argument. \"-s\"", @@ -627,12 +629,6 @@ describe "main command with string required true and default exists." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithStringRequiredTrueAndDefaultExists.run_proc_arguments(spec_case[:argv]) @@ -660,52 +656,52 @@ describe "main command with string required true and default exists." do [ { argv: %w(), - expect_opts: create_values({"string" => "default value"}), + expect_opts: create_opts_hash({"string" => "default value"}), expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values({"string" => "default value"}), + expect_opts: create_opts_hash({"string" => "default value"}), expect_args: ["arg1"], }, { argv: %w(-s string1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: [] of String, }, { argv: %w(-sstring1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: [] of String, }, { argv: %w(--string string1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: [] of String, }, { argv: %w(--string=string1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: [] of String, }, { argv: %w(-s string1 arg1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: ["arg1"], }, { argv: %w(arg1 -s string1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: ["arg1"], }, { argv: %w(-string), # Unintended case. - expect_opts: create_values({"string" => "tring"}), + expect_opts: create_opts_hash({"string" => "tring"}), expect_args: [] of String, }, { argv: %w(-s=string1), # Unintended case. - expect_opts: create_values({"string" => "=string1"}), + expect_opts: create_opts_hash({"string" => "=string1"}), expect_args: [] of String, }, ].each do |spec_case| @@ -723,6 +719,14 @@ describe "main command with string required true and default exists." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(-s), exception_message: "Option that requires an argument. \"-s\"", @@ -770,12 +774,6 @@ describe "main command with string required only." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithStringRequiredTrueOnly.run_proc_arguments(spec_case[:argv]) @@ -803,42 +801,42 @@ describe "main command with string required only." do [ { argv: %w(-s string1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: [] of String, }, { argv: %w(-sstring1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: [] of String, }, { argv: %w(--string string1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: [] of String, }, { argv: %w(--string=string1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: [] of String, }, { argv: %w(-s string1 arg1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: ["arg1"], }, { argv: %w(arg1 -s string1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: ["arg1"], }, { argv: %w(-string), # Unintended case. - expect_opts: create_values({"string" => "tring"}), + expect_opts: create_opts_hash({"string" => "tring"}), expect_args: [] of String, }, { argv: %w(-s=string1), # Unintended case. - expect_opts: create_values({"string" => "=string1"}), + expect_opts: create_opts_hash({"string" => "=string1"}), expect_args: [] of String, }, ].each do |spec_case| @@ -860,6 +858,14 @@ describe "main command with string required only." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(arg1), exception_message: "Required options. \"-s ARG\"", @@ -911,12 +917,6 @@ describe "main command with string required false and default exists." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithStringRequiredFalseAndDefaultExists.run_proc_arguments(spec_case[:argv]) @@ -944,52 +944,52 @@ describe "main command with string required false and default exists." do [ { argv: %w(), - expect_opts: create_values({"string" => "default value"}), + expect_opts: create_opts_hash({"string" => "default value"}), expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values({"string" => "default value"}), + expect_opts: create_opts_hash({"string" => "default value"}), expect_args: ["arg1"], }, { argv: %w(-s string1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: [] of String, }, { argv: %w(-sstring1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: [] of String, }, { argv: %w(--string string1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: [] of String, }, { argv: %w(--string=string1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: [] of String, }, { argv: %w(-s string1 arg1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: ["arg1"], }, { argv: %w(arg1 -s string1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: ["arg1"], }, { argv: %w(-string), # Unintended case. - expect_opts: create_values({"string" => "tring"}), + expect_opts: create_opts_hash({"string" => "tring"}), expect_args: [] of String, }, { argv: %w(-s=string1), # Unintended case. - expect_opts: create_values({"string" => "=string1"}), + expect_opts: create_opts_hash({"string" => "=string1"}), expect_args: [] of String, }, ].each do |spec_case| @@ -1007,6 +1007,14 @@ describe "main command with string required false and default exists." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(-s), exception_message: "Option that requires an argument. \"-s\"", @@ -1054,12 +1062,6 @@ describe "main command with string required false only." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecMainCommandWithStringRequiredFalseOnly.run_proc_arguments(spec_case[:argv]) @@ -1087,52 +1089,52 @@ describe "main command with string required false only." do [ { argv: %w(), - expect_opts: create_values({"string" => nil}), + expect_opts: create_opts_hash({"string" => nil}), expect_args: [] of String, }, { argv: %w(arg1), - expect_opts: create_values({"string" => nil}), + expect_opts: create_opts_hash({"string" => nil}), expect_args: ["arg1"], }, { argv: %w(-s string1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: [] of String, }, { argv: %w(-sstring1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: [] of String, }, { argv: %w(--string string1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: [] of String, }, { argv: %w(--string=string1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: [] of String, }, { argv: %w(-s string1 arg1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: ["arg1"], }, { argv: %w(arg1 -s string1), - expect_opts: create_values({"string" => "string1"}), + expect_opts: create_opts_hash({"string" => "string1"}), expect_args: ["arg1"], }, { argv: %w(-string), # Unintended case. - expect_opts: create_values({"string" => "tring"}), + expect_opts: create_opts_hash({"string" => "tring"}), expect_args: [] of String, }, { argv: %w(-s=string1), # Unintended case. - expect_opts: create_values({"string" => "=string1"}), + expect_opts: create_opts_hash({"string" => "=string1"}), expect_args: [] of String, }, ].each do |spec_case| @@ -1150,6 +1152,14 @@ describe "main command with string required false only." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(-s), exception_message: "Option that requires an argument. \"-s\"", diff --git a/spec/clim/dsl_spec/sub_command_spec.cr b/spec/clim/dsl_spec/sub_command_spec.cr index d38e5e99..f5115da4 100644 --- a/spec/clim/dsl_spec/sub_command_spec.cr +++ b/spec/clim/dsl_spec/sub_command_spec.cr @@ -25,12 +25,6 @@ describe "sub command only." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecSubCommandOnly.run_proc_arguments(spec_case[:argv]) @@ -68,12 +62,6 @@ describe "sub command only." do { argv: %w(sub_command ignore-arg --help), }, - { - argv: %w(sub_command --help -ignore-option), - }, - { - argv: %w(sub_command -ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecSubCommandOnly.run_proc_arguments(spec_case[:argv]) @@ -100,22 +88,22 @@ describe "sub command only." do [ { argv: %w(sub_command), - expect_opts: create_values, + expect_opts: create_opts_hash, expect_args: [] of String, }, { argv: %w(sub_command arg1), - expect_opts: create_values, + expect_opts: create_opts_hash, expect_args: ["arg1"], }, { argv: %w(sub_command arg1 arg2), - expect_opts: create_values, + expect_opts: create_opts_hash, expect_args: ["arg1", "arg2"], }, { argv: %w(sub_command arg1 arg2 arg3), - expect_opts: create_values, + expect_opts: create_opts_hash, expect_args: ["arg1", "arg2", "arg3"], }, ].each do |spec_case| @@ -133,6 +121,22 @@ describe "sub command only." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(sub_command --help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(sub_command -ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(sub_command -m), exception_message: "Undefined option. \"-m\"", @@ -190,12 +194,6 @@ describe "sub command with desc." do { argv: %w(sub_command ignore-arg --help), }, - { - argv: %w(sub_command --help -ignore-option), - }, - { - argv: %w(sub_command -ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecSubCommandWithDesc.run_proc_arguments(spec_case[:argv]) @@ -249,12 +247,6 @@ describe "sub command with usage." do { argv: %w(sub_command ignore-arg --help), }, - { - argv: %w(sub_command --help -ignore-option), - }, - { - argv: %w(sub_command -ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecSubCommandWithUsage.run_proc_arguments(spec_case[:argv]) @@ -317,12 +309,6 @@ describe "sub sub command." do { argv: %w(sub_command sub_sub_command ignore-arg --help), }, - { - argv: %w(sub_command sub_sub_command --help -ignore-option), - }, - { - argv: %w(sub_command sub_sub_command -ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecSubSubCommand.run_proc_arguments(spec_case[:argv]) @@ -356,18 +342,9 @@ describe "sub sub command." do { argv: %w(sub_command ignore-arg --help sub_sub_command), }, - { - argv: %w(sub_command --help -ignore-option sub_sub_command), - }, - { - argv: %w(sub_command -ignore-option --help sub_sub_command), - }, { argv: %w(sub_command ignore-arg sub_sub_command --help), }, - { - argv: %w(sub_command -ignore-option sub_sub_command --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecSubSubCommand.run_proc_arguments(spec_case[:argv]) @@ -398,22 +375,22 @@ describe "sub sub command." do [ { argv: %w(sub_command sub_sub_command), - expect_opts: create_values, + expect_opts: create_opts_hash, expect_args: [] of String, }, { argv: %w(sub_command sub_sub_command arg1), - expect_opts: create_values, + expect_opts: create_opts_hash, expect_args: ["arg1"], }, { argv: %w(sub_command sub_sub_command arg1 arg2), - expect_opts: create_values, + expect_opts: create_opts_hash, expect_args: ["arg1", "arg2"], }, { argv: %w(sub_command sub_sub_command arg1 arg2 arg3), - expect_opts: create_values, + expect_opts: create_opts_hash, expect_args: ["arg1", "arg2", "arg3"], }, ].each do |spec_case| @@ -431,6 +408,42 @@ describe "sub sub command." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(sub_command --help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(sub_command -ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(sub_command sub_sub_command --help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(sub_command sub_sub_command -ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(sub_command --help -ignore-option sub_sub_command), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(sub_command -ignore-option --help sub_sub_command), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(sub_command -ignore-option sub_sub_command --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(sub_command sub_sub_command -m), exception_message: "Undefined option. \"-m\"", @@ -506,12 +519,6 @@ describe "sub command jump over sub sub command." do { argv: %w(jump_over_sub_sub_command ignore-arg --help), }, - { - argv: %w(jump_over_sub_sub_command --help -ignore-option), - }, - { - argv: %w(jump_over_sub_sub_command -ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecSubCommandJumpOverSubSubCommand.run_proc_arguments(spec_case[:argv]) @@ -545,12 +552,6 @@ describe "sub command jump over sub sub command." do { argv: %w(ignore-arg --help), }, - { - argv: %w(--help -ignore-option), - }, - { - argv: %w(-ignore-option --help), - }, ].each do |spec_case| it "#{spec_case[:argv].join(" ")}" do run_proc_opts, run_proc_args = SpecSubCommandJumpOverSubSubCommand.run_proc_arguments(spec_case[:argv]) @@ -582,22 +583,22 @@ describe "sub command jump over sub sub command." do [ { argv: %w(jump_over_sub_sub_command), - expect_opts: create_values, + expect_opts: create_opts_hash, expect_args: [] of String, }, { argv: %w(jump_over_sub_sub_command arg1), - expect_opts: create_values, + expect_opts: create_opts_hash, expect_args: ["arg1"], }, { argv: %w(jump_over_sub_sub_command arg1 arg2), - expect_opts: create_values, + expect_opts: create_opts_hash, expect_args: ["arg1", "arg2"], }, { argv: %w(jump_over_sub_sub_command arg1 arg2 arg3), - expect_opts: create_values, + expect_opts: create_opts_hash, expect_args: ["arg1", "arg2", "arg3"], }, ].each do |spec_case| @@ -615,6 +616,22 @@ describe "sub command jump over sub sub command." do argv: %w(-h), exception_message: "Undefined option. \"-h\"", }, + { + argv: %w(--help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(-ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(jump_over_sub_sub_command --help -ignore-option), + exception_message: "Undefined option. \"-ignore-option\"", + }, + { + argv: %w(jump_over_sub_sub_command -ignore-option --help), + exception_message: "Undefined option. \"-ignore-option\"", + }, { argv: %w(jump_over_sub_sub_command -m), exception_message: "Undefined option. \"-m\"", diff --git a/spec/clim/options_spec.cr b/spec/clim/options_spec.cr index b9fb0821..8d4dc237 100644 --- a/spec/clim/options_spec.cr +++ b/spec/clim/options_spec.cr @@ -32,33 +32,7 @@ describe Clim::Options do end end end - describe Options::Values do - describe "#merge!" do - it "merged hash." do - values = Options::Values.new - values.merge!({"string_key" => "string value"}) - values.merge!({"bool_key" => true}) - values.merge!({"array_key" => ["array", "value"]}) - values.merge!({"merge_string_key" => "merge string value"}) - values.hash.should eq({ - "string_key" => "string value", - "merge_string_key" => "merge string value", - "bool_key" => true, - "array_key" => ["array", "value"], - }) - end - it "raises an Exception when option name is duplicated." do - values = Options::Values.new - values.merge!({"string_key" => "string value"}) - values.merge!({"bool_key" => true}) - values.merge!({"array_key" => ["array", "value"]}) - expect_raises(Exception, "Duplicate option. \"string_key\"") do - values.merge!({"string_key" => "merge string value"}) # duplicated - end - end - end - end - describe "#values" do + describe "#to_h" do it "returns hash when options are set." do opts = Options.new opts.add Option(String | Nil).new("-f", "--foo", "", false, "", "value foo") @@ -66,14 +40,14 @@ describe Clim::Options do opts.add Option(String | Nil).new("-z VALUE", "--zoo=VALUE", "", false, "", "value zoo") opts.add Option(Bool | Nil).new("-v", "", false, false, "", true) opts.add Option(Array(String) | Nil).new("-a", "--array", [] of String, false, "", ["a", "b"]) - expect_values = Options::Values.new - expect_values.merge!({"help" => ""}) - expect_values.merge!({"foo" => "value foo"}) - expect_values.merge!({"bar" => "value bar"}) - expect_values.merge!({"zoo" => "value zoo"}) - expect_values.merge!({"v" => true}) - expect_values.merge!({"array" => ["a", "b"]}) - opts.values.should eq(expect_values.hash) + expect = Clim::ReturnOptsType.new + expect.merge!({"help" => ""}) + expect.merge!({"foo" => "value foo"}) + expect.merge!({"bar" => "value bar"}) + expect.merge!({"zoo" => "value zoo"}) + expect.merge!({"v" => true}) + expect.merge!({"array" => ["a", "b"]}) + opts.to_h.should eq(expect) end end describe "#validate!" do diff --git a/src/clim.cr b/src/clim.cr index 0afb0081..783bb56a 100644 --- a/src/clim.cr +++ b/src/clim.cr @@ -1,7 +1,6 @@ require "./clim/exception" require "./clim/option" require "./clim/options" -require "./clim/input_args" require "./clim/command" require "./clim/dsl" require "./clim/*" diff --git a/src/clim/command.cr b/src/clim/command.cr index 15d61e23..b9a671e9 100644 --- a/src/clim/command.cr +++ b/src/clim/command.cr @@ -20,9 +20,9 @@ class Clim end def initialize_parser - parser.on("--help", "Show this help.") { @run_proc = help_proc; @display_help_flag = true } - parser.invalid_option { |opt_name| raise ClimException.new "Undefined option. \"#{opt_name}\"" } - parser.missing_option { |opt_name| raise ClimException.new "Option that requires an argument. \"#{opt_name}\"" } + parser.on("--help", "Show this help.") { @display_help_flag = true } + parser.invalid_option { |opt_name| raise ClimInvalidOptionException.new "Undefined option. \"#{opt_name}\"" } + parser.missing_option { |opt_name| raise ClimInvalidOptionException.new "Option that requires an argument. \"#{opt_name}\"" } parser.unknown_args { |unknown_args| @args = unknown_args } end @@ -36,11 +36,7 @@ class Clim end def help - if sub_cmds.empty? - base_help - else - base_help + sub_cmds_help - end + sub_cmds.empty? ? base_help : base_help + sub_cmds_help end def display_help? @@ -86,41 +82,38 @@ class Clim end def run(opts, args) - run_proc.call(opts, args) + select_run_proc.call(opts, args) + end + + def select_run_proc + display_help? ? @help_proc : @run_proc end def run_proc_arguments - return opts.values, args + return opts.to_h, args end def add_sub_commands(cmd) - raise ClimException.new "There are duplicate registered commands. [#{cmd.name}]" if duplicate_sub_command_name?(cmd.name) + unless find_sub_cmds_by(cmd.name).empty? + raise ClimException.new "There are duplicate registered commands. [#{cmd.name}]" + end @sub_cmds << cmd end - def duplicate_sub_command_name?(name) - !find_sub_cmds_by(name).empty? - end - def find_sub_cmds_by(name) sub_cmds.select(&.name.==(name)) end def parse(argv) return parse_by_parser(argv) if argv.empty? - return parse_by_parser(argv) unless duplicate_sub_command_name?(argv.first) + return parse_by_parser(argv) if find_sub_cmds_by(argv.first).empty? find_sub_cmds_by(argv.first).first.parse(argv[1..-1]) end def parse_by_parser(argv) - input_args = InputArgs.new(argv) - prepare_parse - parser.parse(input_args.to_be_exec.dup) - # parser.parse(argv.dup) - + parser.parse(argv.dup) opts.validate! unless display_help? - opts.help = help self end @@ -130,6 +123,5 @@ class Clim @args = [] of String @display_help_flag = false end - end end diff --git a/src/clim/dsl.cr b/src/clim/dsl.cr index d50bd39b..97a86ae4 100644 --- a/src/clim/dsl.cr +++ b/src/clim/dsl.cr @@ -2,17 +2,17 @@ class Clim alias ReturnOptsType = Hash(String, String | Bool | Array(String) | Nil) alias RunProc = Proc(ReturnOptsType, Array(String), Nil) - @@main : Command = Command.new("main_command") - @@defining : Command = @@main - @@stack : Array(Command) = [] of Command - @@defined_main_block : Bool = false + @@main : Command = Command.new("main_command") # Main command. + @@defining : Command = @@main # Current defining command. + @@stack : Array(Command) = [] of Command # Command stack for `sub do ... end` scope. + @@defined_main : Bool = false # If the main command is defined, @@defined_main = true. module Dsl def main_command - raise ClimException.new "Main command is already defined." if @@defined_main_block + raise ClimException.new "Main command is already defined." if @@defined_main @@main = Command.new("main_command") @@defining = @@main - @@defined_main_block = true + @@defined_main = true end def command(name) @@ -42,6 +42,7 @@ class Clim end end + # Call the define_opts macro. difine_opts(method_name: "string", type: String | Nil) { |arg| opt.set_string(arg) } difine_opts(method_name: "bool", type: Bool | Nil) { |arg| opt.set_bool(arg) } difine_opts(method_name: "array", type: Array(String) | Nil) { |arg| opt.add_to_array(arg) } @@ -68,8 +69,12 @@ class Clim def start(argv) start_main(argv) - rescue ex - puts ex.message + rescue ex : ClimException + puts "ERROR: #{ex.message}" + rescue ex : ClimInvalidOptionException + puts "ERROR: #{ex.message}" + puts "" + puts "Please see the `--help`." end end end diff --git a/src/clim/exception.cr b/src/clim/exception.cr index fe4c7fc4..cf26737d 100644 --- a/src/clim/exception.cr +++ b/src/clim/exception.cr @@ -1,4 +1,6 @@ class Clim class ClimException < Exception end + class ClimInvalidOptionException < Exception + end end diff --git a/src/clim/input_args.cr b/src/clim/input_args.cr deleted file mode 100644 index 596b6a1d..00000000 --- a/src/clim/input_args.cr +++ /dev/null @@ -1,16 +0,0 @@ -class Clim - class InputArgs - property argv : Array(String) - - def initialize(@argv) - end - - def to_be_exec - select_help_arg.empty? ? argv : select_help_arg - end - - def select_help_arg - argv.select { |arg| arg == "--help" } - end - end -end diff --git a/src/clim/option.cr b/src/clim/option.cr index 37d34bdd..60af93b2 100644 --- a/src/clim/option.cr +++ b/src/clim/option.cr @@ -60,17 +60,15 @@ class Clim if arg.empty? @value = true else - exception_msg = "Bool arguments accept only \"true\" or \"false\". Input: [#{arg}]" - raise ClimException.new exception_msg unless arg === "true" || arg == "false" + unless arg === "true" || arg == "false" + raise ClimInvalidOptionException.new "Bool arguments accept only \"true\" or \"false\". Input: [#{arg}]" + end @value = arg === "true" end end def add_to_array(arg) - iv_value = @value - value_tmp = iv_value.nil? ? [] of String : iv_value - value_tmp << arg - @value = value_tmp + @value = @value.nil? ? [arg] : @value.try &.<<(arg) end def reset diff --git a/src/clim/options.cr b/src/clim/options.cr index 8d630528..dd9b4192 100644 --- a/src/clim/options.cr +++ b/src/clim/options.cr @@ -6,34 +6,31 @@ class Clim property help : String = "" def add(opt) - raise ClimException.new "Empty short option." if opt.short_name.empty? - raise ClimException.new "Duplicate option. \"-#{opt.short_name}\"" if opts.map(&.short_name).includes?(opt.short_name) - raise ClimException.new "Duplicate option. \"--#{opt.long_name}\"" if opts.map(&.long_name).reject(&.empty?).includes?(opt.long_name) + opt_validate!(opt) opts << opt end - class Values - property help : String = "" - property hash : ReturnOptsType = ReturnOptsType.new + def opt_validate!(opt) + raise ClimException.new "Empty short option." if opt.short_name.empty? + raise ClimException.new "Duplicate option. \"-#{opt.short_name}\"" if duplicate_short_name?(opt.short_name) + raise ClimException.new "Duplicate option. \"--#{opt.long_name}\"" if duplicate_long_name?(opt.long_name) + end + + def duplicate_short_name?(name) + opts.map(&.short_name).includes?(name) + end - def merge!(other : ReturnOptsType) - other.each do |k, v| - if hash.has_key?(k) - raise ClimException.new "Duplicate option. \"#{k}\"" - else - hash.merge!(other) - end - end - end + def duplicate_long_name?(name) + opts.map(&.long_name).reject(&.empty?).includes?(name) end - def values : ReturnOptsType - values = Values.new - values.merge!({"help" => help}) + def to_h : ReturnOptsType + hash = ReturnOptsType.new + hash.merge!({"help" => help}) opts.each do |opt| - values.merge!(opt.to_h) + hash.merge!(opt.to_h) end - values.hash + hash end def reset @@ -41,10 +38,10 @@ class Clim end def validate! - raise "Required options. \"#{no_required_option_names.join("\", \"")}\"" unless no_required_option_names.empty? + raise "Required options. \"#{invalid_required_names.join("\", \"")}\"" unless invalid_required_names.empty? end - def no_required_option_names + def invalid_required_names opts.map do |opt| opt.required_set? ? opt.short : nil end.compact