From 64e65b27d312f57e51e0c7c0685d3f4b26a3db48 Mon Sep 17 00:00:00 2001 From: anseki Date: Sat, 25 Apr 2015 18:15:35 +0900 Subject: [PATCH] Change: error message includes extMessage --- README.md | 476 +++++++++++++++++++++---------------------- lib/readline-sync.js | 15 +- package.json | 2 +- 3 files changed, 247 insertions(+), 246 deletions(-) diff --git a/README.md b/README.md index 5ab9779..f564627 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Synchronous [Readline](http://nodejs.org/api/readline.html) for interactively ru readlineSync tries to let your script have a conversation with the user via a console, even when the input/output stream is redirected like `your-script bar.log`. - +
OptionsUtility MethodsPlaceholders
Basic OptionsUtility MethodsPlaceholders
* Some simple cases: @@ -92,24 +92,24 @@ console.log('\nA value the user requested: ' + value); A value the user requested: 51 ``` -## Installation +## Installation  ```shell npm install readline-sync ``` -## Basic Methods +## Basic Methods  -These are used to control details of the behavior. It is recommended to use the [Utility Methods](#utility-methods) instead of Basic Methods if it satisfy your request. +These are used to control details of the behavior. It is recommended to use the [Utility Methods](#utility_methods) instead of Basic Methods if it satisfy your request. -### `question` +### `question`  ```js answer = readlineSync.question([query[, options]]) ``` Display a `query` to the user if it's specified, and then return the input from the user after it has been typed and an Enter key was pressed. -You can specify an `options` (see [Options](#options)) to control the behavior (e.g. refusing unexpected input, avoiding trimming white spaces, etc.). **If you let the user input the secret text (e.g. password), you should consider [`hideEchoBack`](#hideechoback) option.** +You can specify an `options` (see [Basic Options](#basic_options)) to control the behavior (e.g. refusing unexpected input, avoiding trimming white spaces, etc.). **If you let the user input the secret text (e.g. password), you should consider [`hideEchoBack`](#basic_options-hideechoback) option.** The `query` may be string, or may not be (e.g. number, Date, Object, etc.). It is converted to string (i.e. `toString` method is called) before it is displayed. It can include the [placeholders](#placeholders). @@ -122,14 +122,14 @@ program = readlineSync.question('Which program starts do you want? :', { }); ``` -### `prompt` +### `prompt`  ```js input = readlineSync.prompt([options]) ``` -Display a prompt-sign (see [`prompt`](#options%3eprompt) option) to the user, and then return the input from the user after it has been typed and an Enter key was pressed. -You can specify an `options` (see [Options](#options)) to control the behavior (e.g. refusing unexpected input, avoiding trimming white spaces, etc.). +Display a prompt-sign (see [`prompt`](#basic_options-prompt) option) to the user, and then return the input from the user after it has been typed and an Enter key was pressed. +You can specify an `options` (see [Basic Options](#basic_options)) to control the behavior (e.g. refusing unexpected input, avoiding trimming white spaces, etc.). For example: @@ -140,16 +140,16 @@ while (true) { } ``` -### `keyIn` +### `keyIn`  ```js pressedKey = readlineSync.keyIn([query[, options]]) ``` Display a `query` to the user if it's specified, and then return a character as a key immediately it was pressed by the user, **without pressing an Enter key**. Note that the user has no chance to change the input. -You can specify an `options` (see [Options](#options)) to control the behavior (e.g. ignoring keys except some keys, checking target key, etc.). +You can specify an `options` (see [Basic Options](#basic_options)) to control the behavior (e.g. ignoring keys except some keys, checking target key, etc.). -The `query` is handled the same as that of the [`question`](#question) method. +The `query` is handled the same as that of the [`question`](#basic_methods-question) method. For example: @@ -157,18 +157,18 @@ For example: menuId = readlineSync.keyIn('Hit 1...5 key :', {limit: '${1-5}'}); ``` -### `setDefaultOptions` +### `setDefaultOptions`  ```js currentDefaultOptions = readlineSync.setDefaultOptions([newDefaultOptions]) ``` -Change the [Default Options](#options) to the values of properties of `newDefaultOptions` Object. +Change the [Default Options](#basic_options) to the values of properties of `newDefaultOptions` Object. All it takes is to specify options that you want change, because unspecified options are not updated. -## Options +## Basic Options  -An `options` Object can be specified to the methods to control the behavior of readlineSync. The options that were not specified to the methods are got from the Default Options. You can change the Default Options by [`setDefaultOptions`](#setdefaultoptions) method anytime, and it is kept until a current process is exited. +An `options` Object can be specified to the methods to control the behavior of readlineSync. The options that were not specified to the methods are got from the Default Options. You can change the Default Options by [`setDefaultOptions`](#basic_methods-setdefaultoptions) method anytime, and it is kept until a current process is exited. Specify the options that are often used to the Default Options, and specify temporary options to the methods. For example: @@ -186,7 +186,7 @@ a6 = readlineSync.question('And you? :'); // It's limited to 2 th The Object as `options` can have following properties. -### `prompt` +### `prompt`  _For `prompt*` methods only_ *Type:* string or others @@ -214,12 +214,12 @@ readlineSync.setDefaultOptions({ }); ``` -### `hideEchoBack` +### `hideEchoBack`  *Type:* boolean *Default:* `false` -If `true` is specified, hide the secret text (e.g. password) which is typed by user on screen by the mask characters (see [`mask`](#mask) option). +If `true` is specified, hide the secret text (e.g. password) which is typed by user on screen by the mask characters (see [`mask`](#basic_options-mask) option). For example: @@ -233,12 +233,12 @@ PASSWORD :******** Login ... ``` -### `mask` +### `mask`  *Type:* string *Default:* `'*'` -Set the mask characters that are shown instead of the secret text (e.g. password) when `true` is specified to [`hideEchoBack`](#hideechoback) option. If you want to show nothing, specify `''`. (But it might be not user friendly in some cases.) +Set the mask characters that are shown instead of the secret text (e.g. password) when `true` is specified to [`hideEchoBack`](#basic_options-hideechoback) option. If you want to show nothing, specify `''`. (But it might be not user friendly in some cases.) *Note:* In some cases (e.g. when the input stream is redirected on Windows XP), `'*'` or `''` might be used whether other one is specified. For example: @@ -252,19 +252,19 @@ secret = readlineSync.question('Please whisper sweet words :', { ![sample](cl_02.png) -### `limit` +### `limit`  Limit the user's input. The usage differ depending on the method. -#### For `question*` and `prompt*` methods +#### For `question*` and `prompt*` methods  *Type:* string, number, RegExp, function or Array *Default:* `[]` -Accept only the input that matches value that is specified to this. If the user input others, display a string that is specified to [`limitMessage`](#limitmessage) option, and wait for reinput. +Accept only the input that matches value that is specified to this. If the user input others, display a string that is specified to [`limitMessage`](#basic_options-limitmessage) option, and wait for reinput. -* The string is compared with the input. It is affected by [`caseSensitive`](#casesensitive) option. +* The string is compared with the input. It is affected by [`caseSensitive`](#basic_options-casesensitive) option. * The number is compared with the input that is converted to number by `parseFloat()`. For example, it interprets `' 3.14 '`, `'003.1400'`, `'314e-2'` and `'3.14PI'` as `3.14`. And it interprets `'005'`, `'5files'`, `'5kb'` and `'5px'` as `5`. * The RegExp tests the input. * The function that returns boolean to indicate whether it matches is called with the input. @@ -299,14 +299,14 @@ action = readlineSync.prompt({limit: availableActions}); // ** But `promptCL` method should be used instead of this. ** ``` -#### For `keyIn*` method +#### For `keyIn*` method  *Type:* string, number or Array *Default:* `[]` Accept only the key that matches value that is specified to this, ignore others. Specify the characters as the key. All strings or Array of those are decomposed into single characters. For example, `'abcde'` or `['a', 'bc', ['d', 'e']]` are the same as `['a', 'b', 'c', 'd', 'e']`. -These strings are compared with the input. It is affected by [`caseSensitive`](#casesensitive) option. +These strings are compared with the input. It is affected by [`caseSensitive`](#basic_options-casesensitive) option. The [placeholders](#placeholders) like `'${a-e}'` are replaced to an Array that is the character list like `['a', 'b', 'c', 'd', 'e']`. @@ -321,13 +321,13 @@ dice = readlineSync.keyIn('Roll the dice, What will the result be? :', {limit: '${1-6}'}); // range of '1' to '6' ``` -### `limitMessage` +### `limitMessage`  _For `question*` and `prompt*` methods only_ *Type:* string *Default:* `'Input another, please.${( [)limit(])}'` -Display this to the user when the [`limit`](#limit) option is specified and the user input others. +Display this to the user when the [`limit`](#basic_options-limit) option is specified and the user input others. The [placeholders](#placeholders) can be included. For example: @@ -339,7 +339,7 @@ file = readlineSync.question('Name of Text File :', { }); ``` -### `defaultInput` +### `defaultInput`  _For `question*` and `prompt*` methods only_ *Type:* string @@ -353,14 +353,14 @@ For example: lang = readlineSync.question('Which language? :', {defaultInput: 'javascript'}); ``` -### `trueValue`, `falseValue` +### `trueValue`, `falseValue`  *Type:* string, number, RegExp, function or Array *Default:* `[]` If the input matches `trueValue`, return `true`. If the input matches `falseValue`, return `false`. In any other case, return the input. -* The string is compared with the input. It is affected by [`caseSensitive`](#casesensitive) option. +* The string is compared with the input. It is affected by [`caseSensitive`](#basic_options-casesensitive) option. * The number is compared with the input that is converted to number by `parseFloat()`. For example, it interprets `' 3.14 '`, `'003.1400'`, `'314e-2'` and `'3.14PI'` as `3.14`. And it interprets `'005'`, `'5files'`, `'5kb'` and `'5px'` as `5`. Note that in `keyIn*` method, the input is every time one character (i.e. the number that is specified must be an integer within the range of `0` to `9`). * The RegExp tests the input. * The function that returns boolean to indicate whether it matches is called with the input. @@ -383,15 +383,15 @@ if (answer === true) { } ``` -### `caseSensitive` +### `caseSensitive`  *Type:* boolean *Default:* `false` By default, the string comparisons is case-insensitive (i.e. `a` equals `A`). If `true` is specified, it is case-sensitive, the cases are not ignored (i.e. `a` is different from `A`). -It affects: [`limit`](#limit), [`trueValue`](#truevalue), [`falseValue`](#falsevalue), some [placeholders](#placeholders), and some [Utility Methods](#utility-methods). +It affects: [`limit`](#basic_options-limit), [`trueValue`](#basic_options-truevalue_falsevalue), [`falseValue`](#basic_options-truevalue_falsevalue), some [placeholders](#placeholders), and some [Utility Methods](#utility_methods). -### `keepWhitespace` +### `keepWhitespace`  _For `question*` and `prompt*` methods only_ *Type:* boolean @@ -399,14 +399,14 @@ _For `question*` and `prompt*` methods only_ By default, remove the leading and trailing white spaces from the input text. If `true` is specified, don't remove those. -### `encoding` +### `encoding`  *Type:* string *Default:* `'utf8'` Set the encoding method of the input and output. -### `bufferSize` +### `bufferSize`  *Type:* number *Default:* `1024` @@ -414,16 +414,16 @@ Set the encoding method of the input and output. When readlineSync reads from a console directly (without external program), use a size `bufferSize` buffer. Even if the input by user exceeds it, it's usually no problem, because the buffer is used repeatedly. But some platforms's (e.g. Windows) console might not accept input that exceeds it. And set an enough size. -### `print` +### `print`  *Type:* function or `undefined` *Default:* `undefined` -Call the specified function with every output. The function is given two arguments, `display` as an output text, and a value of [`encoding`](#encoding) option. +Call the specified function with every output. The function is given two arguments, `display` as an output text, and a value of [`encoding`](#basic_options-encoding) option. For example: -* Pass the plain texts to the Logger (e.g. [log4js](#https://github.com/nomiddlename/log4js-node)), after clean the colored texts. +* Pass the plain texts to the Logger (e.g. [log4js](https://github.com/nomiddlename/log4js-node)), after clean the colored texts. ![sample](cl_01.png) @@ -490,7 +490,7 @@ May I have your name? :Oz Hi Oz! Where do you live? :Emerald City ``` -### `history` +### `history`  _For `question*` and `prompt*` methods only_ *Type:* boolean @@ -523,7 +523,7 @@ hello -- You said "bye" ``` -### `cd` +### `cd`  _For `question*` and `prompt*` methods only_ *Type:* boolean @@ -559,11 +559,11 @@ File :file-c.html -- Specified file is /path/to/foo-dir/bar-dir/file-c.html ``` -## Utility Methods +## Utility Methods  -These are convenient methods that are extended [Basic Methods](#basic-methods) to be used easily. +These are convenient methods that are extended [Basic Methods](#basic_methods) to be used easily. -### `questionEMail` +### `questionEMail`  ```js email = readlineSync.questionEMail([query[, options]]) @@ -571,10 +571,10 @@ email = readlineSync.questionEMail([query[, options]]) Display a `query` to the user if it's specified, and then accept only a valid e-mail address, and then return it after an Enter key was pressed. -The `query` is handled the same as that of the [`question`](#question) method. +The `query` is handled the same as that of the [`question`](#basic_methods-question) method. The default value of `query` is `'Input e-mail address :'`. -*Note:* The valid e-mail address requirement is a willful violation of [RFC5322](http://tools.ietf.org/html/rfc5322), this is defined in [HTML5](http://www.w3.org/TR/html5/forms.html). This works enough to prevent the user mistaking. If you want to change it, specify [`limit`](#limit) option. +*Note:* The valid e-mail address requirement is a willful violation of [RFC5322](http://tools.ietf.org/html/rfc5322), this is defined in [HTML5](http://www.w3.org/TR/html5/forms.html). This works enough to prevent the user mistaking. If you want to change it, specify [`limit`](#basic_options-limit) option. For example: @@ -590,26 +590,26 @@ Input e-mail address :mail@example.com -- E-mail is mail@example.com ``` -#### Options +#### Options  -The following options have independent default value that is not affected by [Default Options](#options). +The following options have independent default value that is not affected by [Default Options](#basic_options). | Option Name | Default Value | |-------------------|---------------| -| [`hideEchoBack`](#hideechoback) | `false` | -| [`limit`](#limit) | RegExp by [HTML5](http://www.w3.org/TR/html5/forms.html) | -| [`limitMessage`](#limitmessage) | `'Input valid e-mail address, please.'` | -| [`trueValue`](#truevalue) | `null` | -| [`falseValue`](#falsevalue) | `null` | +| [`hideEchoBack`](#basic_options-hideechoback) | `false` | +| [`limit`](#basic_options-limit) | RegExp by [HTML5](http://www.w3.org/TR/html5/forms.html) | +| [`limitMessage`](#basic_options-limitmessage) | `'Input valid e-mail address, please.'` | +| [`trueValue`](#basic_options-truevalue_falsevalue) | `null` | +| [`falseValue`](#basic_options-truevalue_falsevalue) | `null` | -The following options work as shown in the [Options](#options) section. +The following options work as shown in the [Basic Options](#basic_options) section. - - + +
maskdefaultInputcaseSensitiveencodingbufferSize
printhistory
maskdefaultInputcaseSensitiveencodingbufferSize
printhistory
-### `questionNewPassword` +### `questionNewPassword`  ```js password = readlineSync.questionNewPassword([query[, options]]) @@ -619,10 +619,10 @@ Display a `query` to the user if it's specified, and then accept only a valid pa It's the password, or something that is the secret text like the password. You can specify the valid password requirement to the options. -The `query` is handled the same as that of the [`question`](#question) method. +The `query` is handled the same as that of the [`question`](#basic_methods-question) method. The default value of `query` is `'Input new password :'`. -*Note:* Only the form of password is checked. Check it more if you want. For example, [zxcvbn](#https://github.com/dropbox/zxcvbn) is password strength estimation library. +*Note:* Only the form of password is checked. Check it more if you want. For example, [zxcvbn](https://github.com/dropbox/zxcvbn) is password strength estimation library. For example: @@ -642,28 +642,28 @@ Reinput a same one to confirm it :************* -- Password is _my_password_ ``` -#### Options +#### Options  -The following options have independent default value that is not affected by [Default Options](#options). +The following options have independent default value that is not affected by [Default Options](#basic_options). | Option Name | Default Value | |-------------------|---------------| -| [`hideEchoBack`](#hideechoback) | `true` | -| [`mask`](#mask) | `'*'` | -| [`limitMessage`](#limitmessage) | `'It can include: ${charlist}\nAnd the length must be: ${length}'` | -| [`trueValue`](#truevalue) | `null` | -| [`falseValue`](#falsevalue) | `null` | -| [`caseSensitive`](#casesensitive) | `true` | +| [`hideEchoBack`](#basic_options-hideechoback) | `true` | +| [`mask`](#basic_options-mask) | `'*'` | +| [`limitMessage`](#basic_options-limitmessage) | `'It can include: ${charlist}\nAnd the length must be: ${length}'` | +| [`trueValue`](#basic_options-truevalue_falsevalue) | `null` | +| [`falseValue`](#basic_options-truevalue_falsevalue) | `null` | +| [`caseSensitive`](#basic_options-casesensitive) | `true` | -The following options work as shown in the [Options](#options) section. +The following options work as shown in the [Basic Options](#basic_options) section. - +
defaultInputkeepWhitespaceencodingbufferSizeprint
defaultInputkeepWhitespaceencodingbufferSizeprint
And the following additional options are available. -##### `charlist` +##### `charlist`  *Type:* string *Default:* `'${!-~}'` @@ -677,7 +677,7 @@ For example, let the user input a password that is created with alphabet and som password = readlineSync.questionNewPassword('PASSWORD :', {charlist: '${a-z}#$@%'}); ``` -##### `min`, `max` +##### `min`, `max`  *Type:* number *Default:* `min`: `12`, `max`: `24` @@ -685,7 +685,7 @@ password = readlineSync.questionNewPassword('PASSWORD :', {charlist: '${a-z}#$@% `min`: A number as a minimum length of the password. `max`: A number as a maximum length of the password. -##### `confirmMessage` +##### `confirmMessage`  *Type:* string or others *Default:* `'Reinput a same one to confirm it :'` @@ -694,27 +694,27 @@ A message that lets the user input the same password again. It can include the [placeholders](#placeholders). If this is not string, it is converted to string (i.e. `toString` method is called). -##### `unmatchMessage` +##### `unmatchMessage`  *Type:* string or others *Default:* `'It differs from first one. Hit only Enter key if you want to retry from first one.'` A warning message that is displayed when the second input did not match first one. -This is converted the same as the [`confirmMessage`](#confirmmessage) option. +This is converted the same as the [`confirmMessage`](#utility_methods-questionnewpassword-options-confirmmessage) option. -#### Additional Placeholders +#### Additional Placeholders  The following additional [placeholder](#placeholders) parameters are available. -##### `charlist` +##### `charlist`  -A current value of [`charlist`](#charlist) option that is converted to human readable if possible. (e.g. `'A...Z'`) +A current value of [`charlist`](#utility_methods-questionnewpassword-options-charlist) option that is converted to human readable if possible. (e.g. `'A...Z'`) -##### `length` +##### `length`  -A current value of [`min` and `max`](#min-max) option that is converted to human readable. (e.g. `'12...24'`) +A current value of [`min` and `max`](#utility_methods-questionnewpassword-options-min_max) option that is converted to human readable. (e.g. `'12...24'`) -### `questionInt` +### `questionInt`  ```js numInt = readlineSync.questionInt([query[, options]]) @@ -723,24 +723,24 @@ numInt = readlineSync.questionInt([query[, options]]) Display a `query` to the user if it's specified, and then accept only an input that can be interpreted as an integer, and then return the number (not string) after an Enter key was pressed. This parses the input as much as possible by `parseInt()`. For example, it interprets `' 5 '`, `'5.6'`, `'005'`, `'5files'`, `'5kb'` and `'5px'` as `5`. -The `query` is handled the same as that of the [`question`](#question) method. +The `query` is handled the same as that of the [`question`](#basic_methods-question) method. -#### Options +#### Options  -The following option has independent default value that is not affected by [Default Options](#options). +The following option has independent default value that is not affected by [Default Options](#basic_options). | Option Name | Default Value | |-------------------|---------------| -| [`limitMessage`](#limitmessage) | `'Input valid number, please.'` | +| [`limitMessage`](#basic_options-limitmessage) | `'Input valid number, please.'` | -The following options work as shown in the [Options](#options) section. +The following options work as shown in the [Basic Options](#basic_options) section. - - + +
hideEchoBackmaskdefaultInputcaseSensitivekeepWhitespace
encodingbufferSizeprinthistory
hideEchoBackmaskdefaultInputcaseSensitivekeepWhitespace
encodingbufferSizeprinthistory
-### `questionFloat` +### `questionFloat`  ```js numFloat = readlineSync.questionFloat([query[, options]]) @@ -749,24 +749,24 @@ numFloat = readlineSync.questionFloat([query[, options]]) Display a `query` to the user if it's specified, and then accept only an input that can be interpreted as a floating-point number, and then return the number (not string) after an Enter key was pressed. This parses the input as much as possible by `parseFloat()`. For example, it interprets `' 3.14 '`, `'003.1400'`, `'314e-2'` and `'3.14PI'` as `3.14`. -The `query` is handled the same as that of the [`question`](#question) method. +The `query` is handled the same as that of the [`question`](#basic_methods-question) method. -#### Options +#### Options  -The following option has independent default value that is not affected by [Default Options](#options). +The following option has independent default value that is not affected by [Default Options](#basic_options). | Option Name | Default Value | |-------------------|---------------| -| [`limitMessage`](#limitmessage) | `'Input valid number, please.'` | +| [`limitMessage`](#basic_options-limitmessage) | `'Input valid number, please.'` | -The following options work as shown in the [Options](#options) section. +The following options work as shown in the [Basic Options](#basic_options) section. - - + +
hideEchoBackmaskdefaultInputcaseSensitivekeepWhitespace
encodingbufferSizeprinthistory
hideEchoBackmaskdefaultInputcaseSensitivekeepWhitespace
encodingbufferSizeprinthistory
-### `questionPath` +### `questionPath`  ```js path = readlineSync.questionPath([query[, options]]) @@ -776,9 +776,9 @@ Display a `query` to the user if it's specified, and then accept only a valid lo The `~` that is input by the user is replaced to the home directory. You can specify the valid local file or directory path requirement to the options. And you can make it create a new file or directory when it doesn't exist. -It is recommended to use this method with the [`cd`](#cd) option. (Default: `true`) +It is recommended to use this method with the [`cd`](#basic_options-cd) option. (Default: `true`) -The `query` is handled the same as that of the [`question`](#question) method. +The `query` is handled the same as that of the [`question`](#basic_methods-question) method. The default value of `query` is `'Input path (you can "cd" and "pwd") :'`. For example: @@ -810,29 +810,29 @@ Save to :~/deploy/data -- saveDir: /home/user/deploy/data ``` -#### Options +#### Options  -The following options have independent default value that is not affected by [Default Options](#options). +The following options have independent default value that is not affected by [Default Options](#basic_options). | Option Name | Default Value | |-------------------|---------------| -| [`hideEchoBack`](#hideechoback) | `false` | -| [`limitMessage`](#limitmessage) | `'${error(\n)}Input valid path, please.${( Min:)min}${( Max:)max}'` | -| [`history`](#history) | `true` | -| [`cd`](#cd) | `true` | +| [`hideEchoBack`](#basic_options-hideechoback) | `false` | +| [`limitMessage`](#basic_options-limitmessage) | `'${error(\n)}Input valid path, please.${( Min:)min}${( Max:)max}'` | +| [`history`](#basic_options-history) | `true` | +| [`cd`](#basic_options-cd) | `true` | -The following options work as shown in the [Options](#options) section. +The following options work as shown in the [Basic Options](#basic_options) section. - - + +
maskdefaultInputcaseSensitiveencodingbufferSize
print
maskdefaultInputcaseSensitiveencodingbufferSize
print
And the following additional options are available. *Note:* It does not check the coherency about a combination of the options as the path requirement. For example, the `{exists: false, isFile: true}` never check that it is a file because it is limited to the path that does not exist. -##### `exists` +##### `exists`  *Type:* boolean or others *Default:* `true` @@ -840,7 +840,7 @@ And the following additional options are available. If `true` is specified, accept only a file or directory path that exists. If `false` is specified, accept only a file or directory path that does *not* exist. In any other case, the existence is not checked. -##### `min`, `max` +##### `min`, `max`  *Type:* number or others *Default:* `undefined` @@ -849,7 +849,7 @@ In any other case, the existence is not checked. `max`: A number as a maximum size of the file that is accepted. If it is not specified or `0` is specified, the size is not checked. (A size of directory is `0`.) -##### `isFile`, `isDirectory` +##### `isFile`, `isDirectory`  *Type:* boolean *Default:* `false` @@ -857,13 +857,13 @@ If it is not specified or `0` is specified, the size is not checked. (A size of `isFile`: If `true` is specified, accept only a file path. `isDirectory`: If `true` is specified, accept only a directory path. -##### `validate` +##### `validate`  *Type:* function or `undefined` *Default:* `undefined` If a function that returns `true` or an error message is specified, call it with a path that was input, and accept the input when the function returned `true`. -If the function returned a string as an error message, that message is got by the [`error`](#error) additional [placeholder](#placeholders) parameter. +If the function returned a string as an error message, that message is got by the [`error`](#utility_methods-questionpath-additional_placeholders-error) additional [placeholder](#placeholders) parameter. A path that was input is parsed before it is passed to the function. `~` is replaced to a home directory, and a path is converted to an absolute path. This is also a return value from this method. @@ -875,34 +875,34 @@ imageFile = readlineSync.questionPath('Image File :', { }); ``` -##### `create` +##### `create`  *Type:* boolean *Default:* `false` -If `true` is specified, create a file or directory as a path that was input when it doesn't exist. If `true` is specified to the [`isDirectory`](#isdirectory) option, create a directory, otherwise a file. +If `true` is specified, create a file or directory as a path that was input when it doesn't exist. If `true` is specified to the [`isDirectory`](#utility_methods-questionpath-options-isfile_isdirectory) option, create a directory, otherwise a file. It does not affect the existence check. Therefore, you can get a new file or directory path anytime by specifying: `{exists: false, create: true}` -#### Additional Placeholders +#### Additional Placeholders  The following additional [placeholder](#placeholders) parameters are available. -##### `error` +##### `error`  An error message when the input was not accepted. -This value is set by readlineSync, or the function that was specified to [`validate`](#validate) option. +This value is set by readlineSync, or the function that was specified to [`validate`](#utility_methods-questionpath-options-validate) option. -##### `min`, `max` +##### `min`, `max`  -A current value of [`min` and `max`](#min-max-1) option. +A current value of [`min` and `max`](#utility_methods-questionpath-options-min_max) option. -### `promptCL` +### `promptCL`  ```js argsArray = readlineSync.promptCL([commandHandler[, options]]) ``` -Display a prompt-sign (see [`prompt`](#prompt-1) option) to the user, and then consider the input as a command-line and parse it, and then return a result after an Enter key was pressed. +Display a prompt-sign (see [`prompt`](#basic_options-prompt) option) to the user, and then consider the input as a command-line and parse it, and then return a result after an Enter key was pressed. A return value is an Array that includes the tokens that were parsed. It parses the input from the user as the command-line, and it interprets whitespaces, quotes, etc., and it splits it to tokens properly. Usually, a first element of the Array is command-name, and remaining elements are arguments. For example: @@ -924,9 +924,9 @@ arg arg ``` -#### `commandHandler` +#### `commandHandler`  -By using the `commandHandler` argument, this method will come into its own. Specifying the Object to this argument has the more merit. And it has the more merit for [`promptCLLoop`](#promptclloop) method. +By using the `commandHandler` argument, this method will come into its own. Specifying the Object to this argument has the more merit. And it has the more merit for [`promptCLLoop`](#utility_methods-promptclloop) method. If a function is specified to `commandHandler` argument, it is just called with a parsed Array as an argument list of the function. And `this` is an original input string, in the function. @@ -952,7 +952,7 @@ readlineSync.promptCL(function(command, arg1, arg2) { }); ``` -If an Object that has properties named as the command-name is specified, the command-name is interpreted, and a function as the value of matched property is called. A function is chosen properly by handling case of the command-name in accordance with the [`caseSensitive`](#casesensitive) option. +If an Object that has properties named as the command-name is specified, the command-name is interpreted, and a function as the value of matched property is called. A function is chosen properly by handling case of the command-name in accordance with the [`caseSensitive`](#basic_options-casesensitive) option. The function is called with a parsed Array that excludes a command-name (i.e. first element is removed from the Array) as an argument list of the function. That is, a structure of the `commandHandler` Object looks like: @@ -995,31 +995,31 @@ readlineSync.promptCL({ }); ``` -#### Options +#### Options  -The following options have independent default value that is not affected by [Default Options](#options). +The following options have independent default value that is not affected by [Default Options](#basic_options). | Option Name | Default Value | |-------------------|---------------| -| [`hideEchoBack`](#hideechoback) | `false` | -| [`limitMessage`](#limitmessage) | `'Requested command is not available.'` | -| [`caseSensitive`](#casesensitive) | `false` | -| [`history`](#history) | `true` | +| [`hideEchoBack`](#basic_options-hideechoback) | `false` | +| [`limitMessage`](#basic_options-limitmessage) | `'Requested command is not available.'` | +| [`caseSensitive`](#basic_options-casesensitive) | `false` | +| [`history`](#basic_options-history) | `true` | -The following options work as shown in the [Options](#options) section. +The following options work as shown in the [Basic Options](#basic_options) section. - - + +
promptmaskdefaultInputencodingbufferSize
printcd
promptmaskdefaultInputencodingbufferSize
printcd
-### `promptLoop` +### `promptLoop`  ```js readlineSync.promptLoop(inputHandler[, options]) ``` -Display a prompt-sign (see [`prompt`](#prompt-1) option) to the user, and then call `inputHandler` function with the input from the user after it has been typed and an Enter key was pressed. Do these repeatedly until `inputHandler` function returns `true`. +Display a prompt-sign (see [`prompt`](#basic_options-prompt) option) to the user, and then call `inputHandler` function with the input from the user after it has been typed and an Enter key was pressed. Do these repeatedly until `inputHandler` function returns `true`. For example, the following 2 codes work same: @@ -1052,28 +1052,28 @@ console.log('It\'s exited from loop.'); It's exited from loop. ``` -#### Options +#### Options  -The following options have independent default value that is not affected by [Default Options](#options). +The following options have independent default value that is not affected by [Default Options](#basic_options). | Option Name | Default Value | |-------------------|---------------| -| [`hideEchoBack`](#hideechoback) | `false` | -| [`trueValue`](#truevalue) | `null` | -| [`falseValue`](#falsevalue) | `null` | -| [`caseSensitive`](#casesensitive) | `false` | -| [`history`](#history) | `true` | +| [`hideEchoBack`](#basic_options-hideechoback) | `false` | +| [`trueValue`](#basic_options-truevalue_falsevalue) | `null` | +| [`falseValue`](#basic_options-truevalue_falsevalue) | `null` | +| [`caseSensitive`](#basic_options-casesensitive) | `false` | +| [`history`](#basic_options-history) | `true` | -The other options work as shown in the [Options](#options) section. +The other options work as shown in the [Basic Options](#basic_options) section. -### `promptCLLoop` +### `promptCLLoop`  ```js readlineSync.promptCLLoop([commandHandler[, options]]) ``` -Execute [`promptCL`](#promptcl) method repeatedly until chosen [`commandHandler`](#commandhandler) returns `true`. -The [`commandHandler`](#commandhandler) may be a function that is called like: +Execute [`promptCL`](#utility_methods-promptcl) method repeatedly until chosen [`commandHandler`](#utility_methods-promptcl-commandhandler) returns `true`. +The [`commandHandler`](#utility_methods-promptcl-commandhandler) may be a function that is called like: ```js exit = allCommands(command, arg1, arg2, ...); @@ -1085,8 +1085,8 @@ or an Object that has the functions that are called like: exit = foundCommand(arg1, arg2, ...); ``` -See [`promptCL`](#promptcl) method for details. -This method looks like a combination of [`promptCL`](#promptcl) method and [`promptLoop`](#promptloop) method. +See [`promptCL`](#utility_methods-promptcl) method for details. +This method looks like a combination of [`promptCL`](#utility_methods-promptcl) method and [`promptLoop`](#utility_methods-promptloop) method. For example: @@ -1114,25 +1114,25 @@ filesOnOld is copied to New Hard Disk. It's exited from loop. ``` -#### Options +#### Options  -The following options have independent default value that is not affected by [Default Options](#options). +The following options have independent default value that is not affected by [Default Options](#basic_options). | Option Name | Default Value | |-------------------|---------------| -| [`hideEchoBack`](#hideechoback) | `false` | -| [`limitMessage`](#limitmessage) | `'Requested command is not available.'` | -| [`caseSensitive`](#casesensitive) | `false` | -| [`history`](#history) | `true` | +| [`hideEchoBack`](#basic_options-hideechoback) | `false` | +| [`limitMessage`](#basic_options-limitmessage) | `'Requested command is not available.'` | +| [`caseSensitive`](#basic_options-casesensitive) | `false` | +| [`history`](#basic_options-history) | `true` | -The following options work as shown in the [Options](#options) section. +The following options work as shown in the [Basic Options](#basic_options) section. - - + +
promptmaskdefaultInputencodingbufferSize
printcd
promptmaskdefaultInputencodingbufferSize
printcd
-### `promptSimShell` +### `promptSimShell`  ```js input = readlineSync.promptSimShell([options]) @@ -1153,18 +1153,18 @@ On others: user@host:~/path/to/directory$ ``` -#### Options +#### Options  -The following options have independent default value that is not affected by [Default Options](#options). +The following options have independent default value that is not affected by [Default Options](#basic_options). | Option Name | Default Value | |-------------------|---------------| -| [`hideEchoBack`](#hideechoback) | `false` | -| [`history`](#history) | `true` | +| [`hideEchoBack`](#basic_options-hideechoback) | `false` | +| [`history`](#basic_options-history) | `true` | -The other options other than [`prompt`](#prompt-1) option work as shown in the [Options](#options) section. +The other options other than [`prompt`](#basic_options-prompt) option work as shown in the [Basic Options](#basic_options) section. -### `keyInYN` +### `keyInYN`  ```js boolYesOrEmpty = readlineSync.keyInYN([query[, options]]) @@ -1177,10 +1177,10 @@ This method works like the `window.confirm` method of web browsers. A return val * `N`: `false` * other: `''` -The `query` is handled the same as that of the [`question`](#question) method. +The `query` is handled the same as that of the [`question`](#basic_methods-question) method. The default value of `query` is `'Are you sure? :'`. -The keys other than `Y` and `N` are also accepted (If you want to know a user's wish explicitly, use [`keyInYNStrict`](#keyinynstrict) method). Therefore, if you let the user make an important decision (e.g. files are removed), check whether the return value is not **falsy**. That is, a default is "No". +The keys other than `Y` and `N` are also accepted (If you want to know a user's wish explicitly, use [`keyInYNStrict`](#utility_methods-keyinynstrict) method). Therefore, if you let the user make an important decision (e.g. files are removed), check whether the return value is not *falsy*. That is, a default is "No". For example: @@ -1205,17 +1205,17 @@ if (readlineSync.keyInYN('Continue virus scan?') === false) { // Continue... ``` -#### Options +#### Options  -The following options work as shown in the [Options](#options) section. +The following options work as shown in the [Basic Options](#basic_options) section. - +
encodingbufferSizeprint
encodingbufferSizeprint
And the following additional option is available. -##### `guide` +##### `guide`  *Type:* boolean *Default:* `true` @@ -1234,7 +1234,7 @@ Do you like me? [y/n] :y Really? [y/n] :y ``` -### `keyInYNStrict` +### `keyInYNStrict`  ```js boolYes = readlineSync.keyInYNStrict([query[, options]]) @@ -1246,14 +1246,14 @@ This method works like the `window.confirm` method of web browsers. A return val * `Y`: `true` * `N`: `false` -The `query` is handled the same as that of the [`question`](#question) method. +The `query` is handled the same as that of the [`question`](#basic_methods-question) method. The default value of `query` is `'Are you sure? :'`. -A key other than `Y` and `N` is not accepted. That is, a return value has no default. Therefore, the user has to tell an own wish explicitly. If you want to know a user's wish easily, use [`keyInYN`](#keyinyn) method. +A key other than `Y` and `N` is not accepted. That is, a return value has no default. Therefore, the user has to tell an own wish explicitly. If you want to know a user's wish easily, use [`keyInYN`](#utility_methods-keyinyn) method. -This method works same to [`keyInYN`](#keyinyn) method except that this accept only `Y` or `N` key. The options also work same to [`keyInYN`](#keyinyn) method. +This method works same to [`keyInYN`](#utility_methods-keyinyn) method except that this accept only `Y` or `N` key. The options also work same to [`keyInYN`](#utility_methods-keyinyn) method. -### `keyInPause` +### `keyInPause`  ```js readlineSync.keyInPause([query[, options]]) @@ -1261,9 +1261,9 @@ readlineSync.keyInPause([query[, options]]) Display a `query` to the user if it's specified, and then just wait for a key to be pressed by the user. This method works like the `window.alert` method of web browsers. This is used to make the running of script pause and show something to the user, or wait for the user to be ready. -By default, any key is accepted. You can change this behavior by specifying [`limit`](#limit) option (e.g. accept only a Space Bar). +By default, any key is accepted. You can change this behavior by specifying [`limit`](#basic_options-limit) option (e.g. accept only a Space Bar). -The `query` is handled the same as that of the [`question`](#question) method. +The `query` is handled the same as that of the [`question`](#basic_methods-question) method. The default value of `query` is `'Continue...'`. For example: @@ -1285,23 +1285,23 @@ Continue... (Hit any key) It's executing now... ``` -#### Options +#### Options  -The following option has independent default value that is not affected by [Default Options](#options). +The following option has independent default value that is not affected by [Default Options](#basic_options). | Option Name | Default Value | |-------------------|---------------| -| [`limit`](#limit) | `null` | +| [`limit`](#basic_options-limit) | `null` | -The following options work as shown in the [Options](#options) section. +The following options work as shown in the [Basic Options](#basic_options) section. - +
caseSensitiveencodingbufferSizeprint
caseSensitiveencodingbufferSizeprint
And the following additional option is available. -##### `guide` +##### `guide`  *Type:* boolean *Default:* `true` @@ -1318,7 +1318,7 @@ readlineSync.keyInPause('It\'s pausing now...'); It's pausing now... (Hit any key) ``` -### `keyInSelect` +### `keyInSelect`  ```js index = readlineSync.keyInSelect(items[, query[, options]]) @@ -1326,7 +1326,7 @@ index = readlineSync.keyInSelect(items[, query[, options]]) Display the list that was created with the `items` Array, and the `query` to the user if it's specified, and then return the number as an index of the `items` Array immediately it was chosen by pressing a key by the user, **without pressing an Enter key**. Note that the user has no chance to change the input. -The `query` is handled the same as that of the [`question`](#question) method. +The `query` is handled the same as that of the [`question`](#basic_methods-question) method. The default value of `query` is `'Choose one from list :'`. The minimum length of `items` Array is 1 and maximum length is 35. These elements are displayed as item list. A key to let the user choose an item is assigned to each item automatically in sequence like "1, 2, 3 ... 9, A, B, C ...". A number as an index of the `items` Array that corresponds to a chosen item by the user is returned. @@ -1351,41 +1351,41 @@ Which framework? [1...5 / 0] :2 hapi is enabled. ``` -#### Options +#### Options  -The following option has independent default value that is not affected by [Default Options](#options). +The following option has independent default value that is not affected by [Default Options](#basic_options). | Option Name | Default Value | |-------------------|---------------| -| [`hideEchoBack`](#hideechoback) | `false` | +| [`hideEchoBack`](#basic_options-hideechoback) | `false` | -The following options work as shown in the [Options](#options) section. +The following options work as shown in the [Basic Options](#basic_options) section. - +
maskencodingbufferSizeprint
maskencodingbufferSizeprint
And the following additional options are available. -##### `guide` +##### `guide`  *Type:* boolean *Default:* `true` If `true` is specified, a string like `'[1...5]'` as guide for the user is added to `query`. And `':'` is moved to the end of `query`, or it is added. This is the key list that corresponds to the item list. -##### `cancel` +##### `cancel`  *Type:* boolean *Default:* `true` If `true` is specified, an item to let the user tell "cancel" is added to the item list. "[0] CANCEL" is displayed, and if `0` key is pressed, `-1` is returned. -## Placeholders +## Placeholders  The placeholders in the text are replaced to another string. -For example, the [`limitMessage`](#limitmessage) option to display a warning message that means that the command the user requested is not available: +For example, the [`limitMessage`](#basic_options-limitmessage) option to display a warning message that means that the command the user requested is not available: ```js command = readlineSync.prompt({ @@ -1402,11 +1402,11 @@ delete is not available. The placeholders can be included in: * `query` argument -* [`prompt`](#prompt-1) and [`limitMessage`](#limitmessage) options -* [`limit` option for `keyIn*` method](#for-keyin-method) and [`charlist`](#charlist) option for [`questionNewPassword`](#questionnewpassword) method ([`C1-C2`](#c1-c2) parameter only) -* And some additional options for the [Utility Methods](#utility-methods). +* [`prompt`](#basic_options-prompt) and [`limitMessage`](#basic_options-limitmessage) options +* [`limit` option for `keyIn*` method](#basic_options-limit-for_keyin_method) and [`charlist`](#utility_methods-questionnewpassword-options-charlist) option for [`questionNewPassword`](#utility_methods-questionnewpassword) method ([`C1-C2`](#placeholders-parameters-c1_c2) parameter only) +* And some additional options for the [Utility Methods](#utility_methods). -### Syntax +### Syntax  ``` ${parameter} @@ -1462,15 +1462,15 @@ Refused . Please input another. (May be more better: `'${(Refused )lastInput( you requested. )}Please input another.'`) -### Parameters +### Parameters  -The following parameters are available. And some additional parameters are available in the [Utility Methods](#utility-methods). +The following parameters are available. And some additional parameters are available in the [Utility Methods](#utility_methods). -#### `hideEchoBack`, `mask`, `defaultInput`, `caseSensitive`, `keepWhitespace`, `encoding`, `bufferSize`, `history`, `cd`, `limit`, `trueValue`, `falseValue` +#### `hideEchoBack`, `mask`, `defaultInput`, `caseSensitive`, `keepWhitespace`, `encoding`, `bufferSize`, `history`, `cd`, `limit`, `trueValue`, `falseValue`  A current value of each option. It is converted to human readable if possible. The boolean value is replaced to `'on'` or `'off'`, and the Array is replaced to the list of only string and number elements. -And in the `keyIn*` method, the parts of the list as characters sequence are suppressed. For example, when `['a', 'b', 'c', 'd', 'e']` is specified to the [`limit`](#limit) option, `'${limit}'` is replaced to `'a...e'`. If `true` is specified to the [`caseSensitive`](#casesensitive) option, the characters are converted to lower case. +And in the `keyIn*` method, the parts of the list as characters sequence are suppressed. For example, when `['a', 'b', 'c', 'd', 'e']` is specified to the [`limit`](#basic_options-limit) option, `'${limit}'` is replaced to `'a...e'`. If `true` is specified to the [`caseSensitive`](#basic_options-casesensitive) option, the characters are converted to lower case. For example: @@ -1485,10 +1485,10 @@ input = readlineSync.question( Input something or Enter key as "hello" : ``` -#### `limitCount`, `limitCountNotZero` +#### `limitCount`, `limitCountNotZero`  -A length of a current value of the [`limit`](#limit) option. -When the value of the [`limit`](#limit) option is empty, `'${limitCount}'` is replaced to `'0'`, `'${limitCountNotZero}'` is replaced to `''`. +A length of a current value of the [`limit`](#basic_options-limit) option. +When the value of the [`limit`](#basic_options-limit) option is empty, `'${limitCount}'` is replaced to `'0'`, `'${limitCountNotZero}'` is replaced to `''`. For example: @@ -1503,7 +1503,7 @@ action = readlineSync.question( Choose action from 5 actions : ``` -#### `lastInput` +#### `lastInput`  A last input from the user. In any case, this is saved. @@ -1522,9 +1522,9 @@ command = readlineSync.prompt({ wrong-command is not available. ``` -#### `history_mN` +#### `history_mN`  -When the history expansion feature is enabled (see [`history`](#history) option), a current command line minus `N`. +When the history expansion feature is enabled (see [`history`](#basic_options-history) option), a current command line minus `N`. *This feature keeps the previous input only.* That is, only `history_m1` is supported. For example: @@ -1544,7 +1544,7 @@ hello -- You said "hello" ``` -#### `cwd`, `CWD`, `cwdHome` +#### `cwd`, `CWD`, `cwdHome`  A current working directory. @@ -1562,7 +1562,7 @@ command = readlineSync.prompt({prompt: '[${cwdHome}]$ '}); [~/foo/bar]$ ``` -#### `date`, `time`, `localeDate`, `localeTime` +#### `date`, `time`, `localeDate`, `localeTime`  A string as current date or time. @@ -1581,9 +1581,9 @@ command = readlineSync.prompt({prompt: '[${localeDate}]> '}); [04/21/2015]> ``` -#### `C1-C2` +#### `C1-C2`  -_For [`limit` option for `keyIn*` method](#for-keyin-method) and [`charlist`](#charlist) option for [`questionNewPassword`](#questionnewpassword) method only_ +_For [`limit` option for `keyIn*` method](#basic_options-limit-for_keyin_method) and [`charlist`](#utility_methods-questionnewpassword-options-charlist) option for [`questionNewPassword`](#utility_methods-questionnewpassword) method only_ A character list. `C1` and `C2` are each single character as the start and the end. A sequence in ascending or descending order of characters ranging from `C1` to `C2` is created. For example, `'${a-e}'` is replaced to `'abcde'`. `'${5-1}'` is replaced to `'54321'`. @@ -1594,9 +1594,9 @@ For example, let the user input a password that is created with alphabet: password = readlineSync.questionNewPassword('PASSWORD :', {charlist: '${a-z}'}); ``` -See also [`limit` option for `keyIn*` method](#for-keyin-method). +See also [`limit` option for `keyIn*` method](#basic_options-limit-for_keyin_method). -## With Task Runner +## With Task Runner  The easy way to control a flow of the task runner by the input from the user: @@ -1643,9 +1643,9 @@ grunt.initConfig({ }); ``` -## Note +## Note  -### Platforms +### Platforms  The TTY interfaces are different by the platforms. If the platform doesn't support the interactively reading from TTY, an error is thrown. @@ -1658,7 +1658,7 @@ try { } ``` -### Reading by External Program +### Reading by External Program  readlineSync tries to read from a console by using the external program if it is needed (e.g. when the input stream is redirected on Windows XP). And if the running Node doesn't support the [Synchronous Process Execution](http://nodejs.org/api/child_process.html#child_process_synchronous_process_creation) (i.e. Node v0.10-), readlineSync uses "piping via files" for the synchronous execution. As everyone knows, "piping via files" is no good. It blocks the event loop and a process. It might make the your script be slow. @@ -1668,15 +1668,15 @@ Why did I choose it? : * The good modules (native addon) for the synchronous execution exist, but node-gyp can't compile those in some platforms or Node versions. * I think that the security is important more than the speed. Some modules have problem about security. Those don't protect the data. I think that the speed is not needed usually, because readlineSync is used while the user types keys. -## Deprecated Methods and Options +## Deprecated Methods and Options  The readlineSync current version is fully compatible with older version. The following methods and options are deprecated. -### `setPrint` method +### `setPrint` method  -Use the [`print`](#print) option. -For the [Default Options](#options), use: +Use the [`print`](#basic_options-print) option. +For the [Default Options](#basic_options), use: ```js readlineSync.setDefaultOptions({print: value}); @@ -1688,10 +1688,10 @@ instead of: readlineSync.setPrint(value); ``` -### `setPrompt` method +### `setPrompt` method  -Use the [`prompt`](#prompt-1) option. -For the [Default Options](#options), use: +Use the [`prompt`](#basic_options-prompt) option. +For the [Default Options](#basic_options), use: ```js readlineSync.setDefaultOptions({prompt: value}); @@ -1703,10 +1703,10 @@ instead of: readlineSync.setPrompt(value); ``` -### `setEncoding` method +### `setEncoding` method  -Use the [`encoding`](#encoding) option. -For the [Default Options](#options), use: +Use the [`encoding`](#basic_options-encoding) option. +For the [Default Options](#basic_options), use: ```js readlineSync.setDefaultOptions({encoding: value}); @@ -1718,10 +1718,10 @@ instead of: readlineSync.setEncoding(value); ``` -### `setMask` method +### `setMask` method  -Use the [`mask`](#mask) option. -For the [Default Options](#options), use: +Use the [`mask`](#basic_options-mask) option. +For the [Default Options](#basic_options), use: ```js readlineSync.setDefaultOptions({mask: value}); @@ -1733,10 +1733,10 @@ instead of: readlineSync.setMask(value); ``` -### `setBufferSize` method +### `setBufferSize` method  -Use the [`bufferSize`](#buffersize) option. -For the [Default Options](#options), use: +Use the [`bufferSize`](#basic_options-buffersize) option. +For the [Default Options](#basic_options), use: ```js readlineSync.setDefaultOptions({bufferSize: value}); @@ -1748,10 +1748,10 @@ instead of: readlineSync.setBufferSize(value); ``` -### `noEchoBack` option +### `noEchoBack` option  -Use [`hideEchoBack`](#hideechoback) option instead of it. +Use [`hideEchoBack`](#basic_options-hideechoback) option instead of it. -### `noTrim` option +### `noTrim` option  -Use [`keepWhitespace`](#keepwhitespace) option instead of it. +Use [`keepWhitespace`](#basic_options-keepwhitespace) option instead of it. diff --git a/lib/readline-sync.js b/lib/readline-sync.js index 45c9bc8..f6e5a0f 100644 --- a/lib/readline-sync.js +++ b/lib/readline-sync.js @@ -222,7 +222,7 @@ function _readlineSync(options) { } function readlineExt(options) { - var hostArgs, res = {}, + var hostArgs, res = {}, extMessage, execOptions = {env: process.env, encoding: options.encoding}; if (!extHostPath) { @@ -251,11 +251,12 @@ function readlineExt(options) { try { res.input = childProc.execFileSync(extHostPath, hostArgs, execOptions); } catch (e) { // non-zero exit code - res.error = new Error(DEFAULT_ERR_MSG); + extMessage = e.stderr.trim(); + res.error = new Error(DEFAULT_ERR_MSG + (extMessage ? '\n' + extMessage : '')); res.error.method = 'execFileSync'; res.error.program = extHostPath; res.error.args = hostArgs; - res.error.extMessage = e.stderr.trim(); + res.error.extMessage = extMessage; res.error.exitCode = e.status; res.error.code = e.code; res.error.signal = e.signal; @@ -296,7 +297,7 @@ function _execFileSync(options, execOptions) { return filepath; } - var hostArgs, shellPath, shellArgs, res = {}, exitCode, + var hostArgs, shellPath, shellArgs, res = {}, exitCode, extMessage, pathStdout = getTempfile('readline-sync.stdout'), pathStderr = getTempfile('readline-sync.stderr'), pathExit = getTempfile('readline-sync.exit'), @@ -353,12 +354,12 @@ function _execFileSync(options, execOptions) { 'hex', options.encoding) + decipher.final(options.encoding); } else { - res.error = new Error(DEFAULT_ERR_MSG); + extMessage = fs.readFileSync(pathStderr, {encoding: options.encoding}).trim(); + res.error = new Error(DEFAULT_ERR_MSG + (extMessage ? '\n' + extMessage : '')); res.error.method = '_execFileSync'; res.error.program = shellPath; res.error.args = shellArgs; - res.error.extMessage = - fs.readFileSync(pathStderr, {encoding: options.encoding}).trim(); + res.error.extMessage = extMessage; res.error.exitCode = +exitCode; } diff --git a/package.json b/package.json index c83814a..f618243 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "readline-sync", - "version": "1.2.12", + "version": "1.2.13", "title": "readlineSync", "description": "Synchronous Readline for interactively running to have a conversation with the user via a console(TTY).", "keywords": [