From d877e5f4df71a2b23f9567b8fb7c2c27948de1a0 Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Tue, 1 Aug 2017 19:00:05 +0900 Subject: [PATCH 01/12] ... --- src/clim/command.cr | 10 +++++----- src/clim/dsl.cr | 5 ++++- src/clim/exception.cr | 2 ++ 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/clim/command.cr b/src/clim/command.cr index 15d61e23..4f96c0c5 100644 --- a/src/clim/command.cr +++ b/src/clim/command.cr @@ -21,8 +21,8 @@ class Clim 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.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 @@ -113,11 +113,11 @@ class Clim end def parse_by_parser(argv) - input_args = InputArgs.new(argv) + # input_args = InputArgs.new(argv) prepare_parse - parser.parse(input_args.to_be_exec.dup) - # parser.parse(argv.dup) + # parser.parse(input_args.to_be_exec.dup) + parser.parse(argv.dup) opts.validate! unless display_help? diff --git a/src/clim/dsl.cr b/src/clim/dsl.cr index d50bd39b..38e7cafe 100644 --- a/src/clim/dsl.cr +++ b/src/clim/dsl.cr @@ -68,8 +68,11 @@ class Clim def start(argv) start_main(argv) - rescue ex + rescue ex : ClimException puts ex.message + rescue ex : ClimInvalidOptionException + puts ex.message + @@main.help_proc.call 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 From 9f10a7fa1a775003171be74fefa519fdb06a8b7d Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Tue, 1 Aug 2017 21:33:14 +0900 Subject: [PATCH 02/12] fix spec --- spec/clim/dsl_spec/array_spec.cr | 118 +++++++++------- spec/clim/dsl_spec/bool_spec.cr | 178 ++++++++++++------------ spec/clim/dsl_spec/main_command_spec.cr | 42 +++--- spec/clim/dsl_spec/string_spec.cr | 118 +++++++++------- spec/clim/dsl_spec/sub_command_spec.cr | 119 +++++++++------- 5 files changed, 308 insertions(+), 267 deletions(-) diff --git a/spec/clim/dsl_spec/array_spec.cr b/spec/clim/dsl_spec/array_spec.cr index 74a41aea..7dab5a74 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]) @@ -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]) @@ -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]) @@ -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]) @@ -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]) @@ -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]) @@ -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]) @@ -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]) @@ -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..a22cefd2 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]) @@ -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]) @@ -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]) @@ -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]) @@ -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]) @@ -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]) @@ -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]) @@ -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]) @@ -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]) @@ -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]) @@ -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]) @@ -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]) @@ -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]) @@ -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]) @@ -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..4989daf5 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]) @@ -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]) @@ -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]) @@ -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\"", diff --git a/spec/clim/dsl_spec/string_spec.cr b/spec/clim/dsl_spec/string_spec.cr index 817d79eb..9cced9b4 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]) @@ -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]) @@ -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]) @@ -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]) @@ -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]) @@ -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]) @@ -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]) @@ -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]) @@ -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..2f08889f 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]) @@ -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]) @@ -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]) @@ -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\"", From 1d96fc55fb3c6aced98daf9b6ef205a490c52063 Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Tue, 1 Aug 2017 21:34:20 +0900 Subject: [PATCH 03/12] delete input_args --- src/clim.cr | 1 - src/clim/command.cr | 4 ---- src/clim/input_args.cr | 16 ---------------- 3 files changed, 21 deletions(-) delete mode 100644 src/clim/input_args.cr 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 4f96c0c5..0b630fdc 100644 --- a/src/clim/command.cr +++ b/src/clim/command.cr @@ -113,10 +113,7 @@ class Clim 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) opts.validate! unless display_help? @@ -130,6 +127,5 @@ class Clim @args = [] of String @display_help_flag = false end - 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 From 49c2f4c31772f380ab13607f1d7ee8abe20d01d4 Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Wed, 2 Aug 2017 02:38:19 +0900 Subject: [PATCH 04/12] add select_run_proc and delete duplicate_sub_command_name? --- spec/clim/command_spec.cr | 50 --------------------------------------- src/clim/command.cr | 16 ++++++------- 2 files changed, 8 insertions(+), 58 deletions(-) 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/src/clim/command.cr b/src/clim/command.cr index 0b630fdc..c79e4d32 100644 --- a/src/clim/command.cr +++ b/src/clim/command.cr @@ -20,7 +20,7 @@ class Clim end def initialize_parser - parser.on("--help", "Show this help.") { @run_proc = help_proc; @display_help_flag = true } + 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 } @@ -86,7 +86,11 @@ 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 @@ -94,21 +98,17 @@ class Clim end def add_sub_commands(cmd) - raise ClimException.new "There are duplicate registered commands. [#{cmd.name}]" if duplicate_sub_command_name?(cmd.name) + raise ClimException.new "There are duplicate registered commands. [#{cmd.name}]" unless find_sub_cmds_by(cmd.name).empty? @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 From 4d66bfefd762ecdddeac99c42ae50271b943ed28 Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Wed, 2 Aug 2017 02:53:06 +0900 Subject: [PATCH 05/12] refactoring --- src/clim/options.cr | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/clim/options.cr b/src/clim/options.cr index 8d630528..92857d23 100644 --- a/src/clim/options.cr +++ b/src/clim/options.cr @@ -6,10 +6,14 @@ class Clim property help : String = "" def add(opt) + opt_validate!(opt) + opts << opt + end + + def opt_validate!(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) - opts << opt end class Values @@ -41,10 +45,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 From c31c9f79260a2a6a465bb05527f39a2f28da6c58 Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Wed, 2 Aug 2017 03:04:00 +0900 Subject: [PATCH 06/12] delete Options::Values --- spec/clim/dsl_spec.cr | 6 +++--- spec/clim/options_spec.cr | 30 ++---------------------------- src/clim/options.cr | 19 ++----------------- 3 files changed, 7 insertions(+), 48 deletions(-) diff --git a/spec/clim/dsl_spec.cr b/spec/clim/dsl_spec.cr index 822de4c4..aad60d53 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 + values = Clim::ReturnOptsType.new + values.merge!(other) + values end diff --git a/spec/clim/options_spec.cr b/spec/clim/options_spec.cr index b9fb0821..ecf49595 100644 --- a/spec/clim/options_spec.cr +++ b/spec/clim/options_spec.cr @@ -32,32 +32,6 @@ 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 it "returns hash when options are set." do opts = Options.new @@ -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 = Clim::ReturnOptsType.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) + opts.values.should eq(expect_values) end end describe "#validate!" do diff --git a/src/clim/options.cr b/src/clim/options.cr index 92857d23..5e486c99 100644 --- a/src/clim/options.cr +++ b/src/clim/options.cr @@ -16,28 +16,13 @@ class Clim raise ClimException.new "Duplicate option. \"--#{opt.long_name}\"" if opts.map(&.long_name).reject(&.empty?).includes?(opt.long_name) end - class Values - property help : String = "" - property hash : ReturnOptsType = ReturnOptsType.new - - 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 - end - def values : ReturnOptsType - values = Values.new + values = ReturnOptsType.new values.merge!({"help" => help}) opts.each do |opt| values.merge!(opt.to_h) end - values.hash + values end def reset From 5ceb6f22cf42f4020968d519a25aac7617f3f1f2 Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Wed, 2 Aug 2017 03:16:50 +0900 Subject: [PATCH 07/12] refactoring --- src/clim/command.cr | 2 +- src/clim/dsl.cr | 6 +++--- src/clim/option.cr | 5 +---- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/clim/command.cr b/src/clim/command.cr index c79e4d32..a6aa0adf 100644 --- a/src/clim/command.cr +++ b/src/clim/command.cr @@ -117,8 +117,8 @@ class Clim parser.parse(argv.dup) opts.validate! unless display_help? - opts.help = help + self end diff --git a/src/clim/dsl.cr b/src/clim/dsl.cr index 38e7cafe..1515ed21 100644 --- a/src/clim/dsl.cr +++ b/src/clim/dsl.cr @@ -5,14 +5,14 @@ class Clim @@main : Command = Command.new("main_command") @@defining : Command = @@main @@stack : Array(Command) = [] of Command - @@defined_main_block : Bool = false + @@defined_main : Bool = false 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) diff --git a/src/clim/option.cr b/src/clim/option.cr index 37d34bdd..5741cb78 100644 --- a/src/clim/option.cr +++ b/src/clim/option.cr @@ -67,10 +67,7 @@ class Clim 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 From 3dfa7ab0664b38758a75ef955a47acbb34044baf Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Wed, 2 Aug 2017 08:03:11 +0900 Subject: [PATCH 08/12] ... --- README.md | 2 ++ src/clim/command.cr | 6 +++--- src/clim/option.cr | 5 +++-- src/clim/options.cr | 12 ++++++++++-- 4 files changed, 18 insertions(+), 7 deletions(-) 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/src/clim/command.cr b/src/clim/command.cr index a6aa0adf..61460e37 100644 --- a/src/clim/command.cr +++ b/src/clim/command.cr @@ -98,7 +98,9 @@ class Clim end def add_sub_commands(cmd) - raise ClimException.new "There are duplicate registered commands. [#{cmd.name}]" unless find_sub_cmds_by(cmd.name).empty? + unless find_sub_cmds_by(cmd.name).empty? + raise ClimException.new "There are duplicate registered commands. [#{cmd.name}]" + end @sub_cmds << cmd end @@ -115,10 +117,8 @@ class Clim def parse_by_parser(argv) prepare_parse parser.parse(argv.dup) - opts.validate! unless display_help? opts.help = help - self end diff --git a/src/clim/option.cr b/src/clim/option.cr index 5741cb78..9b277bed 100644 --- a/src/clim/option.cr +++ b/src/clim/option.cr @@ -60,8 +60,9 @@ 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 ClimException.new "Bool arguments accept only \"true\" or \"false\". Input: [#{arg}]" + end @value = arg === "true" end end diff --git a/src/clim/options.cr b/src/clim/options.cr index 5e486c99..0b0ee535 100644 --- a/src/clim/options.cr +++ b/src/clim/options.cr @@ -12,8 +12,16 @@ class Clim def opt_validate!(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) + 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 duplicate_long_name?(name) + opts.map(&.long_name).reject(&.empty?).includes?(name) end def values : ReturnOptsType From 2c92c6eadd336abfcc0ace9dcee3d79810fb5892 Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Wed, 2 Aug 2017 08:05:47 +0900 Subject: [PATCH 09/12] ... --- src/clim/command.cr | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/clim/command.cr b/src/clim/command.cr index 61460e37..8b718772 100644 --- a/src/clim/command.cr +++ b/src/clim/command.cr @@ -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? From 5b7be3af43137e28108df4ea290da8f4dd0b3672 Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Wed, 2 Aug 2017 08:14:10 +0900 Subject: [PATCH 10/12] add comment --- src/clim/dsl.cr | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/clim/dsl.cr b/src/clim/dsl.cr index 1515ed21..122cbd08 100644 --- a/src/clim/dsl.cr +++ b/src/clim/dsl.cr @@ -2,10 +2,10 @@ 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 : 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 @@ -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) } From 2b1ad3c813d9817b8215e7ce3dbbc88c385e4806 Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Wed, 2 Aug 2017 08:19:45 +0900 Subject: [PATCH 11/12] opts.values -> opts.to_h --- spec/clim/dsl_spec.cr | 8 +- spec/clim/dsl_spec/array_spec.cr | 144 +++++++-------- spec/clim/dsl_spec/bool_spec.cr | 236 ++++++++++++------------ spec/clim/dsl_spec/main_command_spec.cr | 26 +-- spec/clim/dsl_spec/string_spec.cr | 144 +++++++-------- spec/clim/dsl_spec/sub_command_spec.cr | 24 +-- spec/clim/options_spec.cr | 18 +- src/clim/command.cr | 2 +- src/clim/options.cr | 10 +- 9 files changed, 306 insertions(+), 306 deletions(-) diff --git a/spec/clim/dsl_spec.cr b/spec/clim/dsl_spec.cr index aad60d53..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 = Clim::ReturnOptsType.new - values.merge!(other) - values +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 7dab5a74..c3ea90fc 100644 --- a/spec/clim/dsl_spec/array_spec.cr +++ b/spec/clim/dsl_spec/array_spec.cr @@ -49,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| @@ -194,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| @@ -337,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| @@ -516,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| @@ -661,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| @@ -806,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| @@ -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| @@ -1094,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| diff --git a/spec/clim/dsl_spec/bool_spec.cr b/spec/clim/dsl_spec/bool_spec.cr index a22cefd2..0afe76de 100644 --- a/spec/clim/dsl_spec/bool_spec.cr +++ b/spec/clim/dsl_spec/bool_spec.cr @@ -48,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| @@ -175,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| @@ -295,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| @@ -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| @@ -552,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| @@ -737,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| @@ -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| @@ -951,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| @@ -1132,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| @@ -1249,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| @@ -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| @@ -1528,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| @@ -1709,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| @@ -1809,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| diff --git a/spec/clim/dsl_spec/main_command_spec.cr b/spec/clim/dsl_spec/main_command_spec.cr index 4989daf5..b4594606 100644 --- a/spec/clim/dsl_spec/main_command_spec.cr +++ b/spec/clim/dsl_spec/main_command_spec.cr @@ -44,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| @@ -160,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| @@ -277,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| @@ -645,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 9cced9b4..91d873a5 100644 --- a/spec/clim/dsl_spec/string_spec.cr +++ b/spec/clim/dsl_spec/string_spec.cr @@ -48,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| @@ -193,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| @@ -336,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| @@ -511,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| @@ -656,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| @@ -801,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| @@ -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| @@ -1089,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| diff --git a/spec/clim/dsl_spec/sub_command_spec.cr b/spec/clim/dsl_spec/sub_command_spec.cr index 2f08889f..f5115da4 100644 --- a/spec/clim/dsl_spec/sub_command_spec.cr +++ b/spec/clim/dsl_spec/sub_command_spec.cr @@ -88,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| @@ -375,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| @@ -583,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| diff --git a/spec/clim/options_spec.cr b/spec/clim/options_spec.cr index ecf49595..8d4dc237 100644 --- a/spec/clim/options_spec.cr +++ b/spec/clim/options_spec.cr @@ -32,7 +32,7 @@ describe Clim::Options do 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") @@ -40,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 = Clim::ReturnOptsType.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) + 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/command.cr b/src/clim/command.cr index 8b718772..b9a671e9 100644 --- a/src/clim/command.cr +++ b/src/clim/command.cr @@ -90,7 +90,7 @@ class Clim end def run_proc_arguments - return opts.values, args + return opts.to_h, args end def add_sub_commands(cmd) diff --git a/src/clim/options.cr b/src/clim/options.cr index 0b0ee535..dd9b4192 100644 --- a/src/clim/options.cr +++ b/src/clim/options.cr @@ -24,13 +24,13 @@ class Clim opts.map(&.long_name).reject(&.empty?).includes?(name) end - def values : ReturnOptsType - values = ReturnOptsType.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 end def reset From 600352435baf953bddb30458ff483560377ed809 Mon Sep 17 00:00:00 2001 From: at-grandpa Date: Wed, 2 Aug 2017 08:46:57 +0900 Subject: [PATCH 12/12] fix Exception message --- src/clim/dsl.cr | 7 ++++--- src/clim/option.cr | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/clim/dsl.cr b/src/clim/dsl.cr index 122cbd08..97a86ae4 100644 --- a/src/clim/dsl.cr +++ b/src/clim/dsl.cr @@ -70,10 +70,11 @@ class Clim def start(argv) start_main(argv) rescue ex : ClimException - puts ex.message + puts "ERROR: #{ex.message}" rescue ex : ClimInvalidOptionException - puts ex.message - @@main.help_proc.call + puts "ERROR: #{ex.message}" + puts "" + puts "Please see the `--help`." end end end diff --git a/src/clim/option.cr b/src/clim/option.cr index 9b277bed..60af93b2 100644 --- a/src/clim/option.cr +++ b/src/clim/option.cr @@ -61,7 +61,7 @@ class Clim @value = true else unless arg === "true" || arg == "false" - raise ClimException.new "Bool arguments accept only \"true\" or \"false\". Input: [#{arg}]" + raise ClimInvalidOptionException.new "Bool arguments accept only \"true\" or \"false\". Input: [#{arg}]" end @value = arg === "true" end