diff --git a/README.md b/README.md index b0751cfd..50c777da 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ Add this to your application's `shard.yml`: dependencies: clim: github: at-grandpa/clim - version: 0.2.2 + version: 0.3.0 ``` ## Minimum sample @@ -363,18 +363,45 @@ class MyCli < Clim end ``` -If default is not specified, it is nilable. +The type of the option is determined by the `default` and `required` patterns. -```crystal -class MyCli < Clim - main_command do - option "-g WORDS", "--greeting=WORDS", type: String, desc: "Words of greetings." - run do |options, arguments| - puts typeof(options.greeting) # => (String | Nil) - end - end -end -``` +*Number* + +For example `Int8`. + + `default` | `required` | Type +---------|----------|--------- + exist | `true` | `Int8` (default: Your specified value.) | + exist | `false` | `Int8` (default: Your specified value.) | + not exist | `true` | `Int8` | + not exist | `false` | `Int8 \| Nil` | + +*String* + + `default` | `required` | Type +---------|----------|--------- + exist | `true` | `String` (default: Your specified value.) | + exist | `false` | `String` (default: Your specified value.) | + not exist | `true` | `String` | + not exist | `false` | `String \| Nil` | + +*Bool* + + `default` | `required` | Type +---------|----------|--------- + exist | `true` | `Bool` (default: Your specified value.) | + exist | `false` | `Bool` (default: Your specified value.) | + not exist | `true` | `Bool` | + not exist | `false` | `Bool` (default: `false`) | + +*Array* + + `default` | `required` | Type +---------|----------|--------- + exist | `true` | `Array(T)` (default: Your specified value.) | + exist | `false` | `Array(T)` (default: Your specified value.) | + not exist | `true` | `Array(T)` | + not exist | `false` | `Array(T)` (default: `[] of T`) | For Bool, you do not need to specify arguments for short or long. @@ -432,4 +459,4 @@ $ make spec ## Contributors -- [at-grandpa](https://github.com/at-grandpa) at-grandpa - creator, maintainer +- [at-grandpa](https://github.com/at-grandpa) - creator, maintainer diff --git a/shard.yml b/shard.yml index 98698aa3..16c68d61 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: clim -version: 0.2.2 +version: 0.3.0 authors: - at-grandpa <@at_grandpa> diff --git a/spec/clim/dsl_spec.cr b/spec/clim/dsl_spec.cr index efd05202..4dcc7d68 100644 --- a/spec/clim/dsl_spec.cr +++ b/spec/clim/dsl_spec.cr @@ -3,7 +3,7 @@ require "./../spec_helper" macro assert_opts_and_args(spec_case) opts.help.should eq {{spec_case["expect_help"]}} {% if spec_case.keys.includes?("expect_opts".id) %} - opts.{{spec_case["expect_opts"]["method"].id}}.is_a?({{spec_case["expect_opts"]["type"]}}).should be_true + typeof(opts.{{spec_case["expect_opts"]["method"].id}}).should eq {{spec_case["expect_opts"]["type"]}} opts.{{spec_case["expect_opts"]["method"].id}}.should eq {{spec_case["expect_opts"]["expect_value"]}} {% end %} args.should eq {{spec_case["expect_args"]}} diff --git a/spec/clim/dsl_spec/array_spec.cr b/spec/clim/dsl_spec/array_spec.cr index 1f168a73..21529d44 100644 --- a/spec/clim/dsl_spec/array_spec.cr +++ b/spec/clim/dsl_spec/array_spec.cr @@ -30,9 +30,9 @@ spec( argv: [] of String, expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", - "expect_value" => nil, + "expect_value" => [] of String, }, expect_args: [] of String, }, @@ -40,9 +40,9 @@ spec( argv: ["arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", - "expect_value" => nil, + "expect_value" => [] of String, }, expect_args: ["arg1"], }, @@ -50,7 +50,7 @@ spec( argv: ["-a", "array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -60,7 +60,7 @@ spec( argv: ["-a", "array1", "arg1", "-a", "array2"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1", "array2"], }, @@ -70,7 +70,7 @@ spec( argv: ["-aarray1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -80,7 +80,7 @@ spec( argv: ["--array", "array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -90,7 +90,7 @@ spec( argv: ["--array=array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -100,7 +100,7 @@ spec( argv: ["-a", "array1", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -110,7 +110,7 @@ spec( argv: ["arg1", "-a", "array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -120,7 +120,7 @@ spec( argv: ["-array"], # Unintended case. expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["rray"], }, @@ -130,7 +130,7 @@ spec( argv: ["-a=array1"], # Unintended case. expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["=array1"], }, @@ -210,9 +210,9 @@ spec( argv: [] of String, expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "a", - "expect_value" => nil, + "expect_value" => [] of String, }, expect_args: [] of String, }, @@ -220,9 +220,9 @@ spec( argv: ["arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "a", - "expect_value" => nil, + "expect_value" => [] of String, }, expect_args: ["arg1"], }, @@ -230,7 +230,7 @@ spec( argv: ["-a", "array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "a", "expect_value" => ["array1"], }, @@ -240,7 +240,7 @@ spec( argv: ["-aarray1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "a", "expect_value" => ["array1"], }, @@ -250,7 +250,7 @@ spec( argv: ["-a", "array1", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "a", "expect_value" => ["array1"], }, @@ -260,7 +260,7 @@ spec( argv: ["arg1", "-a", "array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "a", "expect_value" => ["array1"], }, @@ -270,7 +270,7 @@ spec( argv: ["-array"], # Unintended case. expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "a", "expect_value" => ["rray"], }, @@ -280,7 +280,7 @@ spec( argv: ["-a=array1"], # Unintended case. expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "a", "expect_value" => ["=array1"], }, @@ -368,9 +368,9 @@ spec( argv: [] of String, expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", - "expect_value" => nil, + "expect_value" => [] of String, }, expect_args: [] of String, }, @@ -378,9 +378,9 @@ spec( argv: ["arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", - "expect_value" => nil, + "expect_value" => [] of String, }, expect_args: ["arg1"], }, @@ -388,7 +388,7 @@ spec( argv: ["--array", "array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -398,7 +398,7 @@ spec( argv: ["--array=array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -408,7 +408,7 @@ spec( argv: ["--array", "array1", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -418,7 +418,7 @@ spec( argv: ["arg1", "--array", "array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -547,7 +547,7 @@ spec( argv: [] of String, expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["default value"], }, @@ -557,7 +557,7 @@ spec( argv: ["arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["default value"], }, @@ -567,7 +567,7 @@ spec( argv: ["-a", "array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -577,7 +577,7 @@ spec( argv: ["-aarray1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -587,7 +587,7 @@ spec( argv: ["--array", "array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -597,7 +597,7 @@ spec( argv: ["--array=array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -607,7 +607,7 @@ spec( argv: ["-a", "array1", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -617,7 +617,7 @@ spec( argv: ["arg1", "-a", "array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -627,7 +627,7 @@ spec( argv: ["-array"], # Unintended case. expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["rray"], }, @@ -637,7 +637,7 @@ spec( argv: ["-a=array1"], # Unintended case. expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["=array1"], }, @@ -713,31 +713,11 @@ spec( ], spec_desc: "main command with Array(String) option,", spec_cases: [ - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Array(String)?, - "method" => "array", - "expect_value" => ["default value"], - }, - expect_args: [] of String, - }, - { - argv: ["arg1"], - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Array(String)?, - "method" => "array", - "expect_value" => ["default value"], - }, - expect_args: ["arg1"], - }, { argv: ["-a", "array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -747,7 +727,7 @@ spec( argv: ["-aarray1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -757,7 +737,7 @@ spec( argv: ["--array", "array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -767,7 +747,7 @@ spec( argv: ["--array=array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -777,7 +757,7 @@ spec( argv: ["-a", "array1", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -787,7 +767,7 @@ spec( argv: ["arg1", "-a", "array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -797,7 +777,7 @@ spec( argv: ["-array"], # Unintended case. expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["rray"], }, @@ -807,12 +787,20 @@ spec( argv: ["-a=array1"], # Unintended case. expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["=array1"], }, expect_args: [] of String, }, + { + argv: [] of String, + exception_message: "Required options. \"-a ARG\"", + }, + { + argv: ["arg1"], + exception_message: "Required options. \"-a ARG\"", + }, { argv: ["-h"], exception_message: "Undefined option. \"-h\"", @@ -887,7 +875,7 @@ spec( argv: ["-a", "array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -897,7 +885,7 @@ spec( argv: ["-aarray1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -907,7 +895,7 @@ spec( argv: ["--array", "array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -917,7 +905,7 @@ spec( argv: ["--array=array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -927,7 +915,7 @@ spec( argv: ["-a", "array1", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -937,7 +925,7 @@ spec( argv: ["arg1", "-a", "array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -947,7 +935,7 @@ spec( argv: ["-array"], # Unintended case. expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["rray"], }, @@ -957,7 +945,7 @@ spec( argv: ["-a=array1"], # Unintended case. expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["=array1"], }, @@ -967,6 +955,10 @@ spec( argv: [] of String, exception_message: "Required options. \"-a ARG\"", }, + { + argv: ["arg1"], + exception_message: "Required options. \"-a ARG\"", + }, { argv: ["-h"], exception_message: "Undefined option. \"-h\"", @@ -1045,7 +1037,7 @@ spec( argv: [] of String, expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["default value"], }, @@ -1055,7 +1047,7 @@ spec( argv: ["arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["default value"], }, @@ -1065,7 +1057,7 @@ spec( argv: ["-a", "array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -1075,7 +1067,7 @@ spec( argv: ["-aarray1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -1085,7 +1077,7 @@ spec( argv: ["--array", "array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -1095,7 +1087,7 @@ spec( argv: ["--array=array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -1105,7 +1097,7 @@ spec( argv: ["-a", "array1", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -1115,7 +1107,7 @@ spec( argv: ["arg1", "-a", "array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -1125,7 +1117,7 @@ spec( argv: ["-array"], # Unintended case. expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["rray"], }, @@ -1135,7 +1127,7 @@ spec( argv: ["-a=array1"], # Unintended case. expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["=array1"], }, @@ -1215,9 +1207,9 @@ spec( argv: [] of String, expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", - "expect_value" => nil, + "expect_value" => [] of String, }, expect_args: [] of String, }, @@ -1225,9 +1217,9 @@ spec( argv: ["arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", - "expect_value" => nil, + "expect_value" => [] of String, }, expect_args: ["arg1"], }, @@ -1235,7 +1227,7 @@ spec( argv: ["-a", "array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -1245,7 +1237,7 @@ spec( argv: ["-aarray1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -1255,7 +1247,7 @@ spec( argv: ["--array", "array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -1265,7 +1257,7 @@ spec( argv: ["--array=array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -1275,7 +1267,7 @@ spec( argv: ["-a", "array1", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -1285,7 +1277,7 @@ spec( argv: ["arg1", "-a", "array1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["array1"], }, @@ -1295,7 +1287,7 @@ spec( argv: ["-array"], # Unintended case. expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["rray"], }, @@ -1305,7 +1297,7 @@ spec( argv: ["-a=array1"], # Unintended case. expect_help: {{main_help_message}}, expect_opts: { - "type" => Array(String)?, + "type" => Array(String), "method" => "array", "expect_value" => ["=array1"], }, diff --git a/spec/clim/dsl_spec/bool_spec.cr b/spec/clim/dsl_spec/bool_spec.cr index ab8c72e3..eae2dfbd 100644 --- a/spec/clim/dsl_spec/bool_spec.cr +++ b/spec/clim/dsl_spec/bool_spec.cr @@ -12,7 +12,7 @@ require "../dsl_spec" Options: - -b, --bool Option description. [type:Bool] [default:false] + -b, --bool Option description. [type:Bool] --help Show this help. @@ -30,7 +30,7 @@ spec( argv: [] of String, expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -40,7 +40,7 @@ spec( argv: ["arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -50,7 +50,7 @@ spec( argv: ["-b"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -60,7 +60,7 @@ spec( argv: ["--bool"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -70,7 +70,7 @@ spec( argv: ["-b", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -80,7 +80,7 @@ spec( argv: ["--bool", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -90,7 +90,7 @@ spec( argv: ["arg1", "-b"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -100,7 +100,7 @@ spec( argv: ["arg1", "--bool"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -154,7 +154,7 @@ spec( Options: - -b Option description. [type:Bool] [default:false] + -b Option description. [type:Bool] --help Show this help. @@ -172,7 +172,7 @@ spec( argv: [] of String, expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "b", "expect_value" => false, }, @@ -182,7 +182,7 @@ spec( argv: ["arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "b", "expect_value" => false, }, @@ -192,7 +192,7 @@ spec( argv: ["-b"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "b", "expect_value" => true, }, @@ -202,7 +202,7 @@ spec( argv: ["-b", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "b", "expect_value" => true, }, @@ -212,7 +212,7 @@ spec( argv: ["arg1", "-b"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "b", "expect_value" => true, }, @@ -274,7 +274,7 @@ spec( Options: - --bool Option description. [type:Bool] [default:false] + --bool Option description. [type:Bool] --help Show this help. @@ -292,7 +292,7 @@ spec( argv: [] of String, expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -302,7 +302,7 @@ spec( argv: ["arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -312,7 +312,7 @@ spec( argv: ["--bool"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -322,7 +322,7 @@ spec( argv: ["--bool", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -332,7 +332,7 @@ spec( argv: ["arg1", "--bool"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -390,7 +390,7 @@ spec( Options: - -b ARG, --bool=ARG Option description. [type:Bool] [default:false] + -b ARG, --bool=ARG Option description. [type:Bool] --help Show this help. @@ -408,7 +408,7 @@ spec( argv: [] of String, expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -418,7 +418,7 @@ spec( argv: ["arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -428,7 +428,7 @@ spec( argv: ["-b", "true"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -438,7 +438,7 @@ spec( argv: ["-b", "false"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -448,7 +448,7 @@ spec( argv: ["--bool", "true"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -458,7 +458,7 @@ spec( argv: ["--bool", "false"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -526,7 +526,7 @@ spec( {% begin %} {% - main_help_message = <<-HELP_MESSAGE + main_help_message = <<-HELP_MESSAGE Command Line Interface Tool. @@ -536,7 +536,7 @@ spec( Options: - -b ARG Option description. [type:Bool] [default:false] + -b ARG Option description. [type:Bool] --help Show this help. @@ -554,7 +554,7 @@ spec( argv: [] of String, expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "b", "expect_value" => false, }, @@ -564,7 +564,7 @@ spec( argv: ["arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "b", "expect_value" => false, }, @@ -574,7 +574,7 @@ spec( argv: ["-b", "true"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "b", "expect_value" => true, }, @@ -584,7 +584,7 @@ spec( argv: ["-b", "false"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "b", "expect_value" => false, }, @@ -670,7 +670,7 @@ spec( Options: - --bool=ARG Option description. [type:Bool] [default:false] + --bool=ARG Option description. [type:Bool] --help Show this help. @@ -688,7 +688,7 @@ spec( argv: [] of String, expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -698,7 +698,7 @@ spec( argv: ["arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -708,7 +708,7 @@ spec( argv: ["--bool", "true"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -718,7 +718,7 @@ spec( argv: ["--bool", "false"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -728,7 +728,7 @@ spec( argv: ["--bool=true"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -738,7 +738,7 @@ spec( argv: ["--bool=false"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -816,7 +816,7 @@ spec( Options: - -b, --bool Bool option description. [type:Bool] [default:false] + -b, --bool Bool option description. [type:Bool] --help Show this help. @@ -876,7 +876,7 @@ spec( argv: [] of String, expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -886,7 +886,7 @@ spec( argv: ["arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -896,7 +896,7 @@ spec( argv: ["-b"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -906,7 +906,7 @@ spec( argv: ["--bool"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -916,7 +916,7 @@ spec( argv: ["-b", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -926,7 +926,7 @@ spec( argv: ["arg1", "-b"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -998,7 +998,7 @@ spec( argv: [] of String, expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -1008,7 +1008,7 @@ spec( argv: ["arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -1018,7 +1018,7 @@ spec( argv: ["-b"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -1028,7 +1028,7 @@ spec( argv: ["--bool"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -1038,7 +1038,7 @@ spec( argv: ["-b", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -1048,7 +1048,7 @@ spec( argv: ["arg1", "-b"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -1058,7 +1058,7 @@ spec( argv: ["--bool", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -1068,7 +1068,7 @@ spec( argv: ["arg1", "--bool"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -1120,7 +1120,7 @@ spec( argv: [] of String, expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -1130,7 +1130,7 @@ spec( argv: ["arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -1140,7 +1140,7 @@ spec( argv: ["-b", "true"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -1150,7 +1150,7 @@ spec( argv: ["-b", "true", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -1160,7 +1160,7 @@ spec( argv: ["arg1", "-b", "true"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -1170,7 +1170,7 @@ spec( argv: ["-b", "false"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -1180,7 +1180,7 @@ spec( argv: ["-b", "false", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -1190,7 +1190,7 @@ spec( argv: ["arg1", "-b", "false"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -1200,7 +1200,7 @@ spec( argv: ["--bool", "true"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -1210,7 +1210,7 @@ spec( argv: ["--bool", "true", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -1220,7 +1220,7 @@ spec( argv: ["arg1", "--bool", "true"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -1230,7 +1230,7 @@ spec( argv: ["--bool", "false"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -1240,7 +1240,7 @@ spec( argv: ["--bool", "false", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -1250,7 +1250,7 @@ spec( argv: ["arg1", "--bool", "false"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -1328,12 +1328,12 @@ spec( Options: - -b, --bool Bool option description. [type:Bool] [default:false] + -b, --bool Bool option description. [type:Bool] --help Show this help. HELP_MESSAGE - %} +%} spec( spec_class_name: MainCommandWithBoolRequiredFalseOnly, @@ -1346,7 +1346,7 @@ spec( argv: [] of String, expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -1356,7 +1356,7 @@ spec( argv: ["arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -1366,7 +1366,7 @@ spec( argv: ["-b"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -1376,7 +1376,7 @@ spec( argv: ["--bool"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -1386,7 +1386,7 @@ spec( argv: ["-b", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -1396,7 +1396,7 @@ spec( argv: ["arg1", "-b"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -1406,7 +1406,7 @@ spec( argv: ["--bool", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -1416,7 +1416,7 @@ spec( argv: ["arg1", "--bool"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -1450,7 +1450,7 @@ spec( Options: - -b ARG, --bool=ARG Bool option description. [type:Bool] [default:false] + -b ARG, --bool=ARG Bool option description. [type:Bool] --help Show this help. @@ -1468,7 +1468,7 @@ spec( argv: [] of String, expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -1478,7 +1478,7 @@ spec( argv: ["arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -1488,7 +1488,7 @@ spec( argv: ["-b", "true"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -1498,7 +1498,7 @@ spec( argv: ["-b", "true", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -1508,7 +1508,7 @@ spec( argv: ["arg1", "-b", "true"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -1518,7 +1518,7 @@ spec( argv: ["-b", "false"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -1528,7 +1528,7 @@ spec( argv: ["-b", "false", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -1538,7 +1538,7 @@ spec( argv: ["arg1", "-b", "false"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -1548,7 +1548,7 @@ spec( argv: ["--bool", "true"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -1558,7 +1558,7 @@ spec( argv: ["--bool", "true", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -1568,7 +1568,7 @@ spec( argv: ["arg1", "--bool", "true"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => true, }, @@ -1578,7 +1578,7 @@ spec( argv: ["--bool", "false"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -1588,7 +1588,7 @@ spec( argv: ["--bool", "false", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, @@ -1598,7 +1598,7 @@ spec( argv: ["arg1", "--bool", "false"], expect_help: {{main_help_message}}, expect_opts: { - "type" => Bool?, + "type" => Bool, "method" => "bool", "expect_value" => false, }, diff --git a/spec/clim/dsl_spec/option_type_spec.cr b/spec/clim/dsl_spec/option_type_spec.cr deleted file mode 100644 index 60675c3b..00000000 --- a/spec/clim/dsl_spec/option_type_spec.cr +++ /dev/null @@ -1,695 +0,0 @@ -require "../dsl_spec" - -{% begin %} -{% - main_help_message = <<-HELP_MESSAGE - - Command Line Interface Tool. - - Usage: - - main_command_of_clim_library [options] [arguments] - - Options: - - -d=DEFAULT_TYPE Option description. [type:String] - --default-type=DEFAULT_TYPE Option description. [type:String] - --default-type-default=DEFAULT_TYPE - Option description. [type:String] [default:\"Default String!\"] - -i=VALUE, --int8=VALUE Option description. [type:Int8] - --int8-default=VALUE Option description. [type:Int8] [default:1] - --int16=VALUE Option description. [type:Int16] - --int32=VALUE Option description. [type:Int32] - --int64=VALUE Option description. [type:Int64] - --uint8=VALUE Option description. [type:UInt8] - --uint16=VALUE Option description. [type:UInt16] - --uint32=VALUE Option description. [type:UInt32] - --uint64=VALUE Option description. [type:UInt64] - --float32=VALUE Option description. [type:Float32] - --float64=VALUE Option description. [type:Float64] - --string=VALUE Option description. [type:String] - --bool Option description. [type:Bool] [default:false] - --bool-default Option description. [type:Bool] [default:false] - --array-int8=VALUE Option description. [type:Array(Int8)] - --array-int16=VALUE Option description. [type:Array(Int16)] - --array-int32=VALUE Option description. [type:Array(Int32)] - --array-int64=VALUE Option description. [type:Array(Int64)] - --array-int8-default=VALUE Option description. [type:Array(Int8)] [default:[] of Int8] - --array-int8-default-value=VALUE Option description. [type:Array(Int8)] [default:[1_i8, 2_i8, 3_i8]] - --array-int16-default=VALUE Option description. [type:Array(Int16)] [default:[] of Int16] - --array-int32-default=VALUE Option description. [type:Array(Int32)] [default:[] of Int32] - --array-int64-default=VALUE Option description. [type:Array(Int64)] [default:[] of Int64] - --array-uint8=VALUE Option description. [type:Array(UInt8)] - --array-uint16=VALUE Option description. [type:Array(UInt16)] - --array-uint32=VALUE Option description. [type:Array(UInt32)] - --array-uint64=VALUE Option description. [type:Array(UInt64)] - --array-uint8-default=VALUE Option description. [type:Array(UInt8)] [default:[] of UInt8] - --array-uint16-default=VALUE Option description. [type:Array(UInt16)] [default:[] of UInt16] - --array-uint32-default=VALUE Option description. [type:Array(UInt32)] [default:[] of UInt32] - --array-uint64-default=VALUE Option description. [type:Array(UInt64)] [default:[] of UInt64] - --array-float32=VALUE Option description. [type:Array(Float32)] - --array-float64=VALUE Option description. [type:Array(Float64)] - --array-string=VALUE Option description. [type:Array(String)] - --help Show this help. - - - HELP_MESSAGE -%} - -spec( - spec_class_name: OptionTypeSpec, - spec_dsl_lines: [ - "option \"-d=DEFAULT_TYPE\"", - "option \"--default-type=DEFAULT_TYPE\"", - "option \"--default-type-default=DEFAULT_TYPE\", default: \"Default String!\"", - "option \"-i=VALUE\", \"--int8=VALUE\", type: Int8", - "option \"--int8-default=VALUE\", type: Int8, default: 1_i8", - "option \"--int16=VALUE\", type: Int16", - "option \"--int32=VALUE\", type: Int32", - "option \"--int64=VALUE\", type: Int64", - "option \"--uint8=VALUE\", type: UInt8", - "option \"--uint16=VALUE\", type: UInt16", - "option \"--uint32=VALUE\", type: UInt32", - "option \"--uint64=VALUE\", type: UInt64", - "option \"--float32=VALUE\", type: Float32", - "option \"--float64=VALUE\", type: Float64", - "option \"--string=VALUE\", type: String", - "option \"--bool\", type: Bool", - "option \"--bool-default\", type: Bool, default: false", - "option \"--array-int8=VALUE\", type: Array(Int8)", - "option \"--array-int16=VALUE\", type: Array(Int16)", - "option \"--array-int32=VALUE\", type: Array(Int32)", - "option \"--array-int64=VALUE\", type: Array(Int64)", - "option \"--array-int8-default=VALUE\", type: Array(Int8), default: [] of Int8", - "option \"--array-int8-default-value=VALUE\", type: Array(Int8), default: [1_i8,2_i8,3_i8]", - "option \"--array-int16-default=VALUE\", type: Array(Int16), default: [] of Int16", - "option \"--array-int32-default=VALUE\", type: Array(Int32), default: [] of Int32", - "option \"--array-int64-default=VALUE\", type: Array(Int64), default: [] of Int64", - "option \"--array-uint8=VALUE\", type: Array(UInt8)", - "option \"--array-uint16=VALUE\", type: Array(UInt16)", - "option \"--array-uint32=VALUE\", type: Array(UInt32)", - "option \"--array-uint64=VALUE\", type: Array(UInt64)", - "option \"--array-uint8-default=VALUE\", type: Array(UInt8), default: [] of UInt8", - "option \"--array-uint16-default=VALUE\", type: Array(UInt16), default: [] of UInt16", - "option \"--array-uint32-default=VALUE\", type: Array(UInt32), default: [] of UInt32", - "option \"--array-uint64-default=VALUE\", type: Array(UInt64), default: [] of UInt64", - "option \"--array-float32=VALUE\", type: Array(Float32)", - "option \"--array-float64=VALUE\", type: Array(Float64)", - "option \"--array-string=VALUE\", type: Array(String)", - ], - spec_desc: "option type spec,", - spec_cases: [ - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => String?, - "method" => "d", - "expect_value" => nil, - }, - expect_args: [] of String, - }, - { - argv: ["-d", "foo", "bar"], - expect_help: {{main_help_message}}, - expect_opts: { - "type" => String?, - "method" => "d", - "expect_value" => "foo", - }, - expect_args: ["bar"] - }, - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => String?, - "method" => "default_type", - "expect_value" => nil, - }, - expect_args: [] of String, - }, - { - argv: ["--default-type", "foo", "bar"], - expect_help: {{main_help_message}}, - expect_opts: { - "type" => String?, - "method" => "default_type", - "expect_value" => "foo", - }, - expect_args: ["bar"] - }, - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => String, - "method" => "default_type_default", - "expect_value" => "Default String!", - }, - expect_args: [] of String, - }, - { - argv: ["--default-type-default", "foo", "bar"], - expect_help: {{main_help_message}}, - expect_opts: { - "type" => String, - "method" => "default_type_default", - "expect_value" => "foo", - }, - expect_args: ["bar"] - }, - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Int8?, - "method" => "int8", - "expect_value" => nil, - }, - expect_args: [] of String, - }, - { - argv: ["-i", "5"] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Int8?, - "method" => "int8", - "expect_value" => 5_i8, - }, - expect_args: [] of String, - }, - { - argv: ["--int8", "5"] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Int8?, - "method" => "int8", - "expect_value" => 5_i8, - }, - expect_args: [] of String, - }, - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Int8, - "method" => "int8_default", - "expect_value" => 1_i8, - }, - expect_args: [] of String, - }, - { - argv: ["--int8-default", "5"] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Int8, - "method" => "int8_default", - "expect_value" => 5_i8, - }, - expect_args: [] of String, - }, - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Int16?, - "method" => "int16", - "expect_value" => nil, - }, - expect_args: [] of String, - }, - { - argv: ["--int16", "5"] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Int16?, - "method" => "int16", - "expect_value" => 5_i16, - }, - expect_args: [] of String, - }, - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Int32?, - "method" => "int32", - "expect_value" => nil, - }, - expect_args: [] of String, - }, - { - argv: ["--int32", "5"] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Int32?, - "method" => "int32", - "expect_value" => 5_i32, - }, - expect_args: [] of String, - }, - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Int64?, - "method" => "int64", - "expect_value" => nil, - }, - expect_args: [] of String, - }, - { - argv: ["--int64", "5"] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Int64?, - "method" => "int64", - "expect_value" => 5_i64, - }, - expect_args: [] of String, - }, - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => UInt8?, - "method" => "uint8", - "expect_value" => nil, - }, - expect_args: [] of String, - }, - { - argv: ["--uint8", "5"] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => UInt8?, - "method" => "uint8", - "expect_value" => 5_u8, - }, - expect_args: [] of String, - }, - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => UInt16?, - "method" => "uint16", - "expect_value" => nil, - }, - expect_args: [] of String, - }, - { - argv: ["--uint16", "5"] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => UInt16?, - "method" => "uint16", - "expect_value" => 5_u16, - }, - expect_args: [] of String, - }, - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => UInt32?, - "method" => "uint32", - "expect_value" => nil, - }, - expect_args: [] of String, - }, - { - argv: ["--uint32", "5"] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => UInt32?, - "method" => "uint32", - "expect_value" => 5_u32, - }, - expect_args: [] of String, - }, - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => UInt64?, - "method" => "uint64", - "expect_value" => nil, - }, - expect_args: [] of String, - }, - { - argv: ["--uint64", "5"] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => UInt64?, - "method" => "uint64", - "expect_value" => 5_u64, - }, - expect_args: [] of String, - }, - - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Float32?, - "method" => "float32", - "expect_value" => nil, - }, - expect_args: [] of String, - }, - { - argv: ["--float32", "5.5"] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Float32?, - "method" => "float32", - "expect_value" => 5.5_f32, - }, - expect_args: [] of String, - }, - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Float64?, - "method" => "float64", - "expect_value" => nil, - }, - expect_args: [] of String, - }, - { - argv: ["--float64", "5.5"] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Float64?, - "method" => "float64", - "expect_value" => 5.5_f64, - }, - expect_args: [] of String, - }, - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => String?, - "method" => "string", - "expect_value" => nil, - }, - expect_args: [] of String, - }, - { - argv: ["--string", "5"] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => String?, - "method" => "string", - "expect_value" => "5", - }, - expect_args: [] of String, - }, - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Bool?, - "method" => "bool", - "expect_value" => false, - }, - expect_args: [] of String, - }, - { - argv: ["--bool"] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Bool?, - "method" => "bool", - "expect_value" => true, - }, - expect_args: [] of String, - }, - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Array(Int8)?, - "method" => "array_int8", - "expect_value" => nil, - }, - expect_args: [] of String, - }, - { - argv: ["--array-int8", "1", "--array-int8", "2"], - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Array(Int8)?, - "method" => "array_int8", - "expect_value" => [1_i8, 2_i8], - }, - expect_args: [] of String, - }, - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Array(Int8), - "method" => "array_int8_default", - "expect_value" => [] of Int8, - }, - expect_args: [] of String, - }, - { - argv: ["--array-int8-default", "1", "--array-int8-default", "2"], - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Array(Int8), - "method" => "array_int8_default", - "expect_value" => [1_i8, 2_i8], - }, - expect_args: [] of String, - }, - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Array(Int8), - "method" => "array_int8_default_value", - "expect_value" => [1_i8, 2_i8, 3_i8], - }, - expect_args: [] of String, - }, - { - argv: ["--array-int8-default-value", "8", "--array-int8-default-value", "9"], - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Array(Int8), - "method" => "array_int8_default_value", - "expect_value" => [8_i8, 9_i8], - }, - expect_args: [] of String, - }, - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Array(Int16)?, - "method" => "array_int16", - "expect_value" => nil, - }, - expect_args: [] of String, - }, - { - argv: ["--array-int16", "1", "--array-int16", "2"], - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Array(Int16)?, - "method" => "array_int16", - "expect_value" => [1_i16, 2_i16], - }, - expect_args: [] of String, - }, - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Array(Int32)?, - "method" => "array_int32", - "expect_value" => nil, - }, - expect_args: [] of String, - }, - { - argv: ["--array-int32", "1", "--array-int32", "2"], - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Array(Int32)?, - "method" => "array_int32", - "expect_value" => [1_i32, 2_i32], - }, - expect_args: [] of String, - }, - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Array(Int64)?, - "method" => "array_int64", - "expect_value" => nil, - }, - expect_args: [] of String, - }, - { - argv: ["--array-int64", "1", "--array-int64", "2"], - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Array(Int64)?, - "method" => "array_int64", - "expect_value" => [1_i64, 2_i64], - }, - expect_args: [] of String, - }, - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Array(UInt8)?, - "method" => "array_uint8", - "expect_value" => nil, - }, - expect_args: [] of String, - }, - { - argv: ["--array-uint8", "1", "--array-uint8", "2"], - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Array(UInt8)?, - "method" => "array_uint8", - "expect_value" => [1_u8, 2_u8], - }, - expect_args: [] of String, - }, - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Array(UInt16)?, - "method" => "array_uint16", - "expect_value" => nil, - }, - expect_args: [] of String, - }, - { - argv: ["--array-uint16", "1", "--array-uint16", "2"], - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Array(UInt16)?, - "method" => "array_uint16", - "expect_value" => [1_u16, 2_u16], - }, - expect_args: [] of String, - }, - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Array(UInt32)?, - "method" => "array_uint32", - "expect_value" => nil, - }, - expect_args: [] of String, - }, - { - argv: ["--array-uint32", "1", "--array-uint32", "2"], - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Array(UInt32)?, - "method" => "array_uint32", - "expect_value" => [1_u32, 2_u32], - }, - expect_args: [] of String, - }, - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Array(UInt64)?, - "method" => "array_uint64", - "expect_value" => nil, - }, - expect_args: [] of String, - }, - { - argv: ["--array-uint64", "1", "--array-uint64", "2"], - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Array(UInt64)?, - "method" => "array_uint64", - "expect_value" => [1_u64, 2_u64], - }, - expect_args: [] of String, - }, - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Array(Float32)?, - "method" => "array_float32", - "expect_value" => nil, - }, - expect_args: [] of String, - }, - { - argv: ["--array-float32", "1.1", "--array-float32", "2.2"], - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Array(Float32)?, - "method" => "array_float32", - "expect_value" => [1.1_f32, 2.2_f32], - }, - expect_args: [] of String, - }, - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Array(Float64)?, - "method" => "array_float64", - "expect_value" => nil, - }, - expect_args: [] of String, - }, - { - argv: ["--array-float64", "1.1", "--array-float64", "2.2"], - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Array(Float64)?, - "method" => "array_float64", - "expect_value" => [1.1_f64, 2.2_f64], - }, - expect_args: [] of String, - }, - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Array(String)?, - "method" => "array_string", - "expect_value" => nil, - }, - expect_args: [] of String, - }, - { - argv: ["--array-string", "array1", "--array-string", "array2"], - expect_help: {{main_help_message}}, - expect_opts: { - "type" => Array(String)?, - "method" => "array_string", - "expect_value" => ["array1", "array2"], - }, - expect_args: [] of String, - }, - ] -) -{% end %} diff --git a/spec/clim/dsl_spec/option_types/array/float_spec.cr b/spec/clim/dsl_spec/option_types/array/float_spec.cr new file mode 100644 index 00000000..10cfb397 --- /dev/null +++ b/spec/clim/dsl_spec/option_types/array/float_spec.cr @@ -0,0 +1,88 @@ +require "../../../dsl_spec" + +{% begin %} +{% + main_help_message = <<-HELP_MESSAGE + + Command Line Interface Tool. + + Usage: + + main_command_of_clim_library [options] [arguments] + + Options: + + --array-float32=VALUE Option description. [type:Array(Float32)] + --array-float64=VALUE Option description. [type:Array(Float64)] + --help Show this help. + + + HELP_MESSAGE +%} + +spec( + spec_class_name: OptionTypeSpec, + spec_dsl_lines: [ + "option \"--array-float32=VALUE\", type: Array(Float32)", + "option \"--array-float64=VALUE\", type: Array(Float64)", + ], + spec_desc: "option type spec,", + spec_cases: [ + # ==================================================== + # Array(Float32) + # ==================================================== + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Array(Float32), + "method" => "array_float32", + "expect_value" => [] of Float32, + }, + expect_args: [] of String, + }, + { + argv: ["--array-float32", "1.1", "--array-float32", "2.2"], + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Array(Float32), + "method" => "array_float32", + "expect_value" => [1.1_f32, 2.2_f32], + }, + expect_args: [] of String, + }, + { + argv: ["--array-float32", "foo"], + exception_message: "Invalid Float32: foo", + }, + + # ==================================================== + # Array(Float64) + # ==================================================== + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Array(Float64), + "method" => "array_float64", + "expect_value" => [] of Float64, + }, + expect_args: [] of String, + }, + { + argv: ["--array-float64", "1.1", "--array-float64", "2.2"], + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Array(Float64), + "method" => "array_float64", + "expect_value" => [1.1_f64, 2.2_f64], + }, + expect_args: [] of String, + }, + { + argv: ["--array-float64", "foo"], + exception_message: "Invalid Float64: foo", + }, + ] +) +{% end %} diff --git a/spec/clim/dsl_spec/option_types/array/int_spec.cr b/spec/clim/dsl_spec/option_types/array/int_spec.cr new file mode 100644 index 00000000..15301321 --- /dev/null +++ b/spec/clim/dsl_spec/option_types/array/int_spec.cr @@ -0,0 +1,206 @@ +require "../../../dsl_spec" + +{% begin %} +{% + main_help_message = <<-HELP_MESSAGE + + Command Line Interface Tool. + + Usage: + + main_command_of_clim_library [options] [arguments] + + Options: + + --array-int8=VALUE Option description. [type:Array(Int8)] + --array-int16=VALUE Option description. [type:Array(Int16)] + --array-int32=VALUE Option description. [type:Array(Int32)] + --array-int64=VALUE Option description. [type:Array(Int64)] + --array-int8-default=VALUE Option description. [type:Array(Int8)] [default:[] of Int8] + --array-int8-default-value=VALUE Option description. [type:Array(Int8)] [default:[1_i8, 2_i8, 3_i8]] + --array-int16-default=VALUE Option description. [type:Array(Int16)] [default:[] of Int16] + --array-int32-default=VALUE Option description. [type:Array(Int32)] [default:[] of Int32] + --array-int64-default=VALUE Option description. [type:Array(Int64)] [default:[] of Int64] + --help Show this help. + + + HELP_MESSAGE +%} + +spec( + spec_class_name: OptionTypeSpec, + spec_dsl_lines: [ + "option \"--array-int8=VALUE\", type: Array(Int8)", + "option \"--array-int16=VALUE\", type: Array(Int16)", + "option \"--array-int32=VALUE\", type: Array(Int32)", + "option \"--array-int64=VALUE\", type: Array(Int64)", + "option \"--array-int8-default=VALUE\", type: Array(Int8), default: [] of Int8", + "option \"--array-int8-default-value=VALUE\", type: Array(Int8), default: [1_i8,2_i8,3_i8]", + "option \"--array-int16-default=VALUE\", type: Array(Int16), default: [] of Int16", + "option \"--array-int32-default=VALUE\", type: Array(Int32), default: [] of Int32", + "option \"--array-int64-default=VALUE\", type: Array(Int64), default: [] of Int64", + ], + spec_desc: "option type spec,", + spec_cases: [ + # ==================================================== + # Array(Int8) + # ==================================================== + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Array(Int8), + "method" => "array_int8", + "expect_value" => [] of Int8, + }, + expect_args: [] of String, + }, + { + argv: ["--array-int8", "1", "--array-int8", "2"], + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Array(Int8), + "method" => "array_int8", + "expect_value" => [1_i8, 2_i8], + }, + expect_args: [] of String, + }, + { + argv: ["--array-int8", "foo"], + exception_message: "Invalid Int8: foo", + }, + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Array(Int8), + "method" => "array_int8_default", + "expect_value" => [] of Int8, + }, + expect_args: [] of String, + }, + { + argv: ["--array-int8-default", "1", "--array-int8-default", "2"], + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Array(Int8), + "method" => "array_int8_default", + "expect_value" => [1_i8, 2_i8], + }, + expect_args: [] of String, + }, + { + argv: ["--array-int8-default", "foo"], + exception_message: "Invalid Int8: foo", + }, + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Array(Int8), + "method" => "array_int8_default_value", + "expect_value" => [1_i8, 2_i8, 3_i8], + }, + expect_args: [] of String, + }, + { + argv: ["--array-int8-default-value", "8", "--array-int8-default-value", "9"], + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Array(Int8), + "method" => "array_int8_default_value", + "expect_value" => [8_i8, 9_i8], + }, + expect_args: [] of String, + }, + { + argv: ["--array-int8-default-value", "foo"], + exception_message: "Invalid Int8: foo", + }, + + # ==================================================== + # Array(Int16) + # ==================================================== + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Array(Int16), + "method" => "array_int16", + "expect_value" => [] of Int16, + }, + expect_args: [] of String, + }, + { + argv: ["--array-int16", "1", "--array-int16", "2"], + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Array(Int16), + "method" => "array_int16", + "expect_value" => [1_i16, 2_i16], + }, + expect_args: [] of String, + }, + { + argv: ["--array-int16", "foo"], + exception_message: "Invalid Int16: foo", + }, + + # ==================================================== + # Array(Int32) + # ==================================================== + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Array(Int32), + "method" => "array_int32", + "expect_value" => [] of Int32, + }, + expect_args: [] of String, + }, + { + argv: ["--array-int32", "1", "--array-int32", "2"], + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Array(Int32), + "method" => "array_int32", + "expect_value" => [1_i32, 2_i32], + }, + expect_args: [] of String, + }, + { + argv: ["--array-int32", "foo"], + exception_message: "Invalid Int32: foo", + }, + + # ==================================================== + # Array(Int64) + # ==================================================== + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Array(Int64), + "method" => "array_int64", + "expect_value" => [] of Int64, + }, + expect_args: [] of String, + }, + { + argv: ["--array-int64", "1", "--array-int64", "2"], + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Array(Int64), + "method" => "array_int64", + "expect_value" => [1_i64, 2_i64], + }, + expect_args: [] of String, + }, + { + argv: ["--array-int64", "foo"], + exception_message: "Invalid Int64: foo", + }, + ] +) +{% end %} diff --git a/spec/clim/dsl_spec/option_types/array/string_spec.cr b/spec/clim/dsl_spec/option_types/array/string_spec.cr new file mode 100644 index 00000000..2047dc49 --- /dev/null +++ b/spec/clim/dsl_spec/option_types/array/string_spec.cr @@ -0,0 +1,54 @@ +require "../../../dsl_spec" + +{% begin %} +{% + main_help_message = <<-HELP_MESSAGE + + Command Line Interface Tool. + + Usage: + + main_command_of_clim_library [options] [arguments] + + Options: + + --array-string=VALUE Option description. [type:Array(String)] + --help Show this help. + + + HELP_MESSAGE +%} + +spec( + spec_class_name: OptionTypeSpec, + spec_dsl_lines: [ + "option \"--array-string=VALUE\", type: Array(String)", + ], + spec_desc: "option type spec,", + spec_cases: [ + # ==================================================== + # Array(String) + # ==================================================== + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Array(String), + "method" => "array_string", + "expect_value" => [] of String, + }, + expect_args: [] of String, + }, + { + argv: ["--array-string", "array1", "--array-string", "array2"], + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Array(String), + "method" => "array_string", + "expect_value" => ["array1", "array2"], + }, + expect_args: [] of String, + }, + ] +) +{% end %} diff --git a/spec/clim/dsl_spec/option_types/array/uint_spec.cr b/spec/clim/dsl_spec/option_types/array/uint_spec.cr new file mode 100644 index 00000000..b03f97b4 --- /dev/null +++ b/spec/clim/dsl_spec/option_types/array/uint_spec.cr @@ -0,0 +1,156 @@ +require "../../../dsl_spec" + +{% begin %} +{% + main_help_message = <<-HELP_MESSAGE + + Command Line Interface Tool. + + Usage: + + main_command_of_clim_library [options] [arguments] + + Options: + + --array-uint8=VALUE Option description. [type:Array(UInt8)] + --array-uint16=VALUE Option description. [type:Array(UInt16)] + --array-uint32=VALUE Option description. [type:Array(UInt32)] + --array-uint64=VALUE Option description. [type:Array(UInt64)] + --array-uint8-default=VALUE Option description. [type:Array(UInt8)] [default:[] of UInt8] + --array-uint16-default=VALUE Option description. [type:Array(UInt16)] [default:[] of UInt16] + --array-uint32-default=VALUE Option description. [type:Array(UInt32)] [default:[] of UInt32] + --array-uint64-default=VALUE Option description. [type:Array(UInt64)] [default:[] of UInt64] + --help Show this help. + + + HELP_MESSAGE +%} + +spec( + spec_class_name: OptionTypeSpec, + spec_dsl_lines: [ + "option \"--array-uint8=VALUE\", type: Array(UInt8)", + "option \"--array-uint16=VALUE\", type: Array(UInt16)", + "option \"--array-uint32=VALUE\", type: Array(UInt32)", + "option \"--array-uint64=VALUE\", type: Array(UInt64)", + "option \"--array-uint8-default=VALUE\", type: Array(UInt8), default: [] of UInt8", + "option \"--array-uint16-default=VALUE\", type: Array(UInt16), default: [] of UInt16", + "option \"--array-uint32-default=VALUE\", type: Array(UInt32), default: [] of UInt32", + "option \"--array-uint64-default=VALUE\", type: Array(UInt64), default: [] of UInt64", + ], + spec_desc: "option type spec,", + spec_cases: [ + # ==================================================== + # Array(UInt8) + # ==================================================== + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Array(UInt8), + "method" => "array_uint8", + "expect_value" => [] of UInt8, + }, + expect_args: [] of String, + }, + { + argv: ["--array-uint8", "1", "--array-uint8", "2"], + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Array(UInt8), + "method" => "array_uint8", + "expect_value" => [1_u8, 2_u8], + }, + expect_args: [] of String, + }, + { + argv: ["--array-uint8", "foo"], + exception_message: "Invalid UInt8: foo", + }, + + # ==================================================== + # Array(UInt16) + # ==================================================== + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Array(UInt16), + "method" => "array_uint16", + "expect_value" => [] of UInt16, + }, + expect_args: [] of String, + }, + { + argv: ["--array-uint16", "1", "--array-uint16", "2"], + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Array(UInt16), + "method" => "array_uint16", + "expect_value" => [1_u16, 2_u16], + }, + expect_args: [] of String, + }, + { + argv: ["--array-uint16", "foo"], + exception_message: "Invalid UInt16: foo", + }, + + # ==================================================== + # Array(UInt32) + # ==================================================== + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Array(UInt32), + "method" => "array_uint32", + "expect_value" => [] of UInt32, + }, + expect_args: [] of String, + }, + { + argv: ["--array-uint32", "1", "--array-uint32", "2"], + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Array(UInt32), + "method" => "array_uint32", + "expect_value" => [1_u32, 2_u32], + }, + expect_args: [] of String, + }, + { + argv: ["--array-uint32", "foo"], + exception_message: "Invalid UInt32: foo", + }, + + # ==================================================== + # Array(UInt64) + # ==================================================== + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Array(UInt64), + "method" => "array_uint64", + "expect_value" => [] of UInt64, + }, + expect_args: [] of String, + }, + { + argv: ["--array-uint64", "1", "--array-uint64", "2"], + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Array(UInt64), + "method" => "array_uint64", + "expect_value" => [1_u64, 2_u64], + }, + expect_args: [] of String, + }, + { + argv: ["--array-uint64", "foo"], + exception_message: "Invalid UInt64: foo", + }, + ] +) +{% end %} diff --git a/spec/clim/dsl_spec/option_types/bool_spec.cr b/spec/clim/dsl_spec/option_types/bool_spec.cr new file mode 100644 index 00000000..86cb80e7 --- /dev/null +++ b/spec/clim/dsl_spec/option_types/bool_spec.cr @@ -0,0 +1,112 @@ +require "../../dsl_spec" + +{% begin %} +{% + main_help_message = <<-HELP_MESSAGE + + Command Line Interface Tool. + + Usage: + + main_command_of_clim_library [options] [arguments] + + Options: + + --bool Option description. [type:Bool] + --bool-equal=BOOL Option description. [type:Bool] + --bool-default Option description. [type:Bool] [default:false] + --help Show this help. + + + HELP_MESSAGE +%} + +spec( + spec_class_name: OptionTypeSpec, + spec_dsl_lines: [ + "option \"--bool\", type: Bool", + "option \"--bool-equal=BOOL\", type: Bool", + "option \"--bool-default\", type: Bool, default: false", + ], + spec_desc: "option type spec,", + spec_cases: [ + # ==================================================== + # Bool + # ==================================================== + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Bool, + "method" => "bool", + "expect_value" => false, + }, + expect_args: [] of String, + }, + { + argv: ["--bool"] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Bool, + "method" => "bool", + "expect_value" => true, + }, + expect_args: [] of String, + }, + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Bool, + "method" => "bool_equal", + "expect_value" => false, + }, + expect_args: [] of String, + }, + { + argv: ["--bool-equal=false"] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Bool, + "method" => "bool_equal", + "expect_value" => false, + }, + expect_args: [] of String, + }, + { + argv: ["--bool-equal=true"] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Bool, + "method" => "bool_equal", + "expect_value" => true, + }, + expect_args: [] of String, + }, + { + argv: ["--bool-equal=foo"], + exception_message: "Bool arguments accept only \"true\" or \"false\". Input: [foo]", + }, + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Bool, + "method" => "bool_default", + "expect_value" => false, + }, + expect_args: [] of String, + }, + { + argv: ["--bool-default"] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Bool, + "method" => "bool_default", + "expect_value" => true, + }, + expect_args: [] of String, + }, + ] +) +{% end %} diff --git a/spec/clim/dsl_spec/option_types/default_spec.cr b/spec/clim/dsl_spec/option_types/default_spec.cr new file mode 100644 index 00000000..84c5d838 --- /dev/null +++ b/spec/clim/dsl_spec/option_types/default_spec.cr @@ -0,0 +1,96 @@ +require "../../dsl_spec" + +{% begin %} +{% + main_help_message = <<-HELP_MESSAGE + + Command Line Interface Tool. + + Usage: + + main_command_of_clim_library [options] [arguments] + + Options: + + -d=DEFAULT_TYPE Option description. [type:String] + --default-type=DEFAULT_TYPE Option description. [type:String] + --default-type-default=DEFAULT_TYPE + Option description. [type:String] [default:\"Default String!\"] + --help Show this help. + + + HELP_MESSAGE +%} + +spec( + spec_class_name: OptionTypeSpec, + spec_dsl_lines: [ + "option \"-d=DEFAULT_TYPE\"", + "option \"--default-type=DEFAULT_TYPE\"", + "option \"--default-type-default=DEFAULT_TYPE\", default: \"Default String!\"", + ], + spec_desc: "option type spec,", + spec_cases: [ + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => String?, + "method" => "d", + "expect_value" => nil, + }, + expect_args: [] of String, + }, + { + argv: ["-d", "foo", "bar"], + expect_help: {{main_help_message}}, + expect_opts: { + "type" => String?, + "method" => "d", + "expect_value" => "foo", + }, + expect_args: ["bar"] + }, + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => String?, + "method" => "default_type", + "expect_value" => nil, + }, + expect_args: [] of String, + }, + { + argv: ["--default-type", "foo", "bar"], + expect_help: {{main_help_message}}, + expect_opts: { + "type" => String?, + "method" => "default_type", + "expect_value" => "foo", + }, + expect_args: ["bar"] + }, + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => String, + "method" => "default_type_default", + "expect_value" => "Default String!", + }, + expect_args: [] of String, + }, + { + argv: ["--default-type-default", "foo", "bar"], + expect_help: {{main_help_message}}, + expect_opts: { + "type" => String, + "method" => "default_type_default", + "expect_value" => "foo", + }, + expect_args: ["bar"] + }, + ] +) +{% end %} diff --git a/spec/clim/dsl_spec/option_types/float_spec.cr b/spec/clim/dsl_spec/option_types/float_spec.cr new file mode 100644 index 00000000..b973e45c --- /dev/null +++ b/spec/clim/dsl_spec/option_types/float_spec.cr @@ -0,0 +1,88 @@ +require "../../dsl_spec" + +{% begin %} +{% + main_help_message = <<-HELP_MESSAGE + + Command Line Interface Tool. + + Usage: + + main_command_of_clim_library [options] [arguments] + + Options: + + --float32=VALUE Option description. [type:Float32] + --float64=VALUE Option description. [type:Float64] + --help Show this help. + + + HELP_MESSAGE +%} + +spec( + spec_class_name: OptionTypeSpec, + spec_dsl_lines: [ + "option \"--float32=VALUE\", type: Float32", + "option \"--float64=VALUE\", type: Float64", + ], + spec_desc: "option type spec,", + spec_cases: [ + # ==================================================== + # Float32 + # ==================================================== + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Float32?, + "method" => "float32", + "expect_value" => nil, + }, + expect_args: [] of String, + }, + { + argv: ["--float32", "5.5"] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Float32?, + "method" => "float32", + "expect_value" => 5.5_f32, + }, + expect_args: [] of String, + }, + { + argv: ["--float32", "foo"], + exception_message: "Invalid Float32: foo", + }, + + # ==================================================== + # Float64 + # ==================================================== + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Float64?, + "method" => "float64", + "expect_value" => nil, + }, + expect_args: [] of String, + }, + { + argv: ["--float64", "5.5"] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Float64?, + "method" => "float64", + "expect_value" => 5.5_f64, + }, + expect_args: [] of String, + }, + { + argv: ["--float64", "foo"], + exception_message: "Invalid Float64: foo", + }, + ] +) +{% end %} diff --git a/spec/clim/dsl_spec/option_types/int_spec.cr b/spec/clim/dsl_spec/option_types/int_spec.cr new file mode 100644 index 00000000..88071257 --- /dev/null +++ b/spec/clim/dsl_spec/option_types/int_spec.cr @@ -0,0 +1,188 @@ +require "../../dsl_spec" + +{% begin %} +{% + main_help_message = <<-HELP_MESSAGE + + Command Line Interface Tool. + + Usage: + + main_command_of_clim_library [options] [arguments] + + Options: + + -i=VALUE, --int8=VALUE Option description. [type:Int8] + --int8-default=VALUE Option description. [type:Int8] [default:1] + --int16=VALUE Option description. [type:Int16] + --int32=VALUE Option description. [type:Int32] + --int64=VALUE Option description. [type:Int64] + --help Show this help. + + + HELP_MESSAGE +%} + +spec( + spec_class_name: OptionTypeSpec, + spec_dsl_lines: [ + "option \"-i=VALUE\", \"--int8=VALUE\", type: Int8", + "option \"--int8-default=VALUE\", type: Int8, default: 1_i8", + "option \"--int16=VALUE\", type: Int16", + "option \"--int32=VALUE\", type: Int32", + "option \"--int64=VALUE\", type: Int64", + ], + spec_desc: "option type spec,", + spec_cases: [ + # ==================================================== + # Int8 + # ==================================================== + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Int8?, + "method" => "int8", + "expect_value" => nil, + }, + expect_args: [] of String, + }, + { + argv: ["-i", "5"] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Int8?, + "method" => "int8", + "expect_value" => 5_i8, + }, + expect_args: [] of String, + }, + { + argv: ["-i", "foo"], + exception_message: "Invalid Int8: foo", + }, + { + argv: ["--int8", "5"] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Int8?, + "method" => "int8", + "expect_value" => 5_i8, + }, + expect_args: [] of String, + }, + { + argv: ["--int8", "foo"], + exception_message: "Invalid Int8: foo", + }, + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Int8, + "method" => "int8_default", + "expect_value" => 1_i8, + }, + expect_args: [] of String, + }, + { + argv: ["--int8-default", "5"] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Int8, + "method" => "int8_default", + "expect_value" => 5_i8, + }, + expect_args: [] of String, + }, + { + argv: ["--int8-default", "foo"], + exception_message: "Invalid Int8: foo", + }, + + # ==================================================== + # Int16 + # ==================================================== + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Int16?, + "method" => "int16", + "expect_value" => nil, + }, + expect_args: [] of String, + }, + { + argv: ["--int16", "5"] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Int16?, + "method" => "int16", + "expect_value" => 5_i16, + }, + expect_args: [] of String, + }, + { + argv: ["--int16", "foo"], + exception_message: "Invalid Int16: foo", + }, + + # ==================================================== + # Int32 + # ==================================================== + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Int32?, + "method" => "int32", + "expect_value" => nil, + }, + expect_args: [] of String, + }, + { + argv: ["--int32", "5"] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Int32?, + "method" => "int32", + "expect_value" => 5_i32, + }, + expect_args: [] of String, + }, + { + argv: ["--int32", "foo"], + exception_message: "Invalid Int32: foo", + }, + + # ==================================================== + # Int64 + # ==================================================== + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Int64?, + "method" => "int64", + "expect_value" => nil, + }, + expect_args: [] of String, + }, + { + argv: ["--int64", "5"] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => Int64?, + "method" => "int64", + "expect_value" => 5_i64, + }, + expect_args: [] of String, + }, + { + argv: ["--int64", "foo"], + exception_message: "Invalid Int64: foo", + }, + ] +) +{% end %} diff --git a/spec/clim/dsl_spec/option_types/string_spec.cr b/spec/clim/dsl_spec/option_types/string_spec.cr new file mode 100644 index 00000000..6888f150 --- /dev/null +++ b/spec/clim/dsl_spec/option_types/string_spec.cr @@ -0,0 +1,54 @@ +require "../../dsl_spec" + +{% begin %} +{% + main_help_message = <<-HELP_MESSAGE + + Command Line Interface Tool. + + Usage: + + main_command_of_clim_library [options] [arguments] + + Options: + + --string=VALUE Option description. [type:String] + --help Show this help. + + + HELP_MESSAGE +%} + +spec( + spec_class_name: OptionTypeSpec, + spec_dsl_lines: [ + "option \"--string=VALUE\", type: String", + ], + spec_desc: "option type spec,", + spec_cases: [ + # ==================================================== + # String + # ==================================================== + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => String?, + "method" => "string", + "expect_value" => nil, + }, + expect_args: [] of String, + }, + { + argv: ["--string", "5"] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => String?, + "method" => "string", + "expect_value" => "5", + }, + expect_args: [] of String, + }, + ] +) +{% end %} diff --git a/spec/clim/dsl_spec/option_types/uint_spec.cr b/spec/clim/dsl_spec/option_types/uint_spec.cr new file mode 100644 index 00000000..3c8abbf6 --- /dev/null +++ b/spec/clim/dsl_spec/option_types/uint_spec.cr @@ -0,0 +1,148 @@ +require "../../dsl_spec" + +{% begin %} +{% + main_help_message = <<-HELP_MESSAGE + + Command Line Interface Tool. + + Usage: + + main_command_of_clim_library [options] [arguments] + + Options: + + --uint8=VALUE Option description. [type:UInt8] + --uint16=VALUE Option description. [type:UInt16] + --uint32=VALUE Option description. [type:UInt32] + --uint64=VALUE Option description. [type:UInt64] + --help Show this help. + + + HELP_MESSAGE +%} + +spec( + spec_class_name: OptionTypeSpec, + spec_dsl_lines: [ + "option \"--uint8=VALUE\", type: UInt8", + "option \"--uint16=VALUE\", type: UInt16", + "option \"--uint32=VALUE\", type: UInt32", + "option \"--uint64=VALUE\", type: UInt64", + ], + spec_desc: "option type spec,", + spec_cases: [ + # ==================================================== + # UInt8 + # ==================================================== + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => UInt8?, + "method" => "uint8", + "expect_value" => nil, + }, + expect_args: [] of String, + }, + { + argv: ["--uint8", "5"] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => UInt8?, + "method" => "uint8", + "expect_value" => 5_u8, + }, + expect_args: [] of String, + }, + { + argv: ["--uint8", "foo"], + exception_message: "Invalid UInt8: foo", + }, + + # ==================================================== + # UInt16 + # ==================================================== + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => UInt16?, + "method" => "uint16", + "expect_value" => nil, + }, + expect_args: [] of String, + }, + { + argv: ["--uint16", "5"] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => UInt16?, + "method" => "uint16", + "expect_value" => 5_u16, + }, + expect_args: [] of String, + }, + { + argv: ["--uint16", "foo"], + exception_message: "Invalid UInt16: foo", + }, + + # ==================================================== + # UInt32 + # ==================================================== + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => UInt32?, + "method" => "uint32", + "expect_value" => nil, + }, + expect_args: [] of String, + }, + { + argv: ["--uint32", "5"] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => UInt32?, + "method" => "uint32", + "expect_value" => 5_u32, + }, + expect_args: [] of String, + }, + { + argv: ["--uint32", "foo"], + exception_message: "Invalid UInt32: foo", + }, + + # ==================================================== + # UInt64 + # ==================================================== + { + argv: [] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => UInt64?, + "method" => "uint64", + "expect_value" => nil, + }, + expect_args: [] of String, + }, + { + argv: ["--uint64", "5"] of String, + expect_help: {{main_help_message}}, + expect_opts: { + "type" => UInt64?, + "method" => "uint64", + "expect_value" => 5_u64, + }, + expect_args: [] of String, + }, + { + argv: ["--uint64", "foo"], + exception_message: "Invalid UInt64: foo", + }, + ] +) +{% end %} diff --git a/spec/clim/dsl_spec/string_spec.cr b/spec/clim/dsl_spec/string_spec.cr index d2d9a7b8..07184f52 100644 --- a/spec/clim/dsl_spec/string_spec.cr +++ b/spec/clim/dsl_spec/string_spec.cr @@ -534,7 +534,7 @@ spec( argv: [] of String, expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "default value", }, @@ -544,7 +544,7 @@ spec( argv: ["arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "default value", }, @@ -554,7 +554,7 @@ spec( argv: ["-s", "string1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "string1", }, @@ -564,7 +564,7 @@ spec( argv: ["-sstring1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "string1", }, @@ -574,7 +574,7 @@ spec( argv: ["--string", "string1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "string1", }, @@ -584,7 +584,7 @@ spec( argv: ["--string=string1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "string1", }, @@ -594,7 +594,7 @@ spec( argv: ["-s", "string1", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "string1", }, @@ -604,7 +604,7 @@ spec( argv: ["arg1", "-s", "string1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "string1", }, @@ -614,7 +614,7 @@ spec( argv: ["-string"], # Unintended case. expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "tring", }, @@ -624,7 +624,7 @@ spec( argv: ["-s=string1"], # Unintended case. expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "=string1", }, @@ -700,31 +700,11 @@ spec( ], spec_desc: "main command with String options,", spec_cases: [ - { - argv: [] of String, - expect_help: {{main_help_message}}, - expect_opts: { - "type" => String?, - "method" => "string", - "expect_value" => "default value", - }, - expect_args: [] of String, - }, - { - argv: ["arg1"], - expect_help: {{main_help_message}}, - expect_opts: { - "type" => String?, - "method" => "string", - "expect_value" => "default value", - }, - expect_args: ["arg1"], - }, { argv: ["-s", "string1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "string1", }, @@ -734,7 +714,7 @@ spec( argv: ["-sstring1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "string1", }, @@ -744,7 +724,7 @@ spec( argv: ["--string", "string1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "string1", }, @@ -754,7 +734,7 @@ spec( argv: ["--string=string1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "string1", }, @@ -764,7 +744,7 @@ spec( argv: ["-s", "string1", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "string1", }, @@ -774,7 +754,7 @@ spec( argv: ["arg1", "-s", "string1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "string1", }, @@ -784,7 +764,7 @@ spec( argv: ["-string"], # Unintended case. expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "tring", }, @@ -794,12 +774,20 @@ spec( argv: ["-s=string1"], # Unintended case. expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "=string1", }, expect_args: [] of String, }, + { + argv: [] of String, + exception_message: "Required options. \"-s ARG\"", + }, + { + argv: ["arg1"] of String, + exception_message: "Required options. \"-s ARG\"", + }, { argv: ["-h"], exception_message: "Undefined option. \"-h\"", @@ -874,7 +862,7 @@ spec( argv: ["-s", "string1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "string1", }, @@ -884,7 +872,7 @@ spec( argv: ["-sstring1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "string1", }, @@ -894,7 +882,7 @@ spec( argv: ["--string", "string1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "string1", }, @@ -904,7 +892,7 @@ spec( argv: ["--string=string1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "string1", }, @@ -914,7 +902,7 @@ spec( argv: ["-s", "string1", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "string1", }, @@ -924,7 +912,7 @@ spec( argv: ["arg1", "-s", "string1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "string1", }, @@ -934,7 +922,7 @@ spec( argv: ["-string"], # Unintended case. expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "tring", }, @@ -944,7 +932,7 @@ spec( argv: ["-s=string1"], # Unintended case. expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "=string1", }, @@ -954,6 +942,10 @@ spec( argv: [] of String, exception_message: "Required options. \"-s ARG\"", }, + { + argv: ["arg1"] of String, + exception_message: "Required options. \"-s ARG\"", + }, { argv: ["-h"], exception_message: "Undefined option. \"-h\"", @@ -1032,7 +1024,7 @@ spec( argv: [] of String, expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "default value", }, @@ -1042,7 +1034,7 @@ spec( argv: ["arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "default value", }, @@ -1052,7 +1044,7 @@ spec( argv: ["-s", "string1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "string1", }, @@ -1062,7 +1054,7 @@ spec( argv: ["-sstring1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "string1", }, @@ -1072,7 +1064,7 @@ spec( argv: ["--string", "string1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "string1", }, @@ -1082,7 +1074,7 @@ spec( argv: ["--string=string1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "string1", }, @@ -1092,7 +1084,7 @@ spec( argv: ["-s", "string1", "arg1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "string1", }, @@ -1102,7 +1094,7 @@ spec( argv: ["arg1", "-s", "string1"], expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "string1", }, @@ -1112,7 +1104,7 @@ spec( argv: ["-string"], # Unintended case. expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "tring", }, @@ -1122,7 +1114,7 @@ spec( argv: ["-s=string1"], # Unintended case. expect_help: {{main_help_message}}, expect_opts: { - "type" => String?, + "type" => String, "method" => "string", "expect_value" => "=string1", }, diff --git a/src/clim.cr b/src/clim.cr index e02c9417..e3a19f67 100644 --- a/src/clim.cr +++ b/src/clim.cr @@ -1,72 +1,7 @@ require "./clim/*" class Clim - SUPPORT_TYPES_INT = [ - Int8, - Int16, - Int32, - Int64, - ] - - SUPPORT_TYPES_UINT = [ - UInt8, - UInt16, - UInt32, - UInt64, - ] - - SUPPORT_TYPES_FLOAT = [ - Float32, - Float64, - ] - - SUPPORT_TYPES_STRING = [ - String, - ] - - SUPPORT_TYPES_BOOL = [ - Bool, - ] - - SUPPORT_TYPES_ARRAY = [ - Array(Int8), - Array(Int16), - Array(Int32), - Array(Int64), - Array(UInt8), - Array(UInt16), - Array(UInt32), - Array(UInt64), - Array(Float32), - Array(Float64), - Array(String), - ] - - SUPPORT_TYPES_ALL = [ - Int8, - Int16, - Int32, - Int64, - UInt8, - UInt16, - UInt32, - UInt64, - Float32, - Float64, - String, - Bool, - Array(Int8), - Array(Int16), - Array(Int32), - Array(Int64), - Array(UInt8), - Array(UInt16), - Array(UInt32), - Array(UInt64), - Array(Float32), - Array(Float64), - Array(String), - ] + include Types macro main_command(&block) @@ -82,7 +17,7 @@ class Clim start_parse(argv) rescue ex : ClimException puts "ERROR: #{ex.message}" - rescue ex : ClimInvalidOptionException + rescue ex : ClimInvalidOptionException | ClimInvalidTypeCastException puts "ERROR: #{ex.message}" puts "" puts "Please see the `--help`." diff --git a/src/clim/command.cr b/src/clim/command.cr index 7e8dec7e..bf884a01 100644 --- a/src/clim/command.cr +++ b/src/clim/command.cr @@ -121,20 +121,27 @@ class Clim end macro option_base(short, long, type, desc, default, required) - {% raise "Empty option name." if short.empty? %} - {% raise "Type [#{type}] is not supported on option." unless SUPPORT_TYPES_ALL.includes?(type) %} + {% raise "Empty option name." if short.empty? %} + {% raise "Type [#{type}] is not supported on option." unless SUPPORT_TYPES.keys.includes?(type) %} {% base_option_name = long == nil ? short : long %} {% option_name = base_option_name.id.stringify.gsub(/\=/, " ").split(" ").first.id.stringify.gsub(/^-+/, "").gsub(/-/, "_").id %} class OptionsForEachCommand class Option_{{option_name}} < Option - define_option_macro({{type}}, {{default}}) + define_option_macro({{type}}, {{default}}, {{required}}) end {% default = false if type.id.stringify == "Bool" %} {% raise "You can not specify 'required: true' for Bool option." if type.id.stringify == "Bool" && required == true %} - property {{ option_name }}_instance : Option_{{option_name}} = Option_{{option_name}}.new({{ short }}, {% unless long == nil %} {{ long }}, {% end %} {{ desc }}, {{ default }}, {{ required }}) - def {{ option_name }} : {{ type }}? + + {% if default == nil %} + {% default_value = SUPPORT_TYPES[type][:nilable] ? default : SUPPORT_TYPES[type][:default] %} + {% else %} + {% default_value = default %} + {% end %} + + property {{ option_name }}_instance : Option_{{option_name}} = Option_{{option_name}}.new({{ short }}, {% unless long == nil %} {{ long }}, {% end %} {{ desc }}, {{ default_value }}, {{ required }}) + def {{ option_name }} {{ option_name }}_instance.@value end end diff --git a/src/clim/command/options.cr b/src/clim/command/options.cr index c80a42a3..671da59f 100644 --- a/src/clim/command/options.cr +++ b/src/clim/command/options.cr @@ -8,14 +8,14 @@ class Clim def invalid_required_names ret = [] of String | Nil {% for iv in @type.instance_vars.reject { |iv| iv.stringify == "help" } %} - short_or_nil = {{iv}}.required_set? ? {{iv}}.short : nil + short_or_nil = {{iv}}.required_not_set? ? {{iv}}.short : nil ret << short_or_nil {% end %} ret.compact end def setup_parser(parser) - {% for iv in @type.instance_vars.reject{|iv| iv.stringify == "help"} %} + {% for iv in @type.instance_vars.reject { |iv| iv.stringify == "help" } %} long = {{iv}}.long if long.nil? parser.on({{iv}}.short, {{iv}}.desc) {|arg| {{iv}}.set_value(arg) } diff --git a/src/clim/command/options/option.cr b/src/clim/command/options/option.cr index d954452d..9c3b30b2 100644 --- a/src/clim/command/options/option.cr +++ b/src/clim/command/options/option.cr @@ -8,25 +8,29 @@ class Clim property required : Bool = false property array_set_flag : Bool = false - def required_set? : Bool - @required && @value.nil? + def required_not_set? : Bool + @required && !set_value? end private def display_default default_value = @default.dup {% begin %} + {% support_types_number = SUPPORT_TYPES.map { |k, v| v[:type] == "number" ? k : nil }.reject(&.==(nil)) %} + {% support_types_string = SUPPORT_TYPES.map { |k, v| v[:type] == "string" ? k : nil }.reject(&.==(nil)) %} + {% support_types_bool = SUPPORT_TYPES.map { |k, v| v[:type] == "bool" ? k : nil }.reject(&.==(nil)) %} + {% support_types_array = SUPPORT_TYPES.map { |k, v| v[:type] == "array" ? k : nil }.reject(&.==(nil)) %} case default_value when Nil "nil" - when {{*SUPPORT_TYPES_BOOL}} + when {{*support_types_bool}} default_value - when {{*SUPPORT_TYPES_STRING}} + when {{*support_types_string}} default_value.empty? ? "\"\"" : "\"#{default_value}\"" - when {{*(SUPPORT_TYPES_INT + SUPPORT_TYPES_UINT + SUPPORT_TYPES_FLOAT)}} + when {{*support_types_number}} default_value - {% for type in (SUPPORT_TYPES_INT + SUPPORT_TYPES_UINT + SUPPORT_TYPES_FLOAT + SUPPORT_TYPES_STRING) %} - when Array({{type}}) - default_value.empty? ? "[] of {{type}}" : default + {% for type in support_types_array %} + when {{type}} + default_value.empty? ? "[] of {{type.type_vars.first}}" : default {% end %} else raise ClimException.new "[#{typeof(default)}] is not supported." @@ -34,91 +38,62 @@ class Clim {% end %} end - macro define_option_macro(type, default) - {% value_type = default == nil ? type.stringify + "?" : type.stringify %} - property default : {{value_type.id}} = {{default}} - property value : {{value_type.id}} = {{default}} + macro define_option_macro(type, default, required) + {% if default != nil %} + {% value_type = type.stringify.id %} + {% value_default = default %} + {% value_assign = "default".id %} + {% default_type = type.stringify.id %} + {% elsif default == nil && required == true %} + {% value_type = type.stringify.id %} + {% value_default = SUPPORT_TYPES[type][:default] %} + {% value_assign = SUPPORT_TYPES[type][:default] %} + {% default_type = SUPPORT_TYPES[type][:nilable] ? (type.stringify + "?").id : type.stringify.id %} + {% elsif default == nil && required == false %} + {% value_type = SUPPORT_TYPES[type][:nilable] ? (type.stringify + "?").id : type.stringify.id %} + {% value_default = SUPPORT_TYPES[type][:nilable] ? default : SUPPORT_TYPES[type][:default] %} + {% value_assign = SUPPORT_TYPES[type][:nilable] ? "default".id : SUPPORT_TYPES[type][:default] %} + {% default_type = SUPPORT_TYPES[type][:nilable] ? (type.stringify + "?").id : type.stringify.id %} + {% end %} + + property value : {{value_type}} = {{value_default}} + property default : {{default_type}} = {{ SUPPORT_TYPES[type][:nilable] ? default : SUPPORT_TYPES[type][:default] }} + property set_value : Bool = false - def initialize(@short : String, @long : String, @desc : String, @default : {{value_type.id}}, @required : Bool) - @value = default + def initialize(@short : String, @long : String, @desc : String, @default : {{default_type}}, @required : Bool) + @value = {{value_assign}} end - def initialize(@short : String, @desc : String, @default : {{value_type.id}}, @required : Bool) + def initialize(@short : String, @desc : String, @default : {{default_type}}, @required : Bool) @long = nil - @value = default + @value = {{value_assign}} end def desc desc = @desc desc = desc + " [type:#{{{type}}.to_s}]" - desc = desc + " [default:#{display_default}]" unless default.nil? + desc = desc + " [default:#{display_default}]" unless {{(default == nil).id}} desc = desc + " [required]" if required desc end def set_value(arg : String) - {% if type.id == Int8.id %} - @value = arg.to_i8 - {% elsif type.id == Int16.id %} - @value = arg.to_i16 - {% elsif type.id == Int32.id %} - @value = arg.to_i32 - {% elsif type.id == Int64.id %} - @value = arg.to_i64 - {% elsif type.id == UInt8.id %} - @value = arg.to_u8 - {% elsif type.id == UInt16.id %} - @value = arg.to_u16 - {% elsif type.id == UInt32.id %} - @value = arg.to_u32 - {% elsif type.id == UInt64.id %} - @value = arg.to_u64 - {% elsif type.id == Float32.id %} - @value = arg.to_f32 - {% elsif type.id == Float64.id %} - @value = arg.to_f64 - {% elsif type.id == String.id %} - @value = arg.to_s - {% elsif type.id == Bool.id %} - @value = arg.try do |obj| - next true if obj.empty? - unless obj === "true" || obj == "false" - raise ClimException.new "Bool arguments accept only \"true\" or \"false\". Input: [#{obj}]" - end - obj === "true" - end - {% elsif type.id == "Array(Int8)".id %} - add_array_value(Int8, to_i8) - {% elsif type.id == "Array(Int16)".id %} - add_array_value(Int16, to_i16) - {% elsif type.id == "Array(Int32)".id %} - add_array_value(Int32, to_i32) - {% elsif type.id == "Array(Int64)".id %} - add_array_value(Int64, to_i64) - {% elsif type.id == "Array(UInt8)".id %} - add_array_value(UInt8, to_u8) - {% elsif type.id == "Array(UInt16)".id %} - add_array_value(UInt16, to_u16) - {% elsif type.id == "Array(UInt32)".id %} - add_array_value(UInt32, to_u32) - {% elsif type.id == "Array(UInt64)".id %} - add_array_value(UInt64, to_u64) - {% elsif type.id == "Array(Float32)".id %} - add_array_value(Float32, to_f32) - {% elsif type.id == "Array(Float64)".id %} - add_array_value(Float64, to_f64) - {% elsif type.id == "Array(String)".id %} - add_array_value(String, to_s) - {% else %} - {% raise "Type [#{type}] is not supported on option." %} - {% end %} + {% raise "Type [#{type}] is not supported on option." unless SUPPORT_TYPES.keys.includes?(type) %} + @value = {{SUPPORT_TYPES[type][:convert_arg_process].id}} + @set_value = true + rescue ex + raise ClimInvalidTypeCastException.new ex.message + end + + def set_value? + @set_value end end - macro add_array_value(type, cast_method) + macro add_array_value(type, casted_arg) @value = [] of {{type}} if @array_set_flag == false @array_set_flag = true - @value = @value.nil? ? [arg.{{cast_method}}] : @value.try &.<<(arg.{{cast_method}}) + @value.nil? ? [{{casted_arg}}] : @value.try &.<<({{casted_arg}}) end end end diff --git a/src/clim/exception.cr b/src/clim/exception.cr index cf26737d..5d7b1d98 100644 --- a/src/clim/exception.cr +++ b/src/clim/exception.cr @@ -3,4 +3,6 @@ class Clim end class ClimInvalidOptionException < Exception end + class ClimInvalidTypeCastException < Exception + end end diff --git a/src/clim/types.cr b/src/clim/types.cr new file mode 100644 index 00000000..ba4865e4 --- /dev/null +++ b/src/clim/types.cr @@ -0,0 +1,152 @@ +class Clim + module Types + SUPPORT_TYPES = { + Int8 => { + type: "number", + default: 0, + nilable: true, + convert_arg_process: "arg.to_i8", + }, + Int16 => { + type: "number", + default: 0, + nilable: true, + convert_arg_process: "arg.to_i16", + }, + Int32 => { + type: "number", + default: 0, + nilable: true, + convert_arg_process: "arg.to_i32", + }, + Int64 => { + type: "number", + default: 0, + nilable: true, + convert_arg_process: "arg.to_i64", + }, + UInt8 => { + type: "number", + default: 0, + nilable: true, + convert_arg_process: "arg.to_u8", + }, + UInt16 => { + type: "number", + default: 0, + nilable: true, + convert_arg_process: "arg.to_u16", + }, + UInt32 => { + type: "number", + default: 0, + nilable: true, + convert_arg_process: "arg.to_u32", + }, + UInt64 => { + type: "number", + default: 0, + nilable: true, + convert_arg_process: "arg.to_u64", + }, + Float32 => { + type: "number", + default: 0.0, + nilable: true, + convert_arg_process: "arg.to_f32", + }, + Float64 => { + type: "number", + default: 0.0, + nilable: true, + convert_arg_process: "arg.to_f64", + }, + String => { + type: "string", + default: "", + nilable: true, + convert_arg_process: "arg.to_s", + }, + Bool => { + type: "bool", + default: false, + nilable: false, + convert_arg_process: <<-PROCESS + arg.try do |obj| + next true if obj.empty? + unless obj === "true" || obj == "false" + raise ClimException.new "Bool arguments accept only \\"true\\" or \\"false\\". Input: [\#{obj}]" + end + obj === "true" + end + PROCESS, + }, + Array(Int8) => { + type: "array", + default: [] of Int8, + nilable: false, + convert_arg_process: "add_array_value(Int8, arg.to_i8)", + }, + Array(Int16) => { + type: "array", + default: [] of Int16, + nilable: false, + convert_arg_process: "add_array_value(Int16, arg.to_i16)", + }, + Array(Int32) => { + type: "array", + default: [] of Int32, + nilable: false, + convert_arg_process: "add_array_value(Int32, arg.to_i32)", + }, + Array(Int64) => { + type: "array", + default: [] of Int64, + nilable: false, + convert_arg_process: "add_array_value(Int64, arg.to_i64)", + }, + Array(UInt8) => { + type: "array", + default: [] of UInt8, + nilable: false, + convert_arg_process: "add_array_value(UInt8, arg.to_u8)", + }, + Array(UInt16) => { + type: "array", + default: [] of UInt16, + nilable: false, + convert_arg_process: "add_array_value(UInt16, arg.to_u16)", + }, + Array(UInt32) => { + type: "array", + default: [] of UInt32, + nilable: false, + convert_arg_process: "add_array_value(UInt32, arg.to_u32)", + }, + Array(UInt64) => { + type: "array", + default: [] of UInt64, + nilable: false, + convert_arg_process: "add_array_value(UInt64, arg.to_u64)", + }, + Array(Float32) => { + type: "array", + default: [] of Float32, + nilable: false, + convert_arg_process: "add_array_value(Float32, arg.to_f32)", + }, + Array(Float64) => { + type: "array", + default: [] of Float64, + nilable: false, + convert_arg_process: "add_array_value(Float64, arg.to_f64)", + }, + Array(String) => { + type: "array", + default: [] of String, + nilable: false, + convert_arg_process: "add_array_value(String, arg.to_s)", + }, + } + end +end diff --git a/src/clim/version.cr b/src/clim/version.cr index a33e8f8c..e3a0314e 100644 --- a/src/clim/version.cr +++ b/src/clim/version.cr @@ -1,3 +1,3 @@ class Clim - VERSION = "0.2.2" + VERSION = "0.3.0" end