Skip to content

Commit

Permalink
Release v1.2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
dpilafian committed Sep 23, 2023
1 parent ebb2283 commit bd9b73f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
3 changes: 2 additions & 1 deletion dist/cli-argv-util.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! cli-argv-util v1.2.3 ~~ https://github.com/center-key/cli-argv-util ~~ MIT License
//! cli-argv-util v1.2.4 ~~ https://github.com/center-key/cli-argv-util ~~ MIT License

export type StringFlagMap = {
[flag: string]: string | undefined;
Expand All @@ -20,5 +20,6 @@ declare const cliArgvUtil: {
[key: string]: unknown;
}, posix: string): Buffer;
readFolder(folder: string): string[];
unquoteArgs(args: string[]): string[];
};
export { cliArgvUtil };
27 changes: 15 additions & 12 deletions dist/cli-argv-util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! cli-argv-util v1.2.3 ~~ https://github.com/center-key/cli-argv-util ~~ MIT License
//! cli-argv-util v1.2.4 ~~ https://github.com/center-key/cli-argv-util ~~ MIT License

import { execSync } from 'node:child_process';
import fs from 'fs';
Expand All @@ -8,17 +8,7 @@ const cliArgvUtil = {
const toCamel = (token) => token.replace(/-./g, char => char[1].toUpperCase());
const toEntry = (pair) => [toCamel(pair[0]), pair[1]];
const toPair = (flag) => flag.replace(/^--/, '').split('=');
const unquote = (builder, nextArg) => {
const arg = nextArg.replace(/^'/, '').replace(/'$/, '');
const last = builder[1].length - 1;
if (builder[0])
builder[1][last] = builder[1][last] + ' ' + arg;
else
builder[1].push(arg);
const quoteMode = (/^'/.test(nextArg) || builder[0]) && !/'$/.test(nextArg);
return [quoteMode, builder[1]];
};
const args = process.argv.slice(2).reduce(unquote, [false, []])[1];
const args = cliArgvUtil.unquoteArgs(process.argv.slice(2));
const pairs = args.filter(arg => /^--/.test(arg)).map(toPair);
const flagMap = Object.fromEntries(pairs.map(toEntry));
const onEntries = validFlags.map(flag => [toCamel(flag), toCamel(flag) in flagMap]);
Expand All @@ -42,5 +32,18 @@ const cliArgvUtil = {
readFolder(folder) {
return fs.readdirSync(folder, { recursive: true }).map(file => slash(String(file))).sort();
},
unquoteArgs(args) {
const unquote = (builder, nextArg) => {
const arg = nextArg.replace(/^'/, '').replace(/'$/, '');
const last = builder[1].length - 1;
if (builder[0])
builder[1][last] = builder[1][last] + ' ' + arg;
else
builder[1].push(arg);
const quoteMode = (/^'/.test(nextArg) || builder[0]) && !/'$/.test(nextArg);
return [quoteMode, builder[1]];
};
return args.reduce(unquote, [false, []])[1];
},
};
export { cliArgvUtil };
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cli-argv-util",
"version": "1.2.3",
"version": "1.2.4",
"description": "Simple utility to parse command line parameters and flags (arguments vector)",
"license": "MIT",
"type": "module",
Expand All @@ -16,7 +16,10 @@
},
"./": "./dist/"
},
"repository": "github:center-key/cli-argv-util",
"repository": {
"type": "git",
"url": "git+https://github.com/center-key/cli-argv-util.git"
},
"homepage": "https://github.com/center-key/cli-argv-util",
"bugs": "https://github.com/center-key/cli-argv-util/issues",
"docs": "https://github.com/center-key/cli-argv-util#readme",
Expand Down

0 comments on commit bd9b73f

Please sign in to comment.