Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I parse a string into appropriate arguments for child_process.spawn?

I want to be able to take a command string, for example:

some/script --option="Quoted Option" -d --another-option 'Quoted Argument'

And parse it into something that I can send to child_process.spawn:

spawn("some/script", ["--option=\"Quoted Option\"", "-d", "--another-option", "Quoted Argument"])

All of the parsing libraries I've found (e.g. minimist, etc.) do too much here by parsing it into some kind of options object, etc. I basically want the equivalent of whatever Node does to create process.argv in the first place.

This seems like a frustrating hole in the native APIs since exec takes a string, but doesn't execute as safely as spawn. Right now I'm hacking around this by using:

spawn("/bin/sh", ["-c", commandString])

However, I don't want this to be tied to UNIX so strongly (ideally it'd work on Windows too). Halp?

like image 382
Michael Bleigh Avatar asked May 06 '14 06:05

Michael Bleigh


People also ask

How do I process arguments passed on to a script?

Using sys.argv This is a inbuilt module sys.argv can process arguments that are passed on with the script. By default the first argument which is considered at sys.argv is the file name. the rest of the arguments are indexed as 1,2 and so on. In the below example we see how a script uses the arguments passed onto it.

What is argument parsing in Python?

Argument Parsing in Python Python Server Side Programming Programming Every programming language has a feature to create scripts and run them from the terminal or being called by other programs. When running such scripts we often need to pass on arguments needed by the script for various functions to be executed inside the script.

How do I look at each potential argument before a string?

This code sample creates a function called getOptions and tells Python to look at each potential argument preceded by some recognizable string (such as --input or -i ).

How to parse the arguments passed to the script in Linux?

We can use the getopts program/ command to parse the arguments passed to the script in the command line/ terminal by using loops and switch-case statements. Using getopts, we can assign the positional arguments/ parameters from the command line to the bash variables directly. This allows us to manage the parameters nicely and in a systematic way.


2 Answers

Standard Method (no library)

You don't have to parse the command string into arguments, there's an option on child_process.spawn named shell.

options.shell

If true, runs command inside of a shell.
Uses /bin/sh on UNIX, and cmd.exe on Windows.

Example:

let command = `some_script --option="Quoted Option" -d --another-option 'Quoted Argument'`

let process = child_process.spawn(command, [], { shell: true })  // use `shell` option

process.stdout.on('data', (data) => {
  console.log(data)
})

process.stderr.on('data', (data) => {
  console.log(data)
})

process.on('close', (code) => {
  console.log(code)
})
like image 51
chuyik Avatar answered Oct 24 '22 16:10

chuyik


The minimist-string package might be just what you're looking for.

Here's some sample code that parses your sample string -

const ms = require('minimist-string')
const sampleString = 'some/script --option="Quoted Option" -d --another-option \'Quoted Argument\'';
const args = ms(sampleString);
console.dir(args)

This piece of code outputs this -

{
  _: [ 'some/script' ],
  option: 'Quoted Option',
  d: true,
  'another-option': 'Quoted Argument'
}
like image 37
GPX Avatar answered Oct 24 '22 16:10

GPX