From cc52bb036940ee75c4b42cc6e280f5bd641e8de7 Mon Sep 17 00:00:00 2001 From: Ben Lewis Date: Sat, 30 Jun 2018 16:11:10 +0300 Subject: [PATCH] Implement 'With crystal foo.cr --flag, when --flag isn't found, mention did you mean crystal foo.cr -- --flag' from PR #5291 --- spec/std/option_parser_spec.cr | 3 ++- src/option_parser.cr | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/spec/std/option_parser_spec.cr b/spec/std/option_parser_spec.cr index f5fc6c7e4467..80808f03ad37 100644 --- a/spec/std/option_parser_spec.cr +++ b/spec/std/option_parser_spec.cr @@ -215,7 +215,8 @@ describe "OptionParser" do end it "raises on invalid option" do - expect_raises OptionParser::InvalidOption, "Invalid option: -j" do + suggestion = "(did you mean 'crystal foo.cr -- -j'?)".colorize.yellow.bold.to_s + expect_raises OptionParser::InvalidOption, "Invalid option: -j #{suggestion}" do OptionParser.parse(["-f", "-j"]) do |opts| opts.on("-f", "some flag") { } end diff --git a/src/option_parser.cr b/src/option_parser.cr index c5e27b89ab4e..71a42db2adf5 100644 --- a/src/option_parser.cr +++ b/src/option_parser.cr @@ -35,7 +35,11 @@ class OptionParser class InvalidOption < Exception def initialize(option) - super("Invalid option: #{option}") + super("Invalid option: #{option} #{ + type.program.colorize( + "(did you mean 'crystal foo.cr -- #{option}')" + ).yellow.bold + }") end end