diff --git a/shard.yml b/shard.yml index 2f8ca60..5e8cc7a 100644 --- a/shard.yml +++ b/shard.yml @@ -11,4 +11,4 @@ license: MIT development_dependencies: ameba: github: crystal-ameba/ameba - version: ~> 1.0.0 + version: ~> 1.5.0 diff --git a/spec/lucky_task/runner_spec.cr b/spec/lucky_task/runner_spec.cr index 334fbf9..fe2fe59 100644 --- a/spec/lucky_task/runner_spec.cr +++ b/spec/lucky_task/runner_spec.cr @@ -4,9 +4,10 @@ include HaveDefaultHelperMessageMatcher describe LuckyTask::Runner do it "adds tasks to the runner when task classes are created" do - expected_task_names = ["another_task", "my.cool_task", "my.custom_name"] + expected_task_names = ["another_task", "my.cool_task", "my.custom_name", "task_with_args", "task_with_required_format_args", "task_with_switch_flags", "task_with_int32_flags", "task_with_float64_flags", "task_with_positional_args", "task_with_fancy_output"] task_names = LuckyTask::Runner.tasks.map(&.name) + task_names.size.should eq(expected_task_names.size) expected_task_names.each do |expected_task_name| task_names.should contain(expected_task_name) diff --git a/spec/lucky_task/task_spec.cr b/spec/lucky_task/task_spec.cr index 2361fe6..ca320f4 100644 --- a/spec/lucky_task/task_spec.cr +++ b/spec/lucky_task/task_spec.cr @@ -44,24 +44,24 @@ describe LuckyTask::Task do describe "with command line args" do it "creates methods for the args and returns their values" do - task = TaskWithArgs.new.print_help_or_call(args: ["--model-name=User", "--model-type=Polymorphic"]).not_nil! + task = TaskWithArgs.new.print_help_or_call(args: ["--model-name=User", "--model-type=Polymorphic"]).as(TaskWithArgs) task.model_name.should eq "User" task.model_type.should eq "Polymorphic" end it "allows the args to be optional" do - task = TaskWithArgs.new.print_help_or_call(args: ["--model-name=User"]).not_nil! + task = TaskWithArgs.new.print_help_or_call(args: ["--model-name=User"]).as(TaskWithArgs) task.model_name.should eq "User" task.model_type.should eq nil end it "allows using an arg shortcut" do - task = TaskWithArgs.new.print_help_or_call(args: ["-m User"]).not_nil! + task = TaskWithArgs.new.print_help_or_call(args: ["-m User"]).as(TaskWithArgs) task.model_name.should eq "User" end it "raises an error when an arg is required and not passed" do - task = TaskWithRequiredFormatArgs.new.print_help_or_call(args: [""]).not_nil! + task = TaskWithRequiredFormatArgs.new.print_help_or_call(args: [""]).as(TaskWithRequiredFormatArgs) expect_raises(Exception, /--theme=SOME_VALUE/) do task.theme end @@ -80,23 +80,23 @@ describe LuckyTask::Task do end it "sets switch flags that default to false" do - task = TaskWithSwitchFlags.new.print_help_or_call(args: [] of String).not_nil! + task = TaskWithSwitchFlags.new.print_help_or_call(args: [] of String).as(TaskWithSwitchFlags) task.admin?.should eq false end it "sets switch flags from args" do - task = TaskWithSwitchFlags.new.print_help_or_call(args: ["-a"]).not_nil! + task = TaskWithSwitchFlags.new.print_help_or_call(args: ["-a"]).as(TaskWithSwitchFlags) task.admin?.should eq true end it "sets int32 flags that default to 0" do - task = TaskWithInt32Flags.new.print_help_or_call(args: [] of String).not_nil! + task = TaskWithInt32Flags.new.print_help_or_call(args: [] of String).as(TaskWithInt32Flags) task.zero.should eq 0 task.uno.should eq 1 end it "sets int32 flags from args" do - task = TaskWithInt32Flags.new.print_help_or_call(args: ["-u 10000", "--zero=1_000"]).not_nil! + task = TaskWithInt32Flags.new.print_help_or_call(args: ["-u 10000", "--zero=1_000"]).as(TaskWithInt32Flags) task.zero.should eq 1_000 task.uno.should eq 10_000 expect_raises(Exception, /"nada" is an invalid value for uno/) do @@ -105,20 +105,20 @@ describe LuckyTask::Task do end it "sets explicit negative/positive int32 flags from args" do - task = TaskWithInt32Flags.new.print_help_or_call(args: ["-u -10_000", "--zero=+1_000"]).not_nil! + task = TaskWithInt32Flags.new.print_help_or_call(args: ["-u -10_000", "--zero=+1_000"]).as(TaskWithInt32Flags) task.zero.should eq 1_000 task.uno.should eq -10_000 end it "sets float64 flags that default to 0" do - task = TaskWithFloat64Flags.new.print_help_or_call(args: [] of String).not_nil! + task = TaskWithFloat64Flags.new.print_help_or_call(args: [] of String).as(TaskWithFloat64Flags) task.zero.should eq 0.0 task.uno.should eq 1.0 task.pi.should eq 3.14 end it "sets float64 flags from args" do - task = TaskWithFloat64Flags.new.print_help_or_call(args: ["-u 123_456.789", "--zero=1_000"]).not_nil! + task = TaskWithFloat64Flags.new.print_help_or_call(args: ["-u 123_456.789", "--zero=1_000"]).as(TaskWithFloat64Flags) task.zero.should eq 1_000.0 task.uno.should eq 123_456.789 task.pi.should eq 3.14 @@ -128,13 +128,13 @@ describe LuckyTask::Task do end it "sets explicit negative/positive float64 flags from args" do - task = TaskWithFloat64Flags.new.print_help_or_call(args: ["-u -100", "--zero=+505.1"]).not_nil! + task = TaskWithFloat64Flags.new.print_help_or_call(args: ["-u -100", "--zero=+505.1"]).as(TaskWithFloat64Flags) task.zero.should eq 505.1 task.uno.should eq -100.0 end it "allows positional args that do not require a flag name" do - task = TaskWithPositionalArgs.new.print_help_or_call(args: ["User", "name:String", "email:String"]).not_nil! + task = TaskWithPositionalArgs.new.print_help_or_call(args: ["User", "name:String", "email:String"]).as(TaskWithPositionalArgs) task.model.should eq "User" task.columns.should eq ["name:String", "email:String"] end diff --git a/spec/lucky_task_spec.cr b/spec/lucky_task_spec.cr index 1afd23d..d7c41a9 100644 --- a/spec/lucky_task_spec.cr +++ b/spec/lucky_task_spec.cr @@ -6,7 +6,7 @@ describe LuckyTask do file.gets_to_end end - version = /version\:(.*?)\n/.match(content).not_nil![1].strip + version = /version\:(.*?)\n/.match(content).as(Regex::MatchData)[1].strip version.should eq LuckyTask::VERSION end end diff --git a/src/lucky_task/runner.cr b/src/lucky_task/runner.cr index 154ff4e..4369afa 100644 --- a/src/lucky_task/runner.cr +++ b/src/lucky_task/runner.cr @@ -62,6 +62,6 @@ class LuckyTask::Runner end def self.longest_task_name - tasks.map(&.name.size).max + tasks.max_of(&.name.size) end end diff --git a/src/lucky_task/task.cr b/src/lucky_task/task.cr index d5fb4fa..8b2363c 100644 --- a/src/lucky_task/task.cr +++ b/src/lucky_task/task.cr @@ -5,15 +5,17 @@ abstract class LuckyTask::Task property option_parser : OptionParser = OptionParser.new property output : IO = STDOUT + macro finished {% if !@type.abstract? %} LuckyTask::Runner.tasks << self.new {% end %} + end - def name + def name : String "{{@type.name.gsub(/::/, ".").underscore}}" end - def help_message + def help_message : String <<-TEXT #{summary} @@ -33,7 +35,7 @@ abstract class LuckyTask::Task end end - private def wants_help_message?(args) + private def wants_help_message?(args) : Bool args.any? { |arg| {"--help", "-h", "help"}.includes?(arg) } end end @@ -105,7 +107,7 @@ abstract class LuckyTask::Task if @{{ arg_name.id }}.nil? raise "{{ arg_name.id }} is required, but no value was passed." end - @{{ arg_name.id }}.not_nil! + @{{ arg_name.id }}.as({% if to_end %}Array(String){% else %}String{% end %}) end end @@ -155,7 +157,7 @@ abstract class LuckyTask::Task --{{ arg_name.id.stringify.underscore.gsub(/_/, "-").id }}=SOME_VALUE ERROR end - @{{ arg_name.id }}.not_nil! + @{{ arg_name.id }}.as(String) {% else %} @{{ arg_name.id }} {% end %}