complete documentation

This commit is contained in:
Timo Hocker 2020-06-12 14:34:45 +02:00
parent bc960f632e
commit 089519844f
4 changed files with 37 additions and 19 deletions

View File

@ -1,8 +1,8 @@
# @sapphirecode/console-app # @sapphirecode/console-app
version: 1.8.x version: 2.0.x
read parameters from env, console args or interactively read parameters from env, config files, console args or interactively
## Installation ## Installation
@ -26,17 +26,27 @@ const {
PathOption, // paths that exist in the file system PathOption, // paths that exist in the file system
StringOption, StringOption,
} = require('@sapphirecode/console-app'); } = require('@sapphirecode/console-app');
const input = await new BooleanOption({
name: 'foo', // option name used in configs and console arguments
// optional settings:
default: false, // default value
sources: {
configs: [], // config files to read from. none by default
interactive: true, // use interactive prompts
console: true // read from console arguments
// environment is always on if the 'env' option below is specified
},
alias: 'f', // shorthand console argument name
env: 'foo_env', // name of the environment variable to read from
message: 'input foo', // message to display in interactive prompt
error: 'failed to read foo', // message to display when input was invalid
error_callback: (opt, val, err)=>{...}, // function to call when an option value could not be read
exit_on_interrupt: true, // exit program when user cancels the interactive prompt
}).parse();
``` ```
####################################
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 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: config files can import other config files with #include. example:
```json ```json

View File

@ -11,7 +11,6 @@ import { ErrorCallback } from './ErrorCallback';
interface SourceConfig { interface SourceConfig {
console?: boolean, console?: boolean,
configs?: string[], configs?: string[],
env?: boolean,
interactive?: boolean, interactive?: boolean,
} }

View File

@ -25,7 +25,6 @@ export abstract class BaseOption<T> {
)); ));
} }
if (sources.env !== false)
this.sources.push (new EnvSource (config.error_callback)); this.sources.push (new EnvSource (config.error_callback));
if (sources.console !== false) if (sources.console !== false)

View File

@ -11,10 +11,20 @@ import { OptionSource } from './OptionSource';
export class EnvSource extends OptionSource { export class EnvSource extends OptionSource {
public async parse (opt: Option, val:OptionValue): Promise<void> { public async parse (opt: Option, val:OptionValue): Promise<void> {
if ( if (typeof opt.env === 'undefined')
typeof opt.env !== 'undefined' return;
&& typeof process.env[opt.env] !== 'undefined'
) if (typeof process.env[opt.env] === 'undefined') {
if (typeof this.error_callback !== 'undefined') {
this.error_callback (
opt.name,
null,
new Error ('environment variable does not exist')
);
}
return;
}
await this.assign_arg (opt, val, process.env[opt.env]); await this.assign_arg (opt, val, process.env[opt.env]);
} }
} }