Skip to content

Commit ddd6ee6

Browse files
committed
Release v0.0.1
1 parent 3d7e870 commit ddd6ee6

File tree

5 files changed

+85
-5
lines changed

5 files changed

+85
-5
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@ _Simple utility to parse command line parameters and flags (arguments vector)_
1212
supplied information on the command line and return the flags and parameters
1313
in an easy-to-use structure.
1414

15-
<img src=https://raw.githubusercontent.com/center-key/cli-argv-util/main/screenshot.png
16-
width=800 alt=screenshot>
17-
1815
## A) Setup
1916
Install package for node:
2017
```shell
2118
$ npm install cli-argv-util
2219
```
2320

2421
## B) Usage
22+
Enter the following code in your **bin/cli.js** file
2523
```javascript
2624
import { cliArgvUtil } from 'cli-argv-util';
2725

@@ -30,7 +28,9 @@ const cli = cliArgvUtil.parse(validFlags);
3028
if (cliArgvUtil.invalidFlag)
3129
throw Error(cliArgvUtil.invalidFlagMsg);
3230
```
33-
If you CLI tool is names `my-program` and a user runs in like:
31+
For a real world example, see: [cli.js](https://github.com/center-key/copy-file-util/blob/main/bin/cli.js)
32+
33+
If your CLI tool is named `my-program` and a user runs it like:
3434
```shell
3535
$ my-program file.html --cd=src --no-summary file.png
3636
```

dist/cli-argv-util.d.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//! cli-argv-util v0.0.1 ~~ https://github.com/center-key/cli-argv-util ~~ MIT License
2+
3+
export type StringFlagMap = {
4+
[flag: string]: string | undefined;
5+
};
6+
export type BooleanFlagMap = {
7+
[flag: string]: boolean;
8+
};
9+
export type Result = {
10+
flagMap: StringFlagMap;
11+
flagOn: BooleanFlagMap;
12+
invalidFlag: string | null;
13+
invalidFlagMsg: string | null;
14+
params: string[];
15+
};
16+
declare const cliArgvUtil: {
17+
parse(validFlags: string[]): Result;
18+
};
19+
export { cliArgvUtil };

dist/cli-argv-util.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//! cli-argv-util v0.0.1 ~~ https://github.com/center-key/cli-argv-util ~~ MIT License
2+
3+
const cliArgvUtil = {
4+
parse(validFlags) {
5+
var _a, _b;
6+
const toCamel = (token) => token.replace(/-./g, char => char[1].toUpperCase());
7+
const toEntry = (pair) => [toCamel(pair[0]), pair[1]];
8+
const toPair = (flag) => flag.replace(/^--/, '').split('=');
9+
const args = process.argv.slice(2);
10+
const pairs = args.filter(arg => /^--/.test(arg)).map(toPair);
11+
const flagMap = Object.fromEntries(pairs.map(toEntry));
12+
const onEntries = validFlags.map(flag => [toCamel(flag), toCamel(flag) in flagMap]);
13+
const invalidFlag = (_b = (_a = pairs.find(pair => !validFlags.includes(pair[0]))) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : null;
14+
const helpMsg = '\nValid flags are --' + validFlags.join(' --');
15+
return {
16+
flagMap: flagMap,
17+
flagOn: Object.fromEntries(onEntries),
18+
invalidFlag: invalidFlag,
19+
invalidFlagMsg: invalidFlag ? 'Invalid flag: --' + invalidFlag + helpMsg : null,
20+
params: args.filter((arg) => !/^--/.test(arg)),
21+
};
22+
},
23+
};
24+
export { cliArgvUtil };

dist/cli-argv-util.umd.cjs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//! cli-argv-util v0.0.1 ~~ https://github.com/center-key/cli-argv-util ~~ MIT License
2+
3+
(function (factory) {
4+
if (typeof module === "object" && typeof module.exports === "object") {
5+
var v = factory(require, exports);
6+
if (v !== undefined) module.exports = v;
7+
}
8+
else if (typeof define === "function" && define.amd) {
9+
define(["require", "exports"], factory);
10+
}
11+
})(function (require, exports) {
12+
"use strict";
13+
Object.defineProperty(exports, "__esModule", { value: true });
14+
exports.cliArgvUtil = void 0;
15+
const cliArgvUtil = {
16+
parse(validFlags) {
17+
var _a, _b;
18+
const toCamel = (token) => token.replace(/-./g, char => char[1].toUpperCase());
19+
const toEntry = (pair) => [toCamel(pair[0]), pair[1]];
20+
const toPair = (flag) => flag.replace(/^--/, '').split('=');
21+
const args = process.argv.slice(2);
22+
const pairs = args.filter(arg => /^--/.test(arg)).map(toPair);
23+
const flagMap = Object.fromEntries(pairs.map(toEntry));
24+
const onEntries = validFlags.map(flag => [toCamel(flag), toCamel(flag) in flagMap]);
25+
const invalidFlag = (_b = (_a = pairs.find(pair => !validFlags.includes(pair[0]))) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : null;
26+
const helpMsg = '\nValid flags are --' + validFlags.join(' --');
27+
return {
28+
flagMap: flagMap,
29+
flagOn: Object.fromEntries(onEntries),
30+
invalidFlag: invalidFlag,
31+
invalidFlagMsg: invalidFlag ? 'Invalid flag: --' + invalidFlag + helpMsg : null,
32+
params: args.filter((arg) => !/^--/.test(arg)),
33+
};
34+
},
35+
};
36+
exports.cliArgvUtil = cliArgvUtil;
37+
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cli-argv-util",
3-
"version": "0.0.0",
3+
"version": "0.0.1",
44
"description": "Simple utility to parse command line parameters and flags (arguments vector)",
55
"license": "MIT",
66
"type": "module",

0 commit comments

Comments
 (0)