command-line-basics
Wraps the basic command-line functionality to your package.
It is probably easiest to see it in an example:
- Binary file
- The option definitions (this file defines the schema and format for the CLI arguments; note: it is not necessary to export the chalk templates assuming you even need to use them at all).
- The main app which receives the command line arguments ready for use.
Performs the following:
- Utilizes
update-notifier
to notify the user of any updates of your
package. - By default, will automatically add
--version
/-v
and --help
/-h
flags to the options defined in your targeted file's definitions
(processed by command-line-args
) and sections[1].optionList
(processed
by command-line-usage
). When your users call --help
, these two flags
will be shown there. When your users call --version
, it will output
the current version
of your package.json
). - By default, will automatically add
header
to sections[0]
if not
present (based on the name
in package.json
). - All of
sections
will be passed to command-line-usage
.
Install
npm i -P command-line-basics
Simple usage
After adding your binary file to package.json
, e.g.,
{
"bin": {
"myCliApp": "./bin/index.js"
}
}
...and optionally making the script executable, as with
chmod 0755 ./bin/index.js
, then add the following to that file:
#!/usr/bin/env node
import {cliBasics} from 'command-line-basics';
import mainScript from '../src/index.js';
const optionDefinitions = await cliBasics(
'./src/optionDefinitions.js'
);
if (!optionDefinitions) {
process.exit(0);
}
mainScript(optionDefinitions);
Advanced usage
Except for optionsPath
, the example indicates the defaults:
import {dirname} from 'path';
import {fileURLToPath} from 'url';
import {cliBasics} from 'command-line-basics';
const __dirname = dirname(fileURLToPath(import.meta.url));
const options = await cliBasics({
optionsPath: path.join(process.cwd(), './src/optionDefinitions.js'),
cwd: __dirname,
async notifierCallback (notifier) {
const {
latest, current,
name,
type
} = await notifier.fetchInfo();
console.log('Versions', latest, current);
console.log('Package name', name);
console.log('Current update type', type);
},
options: {
packageJsonPath: path.join(process.cwd(), 'package.json'),
autoAddVersion: true,
autoAddHelp: true,
autoAddHeader: true,
autoAddOptionsHeader: true,
autoAddContent: true,
commandLineArgsOptions: {
},
updateNotifierOptions: {
updateCheckInterval: 1000 * 60 * 60 * 24,
shouldNotifyInNpmScript: false,
distTag: 'latest'
},
updateNotifierNotifyOptions: {
defer: false,
message: '',
isGlobal: defaultsToAutoDetectBoolean,
boxenOptions: {
padding: 1, margin: 1, align: 'center',
borderColor: 'yellow', borderStyle: 'round'
}
}
}
});
if (!options) {
process.exit(0);
}
There is also exported an autoAdd
method which takes the same arguments
and returns the (potentially help
/version
and header
enhanced)
definitions
and sections
. It is also used internally by cliBasics
.
See also
To-dos
- Utility for main files to create a file retrieval/writing function (for
conversions)
- Auto-convert JSON Schema into option defintion structures with jsdoc (and
the converted JSON Schema) enhanced to add CLI properties,
e.g., any alias (
@cli-alias {file} f
) and possibly its typeLabel
.
Default should be deducible.