Skip to content

Fix: fix incorrect argument parsing #4384

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

Merged
merged 10 commits into from
Sep 12, 2017
Merged

Fix: fix incorrect argument parsing #4384

merged 10 commits into from
Sep 12, 2017

Conversation

BYK
Copy link
Member

@BYK BYK commented Sep 10, 2017

Summary

Fixes #4383. This patch makes argument parsing a bit tidier, and
starts supporting yarn --silent custom-script style commands as
initiall intended by #4152.

Test plan

Existing unit tests. Should ideally add a few more.

**Summary**

Fixes #4383. This patch makes argument parsing a bit tidier, and
starts supporting `yarn --silent custom-script` style commands as
initiall intended by #4152.

**Test plan**

Existing unit tests.
@BYK BYK requested a review from arcanis September 10, 2017 17:54
@buildsize
Copy link

buildsize bot commented Sep 10, 2017

This change will increase the build size from 9.51 MB to 9.52 MB, an increase of 2.01 KB (0%)

File name Previous Size New Size Change
yarn-[version].noarch.rpm 819.63 KB 819.8 KB 181 bytes (0%)
yarn-[version].js 3.62 MB 3.62 MB 668 bytes (0%)
yarn-legacy-[version].js 3.67 MB 3.67 MB 801 bytes (0%)
yarn-v[version].tar.gz 823.76 KB 823.96 KB 207 bytes (0%)
yarn_[version]all.deb 626.48 KB 626.68 KB 206 bytes (0%)

@@ -3,6 +3,6 @@
"version": "1.0.0",
"license": "UNLICENSED",
"scripts": {
"custom-script": "print() { echo \"A message from custom script with args \"$@\"\"; }; print "
"custom-script": "node echo.js"
Copy link
Member

Choose a reason for hiding this comment

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

Could use

"custom-script": "node -e 'console.log(`A message from custom script with args ${process.argv[process.argv.length - 1]}`)'"

To avoid the need to have a separate .js file.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's what I did at first (I actually used -p) but Node 4 rejects additional arguments after the passed in script block. I used -- to make it ignore but this was only added in Node 7 or 6 so it failed on Node 4 builds, hence this current, "ugly" solution :(

src/cli/index.js Outdated
if (commandName === '--help' || commandName === '-h') {
commandName = 'help';
// get command name
const firstNonFlagIndex = args.findIndex(arg => !arg.startsWith('-'));
Copy link
Member

Choose a reason for hiding this comment

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

Are there unit tests covering all this stuff?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup :)

@BYK
Copy link
Member Author

BYK commented Sep 11, 2017

@arcanis give your final blessing and we can merge!

Copy link
Member

@arcanis arcanis left a comment

Choose a reason for hiding this comment

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

This commit does a lot of things - I'm fine with it, but I think the next one we do on the CLI will have to be about switching to another CLI library, because otherwise it will become even harder to make the call later on.

@@ -0,0 +1 @@
console.log(`A message from custom script with args ${process.argv[process.argv.length - 1]}`);
Copy link
Member

Choose a reason for hiding this comment

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

Have you considered writing JSON instead and using .toMatchObject?

Copy link
Member Author

Choose a reason for hiding this comment

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

Can you elaborate? I haven't considered that and don't understand what exactly you're proposing but it sounds interesting :)

Copy link
Member

Choose a reason for hiding this comment

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

You could

console.log(JSON.stringify(process.argv))

Then in the test:

expect(JSON.parse(stdout)).toEqual([ '--help' ]);

I think it would be semantically more sound because we wouldn't test the rest of the message on top of the args (A message from custom script etc).

Copy link
Member Author

Choose a reason for hiding this comment

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

Like it. Done!

src/cli/index.js Outdated
if (commandName === '--help' || commandName === '-h') {
commandName = 'help';
// get command name
const firstNonFlagIndex = args.findIndex(arg => !arg.startsWith('-'));
Copy link
Member

@arcanis arcanis Sep 11, 2017

Choose a reason for hiding this comment

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

The problem with this approach is that it falls flat with options that expect arguments (like --mutex) :(

Have you tried running yarn --mutex network? If I'm reading this correctly, firstNonFlagIndex will point to network.

Copy link
Member Author

Choose a reason for hiding this comment

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

Dang, good catch! Will fix this.

Copy link
Member Author

@BYK BYK left a comment

Choose a reason for hiding this comment

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

This commit does a lot of things

Ehh. I mean I tried to fix it in a simpler way which broke a bunch of other things so I had to do this larger change.

I'm fine with it, but I think the next one we do on the CLI will have to be about switching to another CLI library, because otherwise it will become even harder to make the call later on.

Exactly what I was thinking. I actually decided to write a yet another argument parser (not being sarcastic!) after dealing with this craziness 😁

@@ -0,0 +1 @@
console.log(`A message from custom script with args ${process.argv[process.argv.length - 1]}`);
Copy link
Member Author

Choose a reason for hiding this comment

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

Can you elaborate? I haven't considered that and don't understand what exactly you're proposing but it sounds interesting :)

src/cli/index.js Outdated
if (commandName === '--help' || commandName === '-h') {
commandName = 'help';
// get command name
const firstNonFlagIndex = args.findIndex(arg => !arg.startsWith('-'));
Copy link
Member Author

Choose a reason for hiding this comment

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

Dang, good catch! Will fix this.

@arcanis
Copy link
Member

arcanis commented Sep 12, 2017

Exactly what I was thinking. I actually decided to write a yet another argument parser (not being sarcastic!) after dealing with this craziness 😁

Why not improve concierge instead? There's even (a few) tests! :D

@BYK BYK merged commit cb3c7fe into master Sep 12, 2017
@BYK BYK deleted the sane-parsing branch September 12, 2017 12:41
@BYK BYK added this to the 1.0.2 milestone Sep 12, 2017
joaolucasl pushed a commit to joaolucasl/yarn that referenced this pull request Oct 27, 2017
**Summary**

Fixes yarnpkg#4383. This patch makes argument parsing a bit tidier, and
starts supporting `yarn --silent custom-script` style commands as
initiall intended by yarnpkg#4152.

**Test plan**

Existing unit tests. Should ideally add a few more.
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.

3 participants