Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove redundant call of usage message #250

Merged
merged 6 commits into from
Dec 17, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions lib/cli.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@ defmodule Onigumo.CLI do
}

def main(argv) do
case OptionParser.parse(
argv,
aliases: [C: :working_dir],
strict: [working_dir: :string]
) do
{switches, [component], []} ->
with {:ok, module} <- Map.fetch(@components, String.to_atom(component)) do
working_dir = Keyword.get(switches, :working_dir, File.cwd!())
module.main(working_dir)
else
:error -> usage_message()
end
parsed = OptionParser.parse(argv, aliases: [C: :working_dir], strict: [working_dir: :string])

_ ->
usage_message()
with {switches, [component], []} <- parsed,
Glutexo marked this conversation as resolved.
Show resolved Hide resolved
{:ok, module} <- Map.fetch(@components, String.to_atom(component)) do
working_dir = Keyword.get(switches, :working_dir, File.cwd!())
module.main(working_dir)
else
:error -> usage_message()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would question the order: OptionParser.parse is first in the with clause, followed by Map.fetch. Yet the order In else matches the Map.fetch failure first and then the OptionParser.parse ones.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Glutexo I had same idea today :D :D, yes it just a detail but could be nice

{_, _, [_ | _]} -> usage_message()
nappex marked this conversation as resolved.
Show resolved Hide resolved
{_, argv, _} when length(argv) != 1 -> usage_message()
nappex marked this conversation as resolved.
Show resolved Hide resolved
_ -> usage_message()
Glutexo marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we handle all cases then the not defined may not be catch

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, all cases are covered by the explicit patterns

  • {_, _, [_ | _]} (invalid switches),
  • {_, argv, _} (invalid argument number),
  • and :error (invalid component).

Universal _ would silently swallow any unexpected value. In that case we rather want an error to be raised.

end
end

Expand Down
Loading