web 3d图形渲染器
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.

138 lines
2.3 KiB

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