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

Use OptionParser.parse #193

Merged
merged 3 commits into from
Feb 24, 2024
Merged

Conversation

nappex
Copy link
Collaborator

@nappex nappex commented Oct 7, 2023

fix #204

nappex added 2 commits October 3, 2023 10:36
   we have to specify option strict: with empty list,
   otherwise we get an error from elixir:
      "warning: not passing the :switches or :strict option to OptionParser is deprecate"
   parse component as first position argument from CLI.
   Rest of positional arguments are unused. This goal is
   achieved by pattern matching of private func get_component.
   If no positional argument is specified then "Downloader"
   must be used.
lib/cli.ex Outdated
def main([component]) do
module = Module.safe_concat("Onigumo", component)
def main(argv) do
{_parsed, args, _invalid} = OptionParser.parse(argv, strict: [])
Copy link
Owner

Choose a reason for hiding this comment

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

If we match the invalid arguments with [], we‘ll get a MatchError. Like that, we can leverage the current rescue clause catching for OptionParser.ParseError and MatchError. It’s not something we want to keep as we want to abandon raising exceptions (see #204), but it can be a nice step that keeps a minimal amount of changes.

The same applies for the parsed argument, which we already do in the codebase. They both would raise a MatchError and we currently don’t need to differentiate between them.

Suggested change
{_parsed, args, _invalid} = OptionParser.parse(argv, strict: [])
{[], args, []} = OptionParser.parse(argv, strict: [])

lib/cli.ex Outdated
root_path = File.cwd!()
module.main(root_path)
end

defp get_component([]), do: "Downloader"
Copy link
Owner

Choose a reason for hiding this comment

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

This adds additional logic of setting a default component. That’s not necessarily wrong, at least now when there is only a single component, but it’s over the scope of introducing OptionParser.

lib/cli.ex Outdated
module =
args
|> get_component
|> safe_concat
Copy link
Owner

Choose a reason for hiding this comment

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

After a productive discussion, we abandoned safe_concat in favor of a module literal.

lib/cli.ex Outdated
Comment on lines 17 to 19
defp safe_concat(component) do
Module.safe_concat("Onigumo", component)
end
Copy link
Owner

Choose a reason for hiding this comment

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

As mentioned above. Thanks to abandoning safe_concat as unsafe 😃, exposing new errors and abusing reflection, we won’t need this function altogether.

Suggested change
defp safe_concat(component) do
Module.safe_concat("Onigumo", component)
end

Copy link
Owner

@Glutexo Glutexo left a comment

Choose a reason for hiding this comment

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

Also, there are no tests. But I know this is only a draft.

@nappex nappex force-pushed the use-optionParser.parse branch from 6dbb778 to f14d087 Compare February 24, 2024 18:42
lib/cli.ex Outdated Show resolved Hide resolved
@@ -5,10 +5,10 @@ defmodule Onigumo.CLI do

def main(argv) do
try do
{[], [component]} = OptionParser.parse!(argv, strict: [])
{[], [component], []} = OptionParser.parse(argv, strict: [])
Copy link
Owner

Choose a reason for hiding this comment

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

Just mentioning that in this case, there are now two different MatchErrors that can occur:

  • An invalid switch is provided, causing the third assignment [] to not match.
  • More than one positional argument causing the secund assignment [component] to fail.

We don’t, however, need to differentiate these two, as both mean invalid CLI usage.

@nappex nappex marked this pull request as ready for review February 24, 2024 18:54
Copy link
Owner

@Glutexo Glutexo left a comment

Choose a reason for hiding this comment

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

Fix the whitespace.

Copy link
Owner

@Glutexo Glutexo left a comment

Choose a reason for hiding this comment

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

Fix the whitespace.

Copy link
Owner

@Glutexo Glutexo left a comment

Choose a reason for hiding this comment

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

Sweet!

@nappex nappex merged commit 66cee63 into Glutexo:master Feb 24, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use functions with trailing bang(!) or without?
2 participants