Closed axetroy closed 1 year ago
Normally the quotes get removed as part of the shell processing and do not appear in process.argv
. How are you calling your program?
I see this with zsh as the shell.
// build.js
console.log(process.argv[2])
const argv = require('minimist')(process.argv.slice(2))
console.log(argv.foo)
% node build.js --foo=abc
--foo=abc
abc
% node build.js --foo="abc"
--foo=abc
abc
% echo --foo="abc"
--foo=abc
(minimist
does not have any extra processing for removing quotes from arguments or option values.)
"how the program is called" is in the OP, but the real question is, what shell/OS are you using?
HI all and thanks for your response.
It really doesn't run in a normal shell environment.
Here is the reproduction code:
// main.go
// you can write it with your own nodejs code
package main
import (
"context"
"os"
"os/exec"
)
func main() {
cmd := exec.CommandContext(context.Background(), "C:\\Windows\\System32\\cmd.exe", "--%", "/c", `node test.js --foo="bar"`)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
if err := cmd.Start(); err != nil {
panic(err)
}
cmd.Wait()
}
// test.js
const argv = require('minimist')(process.argv.slice(2))
console.log(argv)
// actuly
console.log(argv.foo === '"bar"') // true
// expect
console.log(argv.foo === 'bar') // false
console.log('value', argv.foo)
$ go run main.go
true
false
value "bar"
Before use minimist, I use the yargs
to parse command line, and it works fine for me
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')
const argv = yargs(hideBin(process.argv)).argv
// actuly
console.log(argv.foo === '"bar"') // false
// expect
console.log(argv.foo === 'bar') // true
console.log('value', argv.foo)
[
'C:\\Program Files\\nodejs\\node.exe',
'C:\\Users\\Admin\\test.js',
'--foo="bar"'
]
That still suggests a bug in go’s shell exec stuff to me; yargs may have worked around it tho.
Yargs does have some extra handling for quotes which might be why it worked.
I was able to reproduce the quoted argument problem on the command-line with cmd
, but not with double quotes! Double quotes worked fine and did not reach node. I saw the issue with single quotes: --foo='bar'
. PowerShell was fine with both.
So I also think your issue is introduced with the go
call. Is there a way of executing the command by passing an array of arguments instead of a single string with the command, so you don't need to put quotes around the value? This avoids needing to work around how the exec
call splits up the command string and is hence often a better way of passing arguments if there is a choice.
e.g. node test.js --foo="bar"
vs ['node', 'test.js', '--foo=bar']
In summary, not looking like a problem with minimist.
Closing, but we can certainly reopen if there's something actionable for minimist.
node script/build.js --foo="bar"