config-from-argv
v1.0.5
Published
Command line arg parser
Downloads
2
Readme
config-from-argv
Command-line argument parser for CLI scripts written in Javascript or TypeScript.
Usage
Define a base config:
const baseConfig = {
printExtraLines: false,
inputFile: "input.txt",
leftPaddingAmount: 10,
};
Tell config-from-argv
how to map command-line arguments to your baseConfig
:
import { ConfigToCLIOpt } from "config-from-argv";
const configToCLIOpt: ConfigToCLIOpt<typeof baseConfig> = {
printExtraLines: "-x",
inputFile: "-f_string",
leftPaddingAmount: "-p_number",
};
In the example above:
-x
will setprintExtraLines
to the booleantrue
-f other.txt
will setinputFile
to the string"other.txt"
-p 30
will setleftPaddingAmount
to the number30
Last Step
Call newConfigFromArgv
to get the final configuration:
import { newConfigFromArgv } from "config-from-argv";
const config = newConfigFromArgv(baseConfig, configToCLIOpt, process.argv.slice(2));
Behavior
The behavior of this library is described in src/lib/main.spec.ts
Examples
Note: install project's node modules before trying out the examples:
$ npm install # in project root
- Javascript:
- TypeScript:
- examples/ts/example.ts
- https://github.com/desm/config-from-argv-ts-example
Dependency Graph
Graph showing how this library is structured: