stdio icon indicating copy to clipboard operation
stdio copied to clipboard

Defaul value support

Open gabrielthomasjacobs opened this issue 3 years ago • 0 comments

Added optional AskConfig param defaultIndex. Used to specify the Index of the option (from options param) to be made the default.

Properly handles when index is defined and options isn't.

example question with config: const foo = await ask('Would you like some ice cream?, {options: ["yes", "no"], default: 0});

which will produce:

Would you like some ice cream? (default yes): [yes/no]

Pressing enter without typing an answer will automatically select the default.

typing incorrectly, such as 'yws', or a non-answer will still register as failure.

I tested colors/underlining, which looks better, but I didn't feel that it would be consistent across environments (powershell, cmd, bash) and systems, so I went for the (default {option}) approach.

The index method is the simplest way (I could think of) to implement. Additionally, it makes it very simple for longer answers.

e.g. you could do: const foo = await ask('Default File Location?, {options: ["System Default", "Specify Custom Location"], default: 0});

which will produce: Default File Location? (default System Default) [System Default/Specify Custom Location]

specifying the default as an index of the option is much easier than either storing the value as a variable or ensuring the strings match.

gabrielthomasjacobs avatar Jul 02 '21 05:07 gabrielthomasjacobs