diff --git a/ADVANCED.md b/ADVANCED.md index 60d703a..a37e6fa 100644 --- a/ADVANCED.md +++ b/ADVANCED.md @@ -35,7 +35,9 @@ For basics see [README.md](./README.md) - [Automatically defined values](#automatically-defined-values) - [`.doc`](#parserdoc) - [`.usage`](#parserusage) + - [`.packageJson`](#parserpackagejson) - [`.version`](#parserversion) + - [`.author`](#parserauthor) - [`.license`](#parserlicense) - [`.examples`](#parserexamples) - [`.footer`](#parserfooter) @@ -460,21 +462,47 @@ Basic usage hint. Default value: `"$SCRIPTNAME [OPTIONS]"` +##### `.packageJson` + +The path to the metadata JSON file. + + .packageJson = | | undefined + +If not set `package.json` will be searched for in the same directory as the +main script. + +Default value: `undefined`. + ##### `.version` + Version number. .usage = | | undefined -If this is not defined `-version` will print `"0.0.0"`. +If this is not defined it will be read from the project metadata, if none is +found `"0.0.0"` will be printed by `-version`. Default value: `undefined` +##### `.author` + +Author name/contacts. + + .author = | | undefined + +If not set project metadata is used, if found. + +Default value: `undefined` + ##### `.license` + Short license information. .usage = | | undefined +If not set project metadata is used, if found. + Default value: `undefined` diff --git a/README.md b/README.md index bf03a53..052d234 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ This code is an evolution of that parser. - Option/command value collection - Multiple option prefix support - Reasonable defaults: + - Metadata defaults to `package.json` - `-help` – generate and print help - `-version` – print version - `-quiet` – suppress printing @@ -198,6 +199,11 @@ Metadata: license: 'BSD-3-Clause', ``` +If not set, `.version`, `.author`, `.license` are acquired from `package.json` +located at the same path as the main script. +To explicitly set the path of the JSON file from which metadata is read set +`.packageJson`. + These basic bits of metadata can be referenced in other `-help` sections, for example: ```javascript diff --git a/argv.js b/argv.js index ad56fe5..130bdc6 100644 --- a/argv.js +++ b/argv.js @@ -137,7 +137,9 @@ function(attr, func){ return function(p){ try { var res = require(p - || this.packageJson + || (typeof(this.packageJson) == 'function' ? + this.packageJson() + : this.packageJson) || path.dirname( (require.main || {}).filename || '.') +'/package.json')[attr] diff --git a/package.json b/package.json index 9e88427..c8c7fb3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-argv", - "version": "2.10.2", + "version": "2.10.3", "description": "simple argv parser", "main": "argv.js", "scripts": {