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

CLI usage message #206

Merged
merged 3 commits into from
Jan 26, 2024
Merged

CLI usage message #206

merged 3 commits into from
Jan 26, 2024

Conversation

nappex
Copy link
Collaborator

@nappex nappex commented Nov 25, 2023

fix #190

@nappex
Copy link
Collaborator Author

nappex commented Nov 25, 2023

  • Fix tests
  • improve dynamic output of components in usage message Enum.each(@components, &Atom.to_string/1)

@nappex
Copy link
Collaborator Author

nappex commented Nov 25, 2023

Comment on lines 2 to 3
use ExUnit.Case
import Mox
import ExUnit.CaptureIO
Copy link
Owner

Choose a reason for hiding this comment

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

Can we group ExUnit.Case and ExUnit.CaptureIO? Not only it belongs together, but ExUnit comes alphabetically before Mox.

Comment on lines 37 to 48
output = capture_io(fn -> Onigumo.CLI.main([]) end)
assert String.starts_with?(output, "Usage: ./onigumo ")
end

test("run CLI with more than one argument") do
assert_raise(MatchError, fn -> Onigumo.CLI.main(["Downloader", "Parser"]) end)
output = capture_io(fn -> Onigumo.CLI.main(["Downloader", "Parser"]) end)
assert String.starts_with?(output, "Usage: ./onigumo ")
end

test("run CLI with invalid switch") do
assert_raise(OptionParser.ParseError, fn -> Onigumo.CLI.main(["--help"]) end)
output = capture_io(fn -> Onigumo.CLI.main(["--help"]) end)
assert String.starts_with?(output, "Usage: ./onigumo ")
Copy link
Owner

Choose a reason for hiding this comment

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

The capture_io and start_with? logic is quite complex and is repeated three times here. Could it be refactored to a private function acctepting just the fn?

lib/cli.ex Outdated
{[], [component]} = OptionParser.parse!(argv, strict: [])
component
rescue
_ -> 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 think that here we shoudl catch OptionParser.ParseError and MatchError. explicitly.

Suggested change
_ -> usage_message()
OptionParser.ParseError -> usage_message()
MatchError -> 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 am thinking: to keep the amount of changes as small as possible, we can first only rescue the OptionParser.ParseError and then, in a separate pull request, extend the rescue for the MatchError.

Providing an invalid switch is actually a different user story from providing an invalid number of positional arguments. 🤷🏻‍♂️

Comment on lines 47 to 48
output = capture_io(fn -> Onigumo.CLI.main(["--help"]) end)
assert String.starts_with?(output, "Usage: ./onigumo ")
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 only rescued OptionParser.ParseError, this would be the only amended test, reducing the amount of changes.

Comment on lines 37 to 43
output = capture_io(fn -> Onigumo.CLI.main([]) end)
assert String.starts_with?(output, "Usage: ./onigumo ")
end

test("run CLI with more than one argument") do
assert_raise(MatchError, fn -> Onigumo.CLI.main(["Downloader", "Parser"]) end)
output = capture_io(fn -> Onigumo.CLI.main(["Downloader", "Parser"]) end)
assert String.starts_with?(output, "Usage: ./onigumo ")
Copy link
Owner

Choose a reason for hiding this comment

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

These two tests for an invalid number of arguments can be amended in a follow-up pull request, adding the MatchError.

lib/cli.ex Outdated
Comment on lines 20 to 23
defp usage_message() do
IO.puts("Usage: ./onigumo [COMPONENT]\n")
IO.puts("\tSimple program that retrieve http web content in structured data.\n")
IO.puts("COMPONENT\tonigumo component to run, availables #{inspect(Map.keys(@components))}")
Copy link
Owner

Choose a reason for hiding this comment

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

Does Elxir support some kind of heredocs?

IO.puts(<<<END)
Usage: ./onigumo [COMPONENT]
END

lib/cli.ex Outdated
Comment on lines 20 to 23
defp usage_message() do
IO.puts("Usage: ./onigumo [COMPONENT]\n")
IO.puts("\tSimple program that retrieve http web content in structured data.\n")
IO.puts("COMPONENT\tonigumo component to run, availables #{inspect(Map.keys(@components))}")
Copy link
Owner

@Glutexo Glutexo Jan 26, 2024

Choose a reason for hiding this comment

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

Do we want to keep inspect? It exposes Elixir data structure that is only to be used for debugging. Maybe rather use Enum.join(", ", Map.keys(@components))?

Moreover, the expression is already pretty complex. It may deserve its own variable.

lib/cli.ex Outdated
defp usage_message() do
valid_components = Enum.join(Map.keys(@components), ", ")
IO.puts("""
Usage: ./onigumo [COMPONENT]
Copy link
Owner

Choose a reason for hiding this comment

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

Maybe the ./onigumo is too assuming in how the script is called.

Suggested change
Usage: ./onigumo [COMPONENT]
Usage: onigumo [COMPONENT]

@nappex nappex marked this pull request as ready for review January 26, 2024 19:59
@nappex nappex force-pushed the cli-help branch 2 times, most recently from 29deed0 to 632a586 Compare January 26, 2024 20:04
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. ❤️

@Glutexo Glutexo merged commit f727206 into Glutexo:master Jan 26, 2024
1 check passed
@nappex nappex deleted the cli-help branch January 26, 2024 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement help/usage message for CLI
2 participants