console-app/README.md

88 lines
2.3 KiB
Markdown
Raw Normal View History

2020-05-15 16:53:45 +02:00
# @sapphirecode/console-app
2020-05-28 09:20:53 +02:00
version: 1.8.x
2020-05-15 16:53:45 +02:00
read parameters from env, console args or interactively
## Installation
npm:
> npm i --save @sapphirecode/console-app
yarn:
> yarn add @sapphirecode/console-app
## Usage
```js
const {InteractiveOptions} = require('@sapphirecode/console-app');
const reader = new InteractiveOptions([
{
name: 'foo', // name of the option
type: 'boolean', // data type
required: true, // require option to be specified (optional)
default: false, // default value (optional)
alias: 'f', // shorthand alias in the console (optional)
env: 'fooenv', // environment variable to read from (optional)
description: 'the switch foo', // description in the help page (optional)
message: 'should foo be true?', // message when asking interactively (optional)
2020-05-27 15:11:40 +02:00
preset: [], // preset choices for string and path types (optional)
error: 'wrong input' // message to display when the user gives invalid input
2020-05-15 16:53:45 +02:00
},
]);
const result = await reader.parse();
console.log(result.foo);
```
available data types:
- string
- number
- boolean
- path: expects a path that exists
- file: expects a path that exists and is a file
- folder: expects a path that exists and is a folder
- array: arrays made out of strings, numbers and booleans
the console reader automatically adds the options --help (-h) and --quiet (-q)
- help: shows the yargs help screen
- quiet: prevents interactive queries and throws an error when not all required
parameters are specified
the reader can also be constructed with additional options that specify which
2020-05-18 19:18:02 +02:00
sources should be used. It reads from all, except config files by default
2020-05-15 16:53:45 +02:00
2020-05-27 09:55:52 +02:00
config files can import other config files with #include. example:
```json
#include base.json
{
"foo": "bar"
}
```
2020-05-27 17:39:32 +02:00
config files are parsed using [hjson](https://github.com/hjson/hjson-js)
the option exit_on_interrupt determines whether an error should be thrown or the
process should exit when the user presses control + c in an interactive prompt.
2020-05-22 11:50:07 +02:00
2020-05-15 16:53:45 +02:00
```js
const reader = new InteractiveOptions([], {
args: true,
env: true,
interactive: true,
2020-05-22 11:50:07 +02:00
configs: ['json files to search for options'],
exit_on_interrupt: true, // exit when user cancels prompt
error_callback: (opt, val, err)=>{...} // function to call when an option value could not be read
2020-05-15 16:53:45 +02:00
});
```
## License
MIT © Timo Hocker <timo@scode.ovh>