From 089519844fc5373acd8c600a82214b9b871c5b88 Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Fri, 12 Jun 2020 14:34:45 +0200 Subject: [PATCH] complete documentation --- README.md | 32 +++++++++++++++++++++----------- lib/Option.ts | 1 - lib/Options/BaseOption.ts | 3 +-- lib/Sources/EnvSource.ts | 20 +++++++++++++++----- 4 files changed, 37 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 56919c7..010b0e8 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # @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 @@ -26,17 +26,27 @@ const { PathOption, // paths that exist in the file system StringOption, } = 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: ```json diff --git a/lib/Option.ts b/lib/Option.ts index 769afe8..5bfa899 100644 --- a/lib/Option.ts +++ b/lib/Option.ts @@ -11,7 +11,6 @@ import { ErrorCallback } from './ErrorCallback'; interface SourceConfig { console?: boolean, configs?: string[], - env?: boolean, interactive?: boolean, } diff --git a/lib/Options/BaseOption.ts b/lib/Options/BaseOption.ts index 19ce00d..1f0fa5e 100644 --- a/lib/Options/BaseOption.ts +++ b/lib/Options/BaseOption.ts @@ -25,8 +25,7 @@ export abstract class BaseOption { )); } - if (sources.env !== false) - this.sources.push (new EnvSource (config.error_callback)); + this.sources.push (new EnvSource (config.error_callback)); if (sources.console !== false) this.sources.push (new ArgSource (config.error_callback)); diff --git a/lib/Sources/EnvSource.ts b/lib/Sources/EnvSource.ts index d943d7b..d9fddf3 100644 --- a/lib/Sources/EnvSource.ts +++ b/lib/Sources/EnvSource.ts @@ -11,10 +11,20 @@ import { OptionSource } from './OptionSource'; export class EnvSource extends OptionSource { public async parse (opt: Option, val:OptionValue): Promise { - if ( - typeof opt.env !== 'undefined' - && typeof process.env[opt.env] !== 'undefined' - ) - await this.assign_arg (opt, val, process.env[opt.env]); + if (typeof opt.env === 'undefined') + return; + + 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]); } }