small improvement, changelog, start documentation for 2.0

This commit is contained in:
Timo Hocker 2020-06-10 22:44:54 +02:00
parent 3fa23c1697
commit 88a35265d0
4 changed files with 42 additions and 59 deletions

View File

@ -1,5 +1,17 @@
# Changelog # Changelog
## 2.0.0
Restructuring to split different Option types and keep specific parameters separate
### Breaking Changes
- new structure
- option 'required' has been removed
- automatic console parameters have been removed
- help page
- quiet switch (interactive prompts can be disabled using the sources option)
## 1.8.0 ## 1.8.0
callback in case an option could not be assigned instead of silently skipping callback in case an option could not be assigned instead of silently skipping

View File

@ -17,45 +17,25 @@ yarn:
## Usage ## Usage
```js ```js
const {InteractiveOptions} = require('@sapphirecode/console-app'); const {
ArrayOption, // arrays made out of numbers, strings and booleans
const reader = new InteractiveOptions([ BooleanOption,
{ FileOption, // paths that exist and are a file
name: 'foo', // name of the option FolderOption, // paths that exist and are a folder
type: 'boolean', // data type NumberOption,
required: true, // require option to be specified (optional) PathOption, // paths that exist in the file system
default: false, // default value (optional) StringOption,
alias: 'f', // shorthand alias in the console (optional) } = require('@sapphirecode/console-app');
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)
preset: [], // preset choices for string and path types (optional)
error: 'wrong input' // message to display when the user gives invalid input
},
]);
const result = await reader.parse();
console.log(result.foo);
``` ```
available data types: ####################################
- string configs: ['json files to search for options'],
- number exit_on_interrupt: true, // exit program when user cancels prompt
- boolean error_callback: (opt, val, err)=>{...} // function to call when an option value could not be read
- 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) the parameter sources specifies which sources should be used. It reads from all,
except config files by default
- 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
sources should be used. It reads from all, except config files by default
config files can import other config files with #include. example: config files can import other config files with #include. example:
@ -68,20 +48,6 @@ config files can import other config files with #include. example:
config files are parsed using [hjson](https://github.com/hjson/hjson-js) 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.
```js
const reader = new InteractiveOptions([], {
args: true,
env: true,
interactive: true,
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
});
```
## License ## License
MIT © Timo Hocker <timo@scode.ovh> MIT © Timo Hocker <timo@scode.ovh>

View File

@ -6,6 +6,7 @@
*/ */
import { TypeValidation } from './TypeValidation/TypeValidation'; import { TypeValidation } from './TypeValidation/TypeValidation';
import { ErrorCallback } from './ErrorCallback';
interface SourceConfig { interface SourceConfig {
console?: boolean, console?: boolean,
@ -16,13 +17,14 @@ interface SourceConfig {
interface Option { interface Option {
name: string; name: string;
required?: boolean;
default?: unknown; default?: unknown;
sources?: SourceConfig; sources?: SourceConfig;
alias?: string; alias?: string;
env?: string; env?: string;
message?: string; message?: string;
error?: string; error?: string;
error_callback?: ErrorCallback;
exit_on_interrupt?: boolean;
} }
class OptionValue { class OptionValue {

View File

@ -1,7 +1,6 @@
import { OptionSource } from '../Sources/OptionSource'; import { OptionSource } from '../Sources/OptionSource';
import { Option, OptionValue } from '../Option'; import { Option, OptionValue } from '../Option';
import { EnvSource } from '../Sources/EnvSource'; import { EnvSource } from '../Sources/EnvSource';
import { ErrorCallback } from '../ErrorCallback';
import { ArgSource } from '../Sources/ArgSource'; import { ArgSource } from '../Sources/ArgSource';
import { ConfigSource } from '../Sources/ConfigSource'; import { ConfigSource } from '../Sources/ConfigSource';
import { TypeValidation } from '../TypeValidation/TypeValidation'; import { TypeValidation } from '../TypeValidation/TypeValidation';
@ -12,26 +11,30 @@ export abstract class BaseOption<T> {
private _config: Option; private _config: Option;
public constructor ( public constructor (
config: Option, config: Option
error_callback?: ErrorCallback,
exit_on_interrupt = true
) { ) {
this._config = config; this._config = config;
const sources = config.sources || {}; const sources = config.sources || {};
if (typeof sources.configs !== 'undefined') const exit_on_interrupt = config.exit_on_interrupt !== false;
this.sources.push (new ConfigSource (sources.configs, error_callback));
if (typeof sources.configs !== 'undefined') {
this.sources.push (new ConfigSource (
sources.configs,
config.error_callback
));
}
if (sources.env !== false) if (sources.env !== false)
this.sources.push (new EnvSource (error_callback)); this.sources.push (new EnvSource (config.error_callback));
if (sources.console !== false) if (sources.console !== false)
this.sources.push (new ArgSource (error_callback)); this.sources.push (new ArgSource (config.error_callback));
if (sources.interactive !== false) { if (sources.interactive !== false) {
this.sources.push (new InteractiveSource ( this.sources.push (new InteractiveSource (
exit_on_interrupt, exit_on_interrupt,
error_callback config.error_callback
)); ));
} }
} }