-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: master
Are you sure you want to change the base?
Usg msg reason #275
Conversation
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.
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, ", ")
Co-authored-by: Glutexo <[email protected]>
{_, _, invalid = [_ | _]} -> | ||
Enum.map(invalid, fn {k, _} -> k end) | ||
|> then(&"invalid OPTIONS #{Enum.join(&1, ", ")}") | ||
|> usage_message() | ||
|
||
{_, argv, _} when length(argv) != 1 -> |
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 guard is probably unnecessary, I‘ll create an issue for that.
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.
Created: #278
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.
OK
Co-authored-by: Glutexo <[email protected]>
test/onigumo_cli_test.exs
Outdated
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 |
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.
Yes, this sounds fishy.
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.
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.
# there is no easy way to do it dynamically |
# 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.
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.
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, ", ")}") |
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.
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?
|> 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, ", ")}") |
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.
Let’s dissect this too.
|> then(&"invalid OPTIONS #{Enum.join(&1, ", ")}") | |
|> Enum.join(", ") | |
|> then(&"invalid OPTIONS #{&1}") |
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.
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 -> |
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.
Created: #278
IO.write(""" | ||
onigumo: invalid usage | ||
onigumo: invalid usage – #{reason} |
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.
– ❤️
test/onigumo_cli_test.exs
Outdated
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 |
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.
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.
# there is no easy way to do it dynamically |
# 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 |
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.
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.
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.
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" |
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.
"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}") |
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 thinking out loud.
String.starts_with?(output, "onigumo: invalid usage – #{reason}") | |
message = "onigumo: invalid usage – #{reason}" | |
String.starts_with?(output, message) |
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.
yes it is more clean suppose
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.
but then it is for another PR since the same way of solving is for help message
"-c" | ||
["--invalid"], | ||
["-c"], | ||
["-a", "-b"] |
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.
🤔 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.
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.
I would keep it maybe in different format, but you have to test a new feature in this PR
fix #251