-
Notifications
You must be signed in to change notification settings - Fork 1
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
Conversation
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: []) |
There was a problem hiding this comment.
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.
{_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" |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
defp safe_concat(component) do | ||
Module.safe_concat("Onigumo", component) | ||
end |
There was a problem hiding this comment.
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.
defp safe_concat(component) do | |
Module.safe_concat("Onigumo", component) | |
end |
There was a problem hiding this 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.
6dbb778
to
f14d087
Compare
@@ -5,10 +5,10 @@ defmodule Onigumo.CLI do | |||
|
|||
def main(argv) do | |||
try do | |||
{[], [component]} = OptionParser.parse!(argv, strict: []) | |||
{[], [component], []} = OptionParser.parse(argv, strict: []) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the whitespace.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the whitespace.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sweet!
fix #204