Skip to content

Usg msg reason #275

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

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open

Usg msg reason #275

wants to merge 14 commits into from

Conversation

nappex
Copy link
Collaborator

@nappex nappex commented Apr 11, 2025

fix #251

@nappex nappex marked this pull request as ready for review April 12, 2025 16:15
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.

Thanks for the pull request! ❤️

Comments marked as ❗️ are blocking. The other ones are more of comments and suggestions.

The two suggested Enum.join(invalid, ", ") occurrences may be DRIED into a function.

  defp join_for_usage_message(args):
    Enum.join(args, ", ")

{_, _, invalid = [_ | _]} ->
Enum.map(invalid, fn {k, _} -> k end)
|> then(&"invalid OPTIONS #{Enum.join(&1, ", ")}")
|> usage_message()

{_, argv, _} when length(argv) != 1 ->
Copy link
Owner

Choose a reason for hiding this comment

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

This guard is probably unnecessary, I‘ll create an issue for that.

Copy link
Owner

Choose a reason for hiding this comment

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

Created: #278

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

OK

assert usage_message_printed?(fn -> Onigumo.CLI.main(unquote(combination)) end)
assert usage_message_printed?(
fn -> Onigumo.CLI.main(unquote(combination)) end,
# there is no easy way to do it dynamically
Copy link
Owner

Choose a reason for hiding this comment

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

Yes, this sounds fishy.

Copy link
Owner

Choose a reason for hiding this comment

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

The constant can be amended to contain the expected output.

  @invalid_combinations [
    {["--help", "-C", ".", "downloader"], ["--help"],
    {["downloader", "-h"], ["--help"]},
    {["downloader", "--help"], ["--help"]},
  ]

--help is the only incompatible option though. That means the second element is unnecessary.

We can however omit the comment: there is an easy way to do it, it’s just not necessary.

Suggested change
# there is no easy way to do it dynamically
Suggested change
# there is no easy way to do it dynamically
# The incompatible argument is always --help.

Another thing came to my mind, however. It’s how we always call help to be the the incompatible one and never --working-dir. It makes sense to some extent: a component defines a set of valid switches containing --working-dir and not --help. Thus, it’s --help that’s incompatible. But it may become more complicated if the CLI becomes more convoluted.

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.

I pick Request changes for only two reasons:

  • The # there is no easy way to do it dynamically comment doesn’t feel appropriate. There is and easy way. Either do it, change the comment to be more descriptive, or omit it at all.
  • The @invalid_switches extension deserves a preliminary PR that would unblock this one.

lib/cli.ex Outdated

_ ->
OptionParser.to_argv(switches)
|> then(&"incompatible OPTIONS #{Enum.join(&1, ", ")}")
Copy link
Owner

Choose a reason for hiding this comment

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

Clever! I like the conciseness of the &-function returning directly a string.

The expression is maybe too complex. What about extracting the Enum.join/2 call?

Suggested change
|> then(&"incompatible OPTIONS #{Enum.join(&1, ", ")}")
|> Enum.join(", ")
|> then(&"incompatible OPTIONS #{&1}")

lib/cli.ex Outdated
usage_message()
{_, _, invalid = [_ | _]} ->
Enum.map(invalid, &elem(&1, 0))
|> then(&"invalid OPTIONS #{Enum.join(&1, ", ")}")
Copy link
Owner

Choose a reason for hiding this comment

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

Let’s dissect this too.

Suggested change
|> then(&"invalid OPTIONS #{Enum.join(&1, ", ")}")
|> Enum.join(", ")
|> then(&"invalid OPTIONS #{&1}")

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Alrigth why not, it looks like more clear and legibility, thank you.

{_, _, invalid = [_ | _]} ->
Enum.map(invalid, fn {k, _} -> k end)
|> then(&"invalid OPTIONS #{Enum.join(&1, ", ")}")
|> usage_message()

{_, argv, _} when length(argv) != 1 ->
Copy link
Owner

Choose a reason for hiding this comment

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

Created: #278

IO.write("""
onigumo: invalid usage
onigumo: invalid usage – #{reason}
Copy link
Owner

Choose a reason for hiding this comment

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

– ❤️

assert usage_message_printed?(fn -> Onigumo.CLI.main(unquote(combination)) end)
assert usage_message_printed?(
fn -> Onigumo.CLI.main(unquote(combination)) end,
# there is no easy way to do it dynamically
Copy link
Owner

Choose a reason for hiding this comment

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

The constant can be amended to contain the expected output.

  @invalid_combinations [
    {["--help", "-C", ".", "downloader"], ["--help"],
    {["downloader", "-h"], ["--help"]},
    {["downloader", "--help"], ["--help"]},
  ]

--help is the only incompatible option though. That means the second element is unnecessary.

We can however omit the comment: there is an easy way to do it, it’s just not necessary.

Suggested change
# there is no easy way to do it dynamically
Suggested change
# there is no easy way to do it dynamically
# The incompatible argument is always --help.

Another thing came to my mind, however. It’s how we always call help to be the the incompatible one and never --working-dir. It makes sense to some extent: a component defines a set of valid switches containing --working-dir and not --help. Thus, it’s --help that’s incompatible. But it may become more complicated if the CLI becomes more convoluted.

assert usage_message_printed?(
fn -> Onigumo.CLI.main(unquote(switch)) end,
"invalid OPTIONS #{Enum.join(unquote(switch), ", ")}"
)
end
end

for combination <- @invalid_combinations do
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
for combination <- @invalid_combinations do
for {combination, invalid} <- @invalid_combinations do

If the constant contained tuples as imagined below, the parametrization would look like this.

Copy link
Collaborator Author

@nappex nappex Apr 27, 2025

Choose a reason for hiding this comment

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

I am not sure if it is just one value, it feels like unnecessary DRY at this moment.
Ok, it is not so difficult to dynamically solve this problem so, the comment is redundant and should be deleted.

assert usage_message_printed?(
fn -> Onigumo.CLI.main(unquote(combination)) end,
# there is no easy way to do it dynamically
"incompatible OPTIONS --help"
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
"incompatible OPTIONS --help"
"incompatible OPTIONS #{invalid}"

If the constant contained tuples as imagined above, the string would look like this.

output = capture_io(function)
String.starts_with?(output, "onigumo: invalid usage")
String.starts_with?(output, "onigumo: invalid usage – #{reason}")
Copy link
Owner

Choose a reason for hiding this comment

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

Just thinking out loud.

Suggested change
String.starts_with?(output, "onigumo: invalid usage – #{reason}")
message = "onigumo: invalid usage – #{reason}"
String.starts_with?(output, message)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes it is more clean suppose

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

but then it is for another PR since the same way of solving is for help message

"-c"
["--invalid"],
["-c"],
["-a", "-b"]
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 not sure about this. It adds the possibility to test joining for the usage message. But this test should have been there in the first place. Or at least list of invalid switches instead of a single value. I feel it doesn’t belong into this PR.

However, without the list support and this test in place, it is not possible to test the new feature.

Copy link
Collaborator Author

@nappex nappex Apr 27, 2025

Choose a reason for hiding this comment

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

I would keep it maybe in different format, but you have to test a new feature in this PR

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.

Add option to usage_message to write certain reason of problem to output
2 participants