Skip to content

Add an option validation on cli commands#2278

Merged
Alkarex merged 1 commit intoFreshRSS:devfrom
aledeg:feature/cli-options
Mar 19, 2019
Merged

Add an option validation on cli commands#2278
Alkarex merged 1 commit intoFreshRSS:devfrom
aledeg:feature/cli-options

Conversation

@aledeg
Copy link
Copy Markdown
Member

@aledeg aledeg commented Mar 17, 2019

If an option used on cli is not recognized, the command
aborts and displays an error message.
If the typed option is similar to one of the recognized
options, a hint is displayed.

At the moment, there is a limitation on long options.
Short options are not validated at the moment.

See #2046

Copy link
Copy Markdown
Member

@Frenzie Frenzie left a comment

Choose a reason for hiding this comment

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

Interesting idea, although given the fairly limited number of options I'm not entirely convinced that it's actually more helpful than the usage notice. :-)

cli/_cli.php Outdated
if (0 === count($possibleOption)) {
fail(sprintf('FreshRSS error: unknown option: %s'), $unknownOption);
} else {
fail(sprintf('FreshRSS error: unknown option: %s. Do you mean %s?', $unknownOption, array_shift($possibleOption)));
Copy link
Copy Markdown
Member

@Frenzie Frenzie Mar 17, 2019

Choose a reason for hiding this comment

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

I'd say "Did you mean" or perhaps "The most similar command/option/flag is:".

Side note, what if there are multiple results with a low Levenshtein distance?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Noted.
In the case of multiple results, the first one is displayed

@Alkarex Alkarex added this to the 1.14.0 milestone Mar 17, 2019
@aledeg
Copy link
Copy Markdown
Member Author

aledeg commented Mar 17, 2019

I can remove the the suggestion part. It was just an experiment.
But the validation part is interesting because if one of the options is inexistent, the following one are discarded. In that case, you could end up doing things that you don't want to do.
See related issue for example.

@Frenzie
Copy link
Copy Markdown
Member

Frenzie commented Mar 17, 2019

You could also consider printing both, so you'd get something like this:

Did you mean --bla

Usage notice: --bla --yada

@aledeg
Copy link
Copy Markdown
Member Author

aledeg commented Mar 17, 2019

Considering how the cli is build, it won't be possible to have both

@Frenzie
Copy link
Copy Markdown
Member

Frenzie commented Mar 17, 2019

I think usage info is more useful personally. Any thoughts @Alkarex ?

@aledeg
Copy link
Copy Markdown
Member Author

aledeg commented Mar 17, 2019

I can do that but that means a lot of changes

@Frenzie
Copy link
Copy Markdown
Member

Frenzie commented Mar 17, 2019

I don't think suggestion is very useful though. :-)

(At least not when there are fewer than 10 options.)

@aledeg aledeg force-pushed the feature/cli-options branch from 42152ac to 8de7b2b Compare March 17, 2019 13:27
@aledeg
Copy link
Copy Markdown
Member Author

aledeg commented Mar 17, 2019

@Frenzie I've changed the behavior to display wrong parameters AND the usage. Let me know what you think!

Copy link
Copy Markdown
Member

@Frenzie Frenzie left a comment

Choose a reason for hiding this comment

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

lgtm!

return true;
}

fwrite(STDERR, sprintf("FreshRSS error: unknown options: %s\n", implode (', ', $unknownOptions)));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I suppose you could consider giving fail() an argument regarding whether to die or not (true by default), or create a non-die()ing duplicate like a warning() function. But that might be premature abstraction.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That's a good point. Maybe we should do that if we intend to add more warnings.

@Alkarex
Copy link
Copy Markdown
Member

Alkarex commented Mar 19, 2019

@aledeg Could you please fix the Travis white-space error? :-)

@aledeg
Copy link
Copy Markdown
Member Author

aledeg commented Mar 19, 2019

@Alkarex. Will do!

If an option used on cli is not recognized, the command
aborts and displays an error message.
If the typed option is similar to one of the recognized
options, a hint is displayed.

At the moment, there is a limitation on long options.
Short options are not validated at the moment.

See FreshRSS#2046
@aledeg aledeg force-pushed the feature/cli-options branch from 8de7b2b to fea1119 Compare March 19, 2019 19:11
@aledeg
Copy link
Copy Markdown
Member Author

aledeg commented Mar 19, 2019

@Alkarex it's all good!

@Alkarex Alkarex merged commit 71b4226 into FreshRSS:dev Mar 19, 2019
@aledeg aledeg deleted the feature/cli-options branch March 19, 2019 19:31
@oupala
Copy link
Copy Markdown
Contributor

oupala commented Mar 22, 2019

As far as I have tested this merge request, it fixes the bug mentioned in #2046.

Thanks @aledeg for fixing this.

I still have 2 remarks:

  • it is very strange to get syntax inconsistency in parameter name: --api_password seems not natural as we usually use dash or underscore, but not both of them, we should convert all parameter to dash syntax: --api-password
  • it is also very strange to see that installing FreshRSS on an already installed FreshRSS returns an error, following idempotency we should more return a 0 return code:
>php cli/do-install.php --default_user myuser --language fr --api-enabled --db-type sqlite
FreshRSS looks to be already installed! Please use `./cli/reconfigure.php` instead.
>echo $?
1

mdemoss pushed a commit to mdemoss/FreshRSS that referenced this pull request Mar 25, 2021
If an option used on cli is not recognized, the command
aborts and displays an error message.
If the typed option is similar to one of the recognized
options, a hint is displayed.

At the moment, there is a limitation on long options.
Short options are not validated at the moment.

See FreshRSS#2046
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.

4 participants