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

How to trim quotation for argv #13

Closed
axetroy opened this issue Nov 23, 2022 · 7 comments
Closed

How to trim quotation for argv #13

axetroy opened this issue Nov 23, 2022 · 7 comments

Comments

@axetroy
Copy link

axetroy commented Nov 23, 2022

node script/build.js --foo="bar"

// build.js
const argv = require('minimist')(process.argv.slice(2))

// actuly
console.log(argv.foo === '"bar"') // true
// expect
console.log(argv.foo === 'bar') // false
@shadowspawn
Copy link
Collaborator

shadowspawn commented Nov 23, 2022

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

@shadowspawn
Copy link
Collaborator

(minimist does not have any extra processing for removing quotes from arguments or option values.)

@ljharb
Copy link
Member

ljharb commented Nov 24, 2022

"how the program is called" is in the OP, but the real question is, what shell/OS are you using?

@axetroy
Copy link
Author

axetroy commented Nov 24, 2022

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)

Run the program

$ go run main.go
true       
false      
value "bar"

Note

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)

process.argv

[
  'C:\\Program Files\\nodejs\\node.exe',
  'C:\\Users\\Admin\\test.js',
  '--foo="bar"'
]

@ljharb
Copy link
Member

ljharb commented Nov 24, 2022

That still suggests a bug in go’s shell exec stuff to me; yargs may have worked around it tho.

@shadowspawn
Copy link
Collaborator

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.

@ljharb
Copy link
Member

ljharb commented Nov 24, 2022

Closing, but we can certainly reopen if there's something actionable for minimist.

@ljharb ljharb closed this as not planned Won't fix, can't repro, duplicate, stale Nov 24, 2022
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

No branches or pull requests

3 participants