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
## 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
callback in case an option could not be assigned instead of silently skipping

View File

@ -17,45 +17,25 @@ yarn:
## 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)
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);
const {
ArrayOption, // arrays made out of numbers, strings and booleans
BooleanOption,
FileOption, // paths that exist and are a file
FolderOption, // paths that exist and are a folder
NumberOption,
PathOption, // paths that exist in the file system
StringOption,
} = require('@sapphirecode/console-app');
```
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
configs: ['json files to search for options'],
exit_on_interrupt: true, // exit program when user cancels prompt
error_callback: (opt, val, err)=>{...} // function to call when an option value could not be read
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
sources should be used. It reads from all, except config files by default
the parameter sources specifies which sources should be used. It reads from all,
except config files by default
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)
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
MIT © Timo Hocker <timo@scode.ovh>

View File

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

View File

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