You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

readme.markdown 2.5 KiB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # shell-quote
  2. Parse and quote shell commands.
  3. [![build status](https://secure.travis-ci.org/substack/node-shell-quote.png)](http://travis-ci.org/substack/node-shell-quote)
  4. [![browser support](https://ci.testling.com/substack/node-shell-quote.png)](https://ci.testling.com/substack/node-shell-quote)
  5. # example
  6. ## quote
  7. ``` js
  8. var quote = require('shell-quote').quote;
  9. var s = quote([ 'a', 'b c d', '$f', '"g"' ]);
  10. console.log(s);
  11. ```
  12. output
  13. ```
  14. a 'b c d' \$f '"g"'
  15. ```
  16. ## parse
  17. ``` js
  18. var parse = require('shell-quote').parse;
  19. var xs = parse('a "b c" \\$def \'it\\\'s great\'');
  20. console.dir(xs);
  21. ```
  22. output
  23. ```
  24. [ 'a', 'b c', '\\$def', 'it\'s great' ]
  25. ```
  26. ## parse with an environment variable
  27. ``` js
  28. var parse = require('shell-quote').parse;
  29. var xs = parse('beep --boop="$PWD"', { PWD: '/home/robot' });
  30. console.dir(xs);
  31. ```
  32. output
  33. ```
  34. [ 'beep', '--boop=/home/robot' ]
  35. ```
  36. ## parse with custom escape charcter
  37. ``` js
  38. var parse = require('shell-quote').parse;
  39. var xs = parse('beep --boop="$PWD"', { PWD: '/home/robot' }, { escape: '^' });
  40. console.dir(xs);
  41. ```
  42. output
  43. ```
  44. [ 'beep', '--boop=/home/robot' ]
  45. ```
  46. ## parsing shell operators
  47. ``` js
  48. var parse = require('shell-quote').parse;
  49. var xs = parse('beep || boop > /byte');
  50. console.dir(xs);
  51. ```
  52. output:
  53. ```
  54. [ 'beep', { op: '||' }, 'boop', { op: '>' }, '/byte' ]
  55. ```
  56. ## parsing shell comment
  57. ``` js
  58. var parse = require('shell-quote').parse;
  59. var xs = parse('beep > boop # > kaboom');
  60. console.dir(xs);
  61. ```
  62. output:
  63. ```
  64. [ 'beep', { op: '>' }, 'boop', { comment: '> kaboom' } ]
  65. ```
  66. # methods
  67. ``` js
  68. var quote = require('shell-quote').quote;
  69. var parse = require('shell-quote').parse;
  70. ```
  71. ## quote(args)
  72. Return a quoted string for the array `args` suitable for using in shell
  73. commands.
  74. ## parse(cmd, env={})
  75. Return an array of arguments from the quoted string `cmd`.
  76. Interpolate embedded bash-style `$VARNAME` and `${VARNAME}` variables with
  77. the `env` object which like bash will replace undefined variables with `""`.
  78. `env` is usually an object but it can also be a function to perform lookups.
  79. When `env(key)` returns a string, its result will be output just like `env[key]`
  80. would. When `env(key)` returns an object, it will be inserted into the result
  81. array like the operator objects.
  82. When a bash operator is encountered, the element in the array with be an object
  83. with an `"op"` key set to the operator string. For example:
  84. ```
  85. 'beep || boop > /byte'
  86. ```
  87. parses as:
  88. ```
  89. [ 'beep', { op: '||' }, 'boop', { op: '>' }, '/byte' ]
  90. ```
  91. # install
  92. With [npm](http://npmjs.org) do:
  93. ```
  94. npm install shell-quote
  95. ```
  96. # license
  97. MIT