-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Conversation
This change will increase the build size from 9.51 MB to 9.52 MB, an increase of 2.01 KB (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" |
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.
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.
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.
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('-')); |
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.
Are there unit tests covering all this stuff?
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.
Yup :)
@arcanis give your final blessing and we can merge! |
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 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]}`); |
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.
Have you considered writing JSON instead and using .toMatchObject
?
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.
Can you elaborate? I haven't considered that and don't understand what exactly you're proposing but it sounds interesting :)
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.
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).
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.
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('-')); |
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 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
.
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.
Dang, good catch! Will fix 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.
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]}`); |
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.
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('-')); |
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.
Dang, good catch! Will fix this.
**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.
Summary
Fixes #4383. This patch makes argument parsing a bit tidier, and
starts supporting
yarn --silent custom-script
style commands asinitiall intended by #4152.
Test plan
Existing unit tests. Should ideally add a few more.