npm.io
3.0.0 • Published 1 year ago

shlex

Licence
MIT
Version
3.0.0
Deps
0
Size
21 kB
Vulns
0
Weekly
0
Stars
42

node-shlex

Build Status

node-shlex is a JavaScript module for quoting and parsing shell commands.

The API was inspired by the shlex module from the Python Standard Library. However, the Python implementation is fairly complex, and supports a confusing matrix of modes that is not replicated here. node-shlex always operates in what the Python module calls "POSIX mode."

As of version 2.0.0, Bash's ANSI C strings () and locale-specific translation strings ($"x") are supported. This diverges from the Python shlex behavior but makes parsing more accurate.

Note that node-shlex does not attempt to split on or otherwise parse operators (such as 2>/dev/null) or comments, and it does not perform variable interpolation.

Usage

shlex.join()

import { join } from 'shlex'
join(["ls", "-al", '/'])  // returns: 'ls -al /'
join([ 'rm', '-f', '/Volumes/Macintosh HD' ])  // returns: "rm -f '/Volumes/Macintosh HD'"
shlex.quote()
import { quote } from 'shlex'
quote("abc")      // returns: abc
quote("abc def")  // returns: 'abc def'
quote("can't")    // returns: 'can'"'"'t'
shlex.split()
import { split } from 'shlex'
split('ls -al /')  // returns: [ 'ls', '-al', '/' ]
split('rm -f "/Volumes/Macintosh HD"')  // returns: [ 'rm', '-f', '/Volumes/Macintosh HD' ]
x'
) and locale-specific translation strings (__INLINE_CODE_4__) are supported. This diverges from the Python __INLINE_CODE_5__ behavior but makes parsing more accurate.

Note that __INLINE_CODE_6__ does not attempt to split on or otherwise parse operators (such as __INLINE_CODE_7__) or comments, and it does not perform variable interpolation.

Usage

__INLINE_CODE_8__

import { join } from 'shlex'
join(["ls", "-al", '/'])  // returns: 'ls -al /'
join([ 'rm', '-f', '/Volumes/Macintosh HD' ])  // returns: "rm -f '/Volumes/Macintosh HD'"
__INLINE_CODE_9__
import { quote } from 'shlex'
quote("abc")      // returns: abc
quote("abc def")  // returns: 'abc def'
quote("can't")    // returns: 'can'"'"'t'
__INLINE_CODE_10__
import { split } from 'shlex'
split('ls -al /')  // returns: [ 'ls', '-al', '/' ]
split('rm -f "/Volumes/Macintosh HD"')  // returns: [ 'rm', '-f', '/Volumes/Macintosh HD' ]

Keywords