complete documentation
This commit is contained in:
parent
bc960f632e
commit
089519844f
32
README.md
32
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
|
||||
|
@ -11,7 +11,6 @@ import { ErrorCallback } from './ErrorCallback';
|
||||
interface SourceConfig {
|
||||
console?: boolean,
|
||||
configs?: string[],
|
||||
env?: boolean,
|
||||
interactive?: boolean,
|
||||
}
|
||||
|
||||
|
@ -25,8 +25,7 @@ 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)
|
||||
this.sources.push (new ArgSource (config.error_callback));
|
||||
|
@ -11,10 +11,20 @@ import { OptionSource } from './OptionSource';
|
||||
|
||||
export class EnvSource extends OptionSource {
|
||||
public async parse (opt: Option, val:OptionValue): Promise<void> {
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user