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

Env: Fixed run Argument Passing #41179

Merged
merged 3 commits into from
May 23, 2022
Merged

Env: Fixed run Argument Passing #41179

merged 3 commits into from
May 23, 2022

Conversation

ObliviousHarmony
Copy link
Contributor

What?

As it stands, you need to pass options to wp-env run with the entire string having been quoted. This is because of yargs/yargs consuming any options even if they aren't defined. This causes problems when trying to encapsulate scripts in package.json files since you can't easily inject more options into the quoted string in the script.

Closes #32929.

Why?

This will make it easier to write wp-env run commands in package.json scripts that can accept more options. Something like wp-env run tests-wordpress ls -la will now work without being quoted. With this pull request, you can now pass options.

How?

This works by enabling the unknown-options-as-args parser option. This results in any unknown options that are given being treated as a positional argument. In the case of a variadic argument like [command...], this means we can write commands conveniently.

As a note, --help and --version cannot be passed since they are known options. This will also result in other commands failing to execute if unknown flags are passed inappropriately in place of positional arguments.

Testing Instructions

  1. Run npm run wp-env -- run ls -la
  2. With this pull request the command will execute as expected. Without it, there will be no -la option.3.

By enabling the `unknown-options-as-args` parser
option, any options passed to the command that
are not registered will be given to the command
as arguments. This lets us pass them to the
container without quoting them.

Note: --help and --version can't be passed since they
aren't unknown. This is probably fine though and we
can handle that using quotes when necessary.
@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label May 20, 2022
@github-actions
Copy link

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @ObliviousHarmony! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@Mamaduka Mamaduka added [Type] Bug An existing feature does not function as intended [Package] Env /packages/env labels May 20, 2022
@gziolo gziolo changed the title Fixed run Argument Passing Env: Fixed run Argument Passing May 20, 2022
Copy link
Member

@noahtallen noahtallen left a comment

Choose a reason for hiding this comment

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

Love it, very simple way to solve the problem! And quotes still work if the argument matches a wp-env argument as a fallback.

I think we just need to update the README file. For example, we have this notice about needing quotation marks in some cases:

<div class="callout callout-alert">
To run a WP-CLI command that includes optional arguments, enclose the WP-CLI command in quotation marks; otherwise, the optional arguments are ignored. This is because flags are normally passed to `wp-env` itself, meaning that the flags are not considered part of the argument that specifies the WP-CLI command. With quotation marks, `wp-env` considers everything inside quotation marks the WP-CLI command argument.
For example, to list cron schedules with optional arguments that specify the fields returned and the format of the output:
```sh
wp-env run cli "wp cron schedule list --fields=name --format=csv"
```
Without the quotation marks, WP-CLI lists the schedule in its default format, ignoring the `fields` and `format` arguments.
</div>
, so we can probably remove that and note that quotation marks can be used as a fallback if wp-env consumes an arg instead of passing it in.

Edit: oh and we should also note it as an enhancement in the changelog :)

Since we no longer require the quotes, this
commit updates the documentation to
explain what cases they are required to
be used. This will keep it from being a
surprise if a command does not behave as
expected.
@ObliviousHarmony
Copy link
Contributor Author

Thanks, @noahtallen,

I've added a changelog and updated the documentation.

@noahtallen noahtallen merged commit 61c5e1f into WordPress:trunk May 23, 2022
@github-actions
Copy link

Congratulations on your first merged pull request, @ObliviousHarmony! We'd like to credit you for your contribution in the post announcing the next WordPress release, but we can't find a WordPress.org profile associated with your GitHub account. When you have a moment, visit the following URL and click "link your GitHub account" under "GitHub Username" to link your accounts:

https://profiles.wordpress.org/me/profile/edit/

And if you don't have a WordPress.org account, you can create one on this page:

https://login.wordpress.org/register

Kudos!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Package] Env /packages/env [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

wp-env: run cli command misses the options passed with hyphens
3 participants