Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1880 from Shopify/suggest-corrected-store-param
Browse files Browse the repository at this point in the history
Recognize attempts to pass a store name and suggest correction
  • Loading branch information
amcaplan authored Mar 23, 2022
2 parents e7427ca + 10bab7f commit 54c9672
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ From version 2.6.0, the sections in this file adhere to the [keep a changelog](h

### Added
* [#1934](https://github.com/Shopify/shopify-cli/pull/1934): Block directories in theme assets
* [#1880](https://github.com/Shopify/shopify-cli/pull/1880): Recognize attempts to pass a store name and suggest correction

### Fixed
* [#1874](https://github.com/Shopify/shopify-cli/pull/1874): Make ngrok errors more robust and helpful
Expand Down
7 changes: 7 additions & 0 deletions lib/shopify_cli/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ def call(args, command_name, *)
end
rescue OptionParser::InvalidOption => error
arg = error.args.first
store_name = arg.match(/\A--(?<store_name>.*\.myshopify\.com)\z/)&.[](:store_name)
if store_name && !arg.match?(/\A--(store|shop)=/)
# Sometimes it may look like --invalidoption=https://storename.myshopify.com
store_name = store_name.sub(%r{\A(.*=)?(https?://)?}, "")
raise ShopifyCLI::Abort,
@ctx.message("core.errors.option_parser.invalid_option_store_equals", arg, store_name)
end
raise ShopifyCLI::Abort, @ctx.message("core.errors.option_parser.invalid_option", arg)
rescue OptionParser::MissingArgument => error
arg = error.args.first
Expand Down
6 changes: 6 additions & 0 deletions lib/shopify_cli/messages/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ module Messages
missing_ruby: "Ruby is required to continue. Install Ruby here: https://www.ruby-lang.org/en/downloads.",
option_parser: {
invalid_option: "The option {{command:%s}} is not supported.",
invalid_option_store_equals: <<~MESSAGE,
The option {{command:%s}} isn't recognized.
Try this:
{{command:--store=%s}}.
MESSAGE
missing_argument: "The required argument {{command:%s}} is missing.",
},
},
Expand Down
33 changes: 33 additions & 0 deletions test/shopify-cli/commands/command_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,39 @@ def test_calls_help_with_subcommand_h_flag

assert_match(CLI::UI.fmt(ShopifyCLI::Commands::Populate::Customer.help), io.join)
end

[
"",
"http://",
"https://",
"invalidoption=",
"invalidoption=https://",
["store=", false],
["shop=", false],
["s=", false, "-s="],
].each do |prefix, correction_expected = true, full_prefix = "--#{prefix}"|
store_name = "mystore.myshopify.com"
store_name_with_prefix = "#{full_prefix}#{store_name}"

define_method("test_calls_with#{"_prefix_#{prefix}" unless prefix.empty?}_store_as_raw_param") do
io = capture_io_and_assert_raises(ShopifyCLI::Abort) do
run_cmd("help login #{store_name_with_prefix}")
end

if correction_expected
assert_message_output(io: io, expected_content: [
@context.message(
"core.errors.option_parser.invalid_option_store_equals",
store_name_with_prefix, store_name
),
])
else
assert_message_output(io: io, expected_content: [
@context.message("core.errors.option_parser.invalid_option", store_name_with_prefix),
])
end
end
end
end
end
end

0 comments on commit 54c9672

Please sign in to comment.