Comparing version 1.7.2 to 1.7.3
@@ -29,5 +29,3 @@ /** | ||
if (number) { | ||
value = parseInt(value, 10) | ||
} | ||
if (number) value = parseInt(value, 10) | ||
return { | ||
@@ -53,22 +51,22 @@ value, | ||
* @example | ||
``` | ||
const config = { | ||
title: 't', // program -t Title | ||
open: { // program -o | ||
short: 'o', | ||
boolean: true, | ||
}, | ||
delay: { // program -d 100 | ||
short: 'd', | ||
number: true, | ||
}, | ||
file: { // program File.txt | ||
command: true, | ||
}, | ||
file2: { // program File.txt File2.txt | ||
command: true, | ||
multiple: true, | ||
}, | ||
} | ||
``` | ||
``` | ||
const config = { | ||
title: 't', // program -t Title | ||
open: { // program -o | ||
short: 'o', | ||
boolean: true, | ||
}, | ||
delay: { // program -d 100 | ||
short: 'd', | ||
number: true, | ||
}, | ||
file: { // program File.txt | ||
command: true, | ||
}, | ||
file2: { // program File.txt File2.txt | ||
command: true, | ||
multiple: true, | ||
}, | ||
} | ||
``` | ||
* @param {!Array<string>} [args] Array with arguments to parse. `process.argv` is used by default. It is assumed that user arguments start from the 3rd position. | ||
@@ -82,23 +80,24 @@ * @returns {Object<string, string|Array<string>|boolean|number>} An object with all found values for the configuration request. | ||
let commandFound = !titles.length | ||
const res = Object.keys(config).reduce(({ _argv, ...acc }, key) => { | ||
const config2 = Object.entries(config).reduce((acc, [key, value]) => { | ||
if (typeof value == 'string') { | ||
acc[key] = { short: value } | ||
} else acc[key] = value | ||
return acc | ||
}, {}) | ||
const res = Object.entries(config2).reduce(({ _argv, ...acc }, [key, val]) => { | ||
if (_argv.length == 0 && commandFound) return { _argv, ...acc } | ||
const val = config[key] | ||
let value | ||
if (typeof val == 'string') { | ||
({ value, argv: _argv } = find(_argv, key, val)) | ||
} else { | ||
try { | ||
const { short, boolean, number, command, multiple } = val | ||
if (command && multiple && titles.length) { | ||
value = titles | ||
commandFound = true | ||
} else if (command && titles.length) { | ||
value = titles[0] | ||
commandFound = true | ||
} else { | ||
({ value, argv: _argv } = find(_argv, key, short, boolean, number)) | ||
} | ||
} catch (err) { | ||
return { _argv, ...acc } | ||
try { | ||
const { short, boolean, number, command, multiple } = val | ||
if (command && multiple && titles.length) { | ||
value = titles | ||
commandFound = true | ||
} else if (command && titles.length) { | ||
value = titles[0] | ||
commandFound = true | ||
} else { | ||
({ value, argv: _argv } = find(_argv, key, short, boolean, number)) | ||
} | ||
} catch (err) { | ||
return { _argv, ...acc } | ||
} | ||
@@ -111,2 +110,23 @@ if (value === undefined) return { _argv, ...acc } | ||
}) | ||
const shortReduced = Object.entries(config).reduce((acc, [key, val]) => { | ||
const { short } = val | ||
if (!short) return acc | ||
acc[short] = acc[short] || [] | ||
acc[short].push({ ...val, name: key }) | ||
return acc | ||
}, {}) | ||
Object.entries(res).reduce((acc, [key, val]) => { | ||
const c = config2[key] | ||
if (!c) return acc | ||
const { short, boolean } = c | ||
const otherRecords = shortReduced[short] || [] | ||
otherRecords.forEach(({ name, boolean: b, number: n }) => { | ||
if (name == key) return | ||
// check 4 type here | ||
if (boolean && !b) return | ||
if (n) val = parseInt(val, 10) | ||
else if (!b) val = `${val}` | ||
res[name] = val | ||
}) | ||
}, {}) | ||
return res | ||
@@ -113,0 +133,0 @@ } |
## 7 December 2019 | ||
### [1.7.3](https://github.com/artdecocode/argufy/compare/v1.7.2...v1.7.3) | ||
- [feature] Extract args with the same short. | ||
### [1.7.2](https://github.com/artdecocode/argufy/compare/v1.7.1...v1.7.2) | ||
@@ -4,0 +8,0 @@ |
#!/usr/bin/env node | ||
const fs = require('fs'); | ||
const {readFileSync:t, writeFileSync:u} = fs; | ||
function v(a, c, b) { | ||
const {readFileSync:u, writeFileSync:v} = fs; | ||
function w(a, c, b) { | ||
const d = []; | ||
c.replace(a, (e, ...g) => { | ||
e = g.slice(0, g.length - 2).reduce((h, m, k) => { | ||
c.replace(a, (h, ...l) => { | ||
h = l.slice(0, l.length - 2).reduce((m, g, k) => { | ||
k = b[k]; | ||
if (!k || void 0 === m) { | ||
return h; | ||
if (!k || void 0 === g) { | ||
return m; | ||
} | ||
h[k] = m; | ||
return h; | ||
m[k] = g; | ||
return m; | ||
}, {}); | ||
d.push(e); | ||
d.push(h); | ||
}); | ||
return d; | ||
} | ||
;const w = new RegExp(`${/([^\s>=/]+)/.source}(?:\\s*=\\s*${/(?:"([\s\S]*?)"|'([\s\S]*?)')/.source})?`, "g"), x = new RegExp(`(?:\\s+((?:${w.source}\\s*)*))`); | ||
const z = (a, c) => v(new RegExp(`<(${(Array.isArray(a) ? a : [a]).join("|")})${x.source}?(?:${/\s*\/>/.source}|${/>([\s\S]+?)?<\/\1>/.source})`, "g"), c, "t a v v1 v2 c".split(" ")).map(({t:b, a:d = "", c:e = ""}) => { | ||
;const x = new RegExp(`${/([^\s>=/]+)/.source}(?:\\s*=\\s*${/(?:"([\s\S]*?)"|'([\s\S]*?)')/.source})?`, "g"), y = new RegExp(`(?:\\s+((?:${x.source}\\s*)*))`); | ||
const A = (a, c) => w(new RegExp(`<(${(Array.isArray(a) ? a : [a]).join("|")})${y.source}?(?:${/\s*\/>/.source}|${/>([\s\S]+?)?<\/\1>/.source})`, "g"), c, "t a v v1 v2 c".split(" ")).map(({t:b, a:d = "", c:h = ""}) => { | ||
d = d.replace(/\/$/, "").trim(); | ||
d = y(d); | ||
return {content:e, props:d, tag:b}; | ||
}), y = a => v(w, a, ["key", "val", "def", "f"]).reduce((c, {key:b, val:d}) => { | ||
d = z(d); | ||
return {content:h, props:d, tag:b}; | ||
}), z = a => w(x, a, ["key", "val", "def", "f"]).reduce((c, {key:b, val:d}) => { | ||
if (void 0 === d) { | ||
@@ -32,36 +32,36 @@ return c[b] = !0, c; | ||
}, {}); | ||
function A(a = {usage:{}}) { | ||
const {usage:c = {}, description:b, line:d, example:e} = a; | ||
function B(a = {usage:{}}) { | ||
const {usage:c = {}, description:b, line:d, example:h} = a; | ||
a = Object.keys(c); | ||
const g = Object.values(c), [h] = a.reduce(([l = 0, f = 0], n) => { | ||
const p = c[n].split("\n").reduce((q, r) => r.length > q ? r.length : q, 0); | ||
p > f && (f = p); | ||
n.length > l && (l = n.length); | ||
return [l, f]; | ||
}, []), m = (l, f) => { | ||
f = " ".repeat(f - l.length); | ||
return `${l}${f}`; | ||
const l = Object.values(c), [m] = a.reduce(([f = 0, e = 0], n) => { | ||
const r = c[n].split("\n").reduce((p, q) => q.length > p ? q.length : p, 0); | ||
r > e && (e = r); | ||
n.length > f && (f = n.length); | ||
return [f, e]; | ||
}, []), g = (f, e) => { | ||
e = " ".repeat(e - f.length); | ||
return `${f}${e}`; | ||
}; | ||
a = a.reduce((l, f, n) => { | ||
n = g[n].split("\n"); | ||
f = m(f, h); | ||
const [p, ...q] = n; | ||
f = `${f}\t${p}`; | ||
const r = m("", h); | ||
n = q.map(G => `${r}\t${G}`); | ||
return [...l, f, ...n]; | ||
}, []).map(l => `\t${l}`); | ||
const k = [b, ` ${d || ""}`].filter(l => l ? l.trim() : l).join("\n\n"); | ||
a = a.reduce((f, e, n) => { | ||
n = l[n].split("\n"); | ||
e = g(e, m); | ||
const [r, ...p] = n; | ||
e = `${e}\t${r}`; | ||
const q = g("", m); | ||
n = p.map(t => `${q}\t${t}`); | ||
return [...f, e, ...n]; | ||
}, []).map(f => `\t${f}`); | ||
const k = [b, ` ${d || ""}`].filter(f => f ? f.trim() : f).join("\n\n"); | ||
a = `${k ? `${k}\n` : ""} | ||
${a.join("\n")} | ||
`; | ||
return e ? `${a} | ||
return h ? `${a} | ||
Example: | ||
${e} | ||
${h} | ||
` : a; | ||
} | ||
;const B = (a, c, b, d = !1, e = !1) => { | ||
const g = b ? new RegExp(`^-(${b}|-${c})$`) : new RegExp(`^--${c}$`); | ||
c = a.findIndex(h => g.test(h)); | ||
;const C = (a, c, b, d = !1, h = !1) => { | ||
const l = b ? new RegExp(`^-(${b}|-${c})$`) : new RegExp(`^--${c}$`); | ||
c = a.findIndex(m => l.test(m)); | ||
if (-1 == c) { | ||
@@ -78,5 +78,5 @@ return {argv:a}; | ||
} | ||
e && (b = parseInt(b, 10)); | ||
h && (b = parseInt(b, 10)); | ||
return {value:b, argv:[...a.slice(0, c), ...a.slice(d + 1)]}; | ||
}, C = a => { | ||
}, D = a => { | ||
const c = []; | ||
@@ -91,4 +91,4 @@ for (let b = 0; b < a.length; b++) { | ||
return c; | ||
}, E = () => { | ||
var a = D; | ||
}, F = () => { | ||
var a = E; | ||
return Object.keys(a).reduce((c, b) => { | ||
@@ -101,47 +101,64 @@ const d = a[b]; | ||
d.short && (b = `${b}, -${d.short}`); | ||
let e = d.description; | ||
d.default && (e = `${e}\nDefault: ${d.default}.`); | ||
c[b] = e; | ||
let h = d.description; | ||
d.default && (h = `${h}\nDefault: ${d.default}.`); | ||
c[b] = h; | ||
return c; | ||
}, {}); | ||
}; | ||
const D = {input:{description:"The location of the `arguments.xml` file.", command:!0, multiple:!0, default:"types/arguments.xml"}, output:{description:"The destination where to save output.\nIf not passed, prints to stdout.", short:"o"}, help:{description:"Print the help information and exit.", boolean:!0, short:"h"}, version:{description:"Show the version's number and exit.", boolean:!0, short:"v"}}, F = function(a = {}, c = process.argv) { | ||
const E = {input:{description:"The location of the `arguments.xml` file.", command:!0, multiple:!0, default:"types/arguments.xml"}, output:{description:"The destination where to save output.\nIf not passed, prints to stdout.", short:"o"}, help:{description:"Print the help information and exit.", boolean:!0, short:"h"}, version:{description:"Show the version's number and exit.", boolean:!0, short:"v"}}, G = function(a = {}, c = process.argv) { | ||
[, , ...c] = c; | ||
const b = C(c); | ||
const b = D(c); | ||
c = c.slice(b.length); | ||
let d = !b.length; | ||
return Object.keys(a).reduce(({b:e, ...g}, h) => { | ||
if (0 == e.length && d) { | ||
return {b:e, ...g}; | ||
const h = Object.entries(a).reduce((g, [k, f]) => { | ||
g[k] = "string" == typeof f ? {short:f} : f; | ||
return g; | ||
}, {}), l = Object.entries(h).reduce(({b:g, ...k}, [f, e]) => { | ||
if (0 == g.length && d) { | ||
return {b:g, ...k}; | ||
} | ||
const m = a[h]; | ||
let k; | ||
if ("string" == typeof m) { | ||
({value:k, argv:e} = B(e, h, m)); | ||
} else { | ||
try { | ||
const {short:l, boolean:f, number:n, command:p, multiple:q} = m; | ||
p && q && b.length ? (k = b, d = !0) : p && b.length ? (k = b[0], d = !0) : {value:k, argv:e} = B(e, h, l, f, n); | ||
} catch (l) { | ||
return {b:e, ...g}; | ||
} | ||
let n; | ||
try { | ||
const {short:r, boolean:p, number:q, command:t, multiple:J} = e; | ||
t && J && b.length ? (n = b, d = !0) : t && b.length ? (n = b[0], d = !0) : {value:n, argv:g} = C(g, f, r, p, q); | ||
} catch (r) { | ||
return {b:g, ...k}; | ||
} | ||
return void 0 === k ? {b:e, ...g} : {b:e, ...g, [h]:k}; | ||
}, {b:c}); | ||
}(D), H = F.input || "types/arguments.xml", I = F.output, J = F.version; | ||
const K = a => { | ||
(a = a.map(({name:c, short:b, boolean:d, number:e, command:g, multiple:h, description:m, default:k}) => { | ||
const l = m.replace(/'/g, "\\'").replace(/\n\s*/g, "\\n"); | ||
let f = []; | ||
m && f.push(`description: '${l}'`); | ||
g && f.push("command: true"); | ||
h && f.push("multiple: true"); | ||
d && f.push("boolean: true"); | ||
e && f.push("number: true"); | ||
k && f.push(`default: '${k}'`); | ||
if (!f.length && b) { | ||
return void 0 === n ? {b:g, ...k} : {b:g, ...k, [f]:n}; | ||
}, {b:c}), m = Object.entries(a).reduce((g, [k, f]) => { | ||
const {short:e} = f; | ||
if (!e) { | ||
return g; | ||
} | ||
g[e] = g[e] || []; | ||
g[e].push({...f, name:k}); | ||
return g; | ||
}, {}); | ||
Object.entries(l).reduce((g, [k, f]) => { | ||
const e = h[k]; | ||
if (!e) { | ||
return g; | ||
} | ||
const {short:n, boolean:r} = e; | ||
(m[n] || []).forEach(({name:p, boolean:q, number:t}) => { | ||
p == k || r && !q || (t ? f = parseInt(f, 10) : q || (f = `${f}`), l[p] = f); | ||
}); | ||
}, {}); | ||
return l; | ||
}(E), H = G.input || "types/arguments.xml", I = G.output, K = G.version; | ||
const L = a => { | ||
(a = a.map(({name:c, short:b, boolean:d, number:h, command:l, multiple:m, description:g, default:k}) => { | ||
const f = g.replace(/'/g, "\\'").replace(/\n\s*/g, "\\n"); | ||
let e = []; | ||
g && e.push(`description: '${f}'`); | ||
l && e.push("command: true"); | ||
m && e.push("multiple: true"); | ||
d && e.push("boolean: true"); | ||
h && e.push("number: true"); | ||
k && e.push(`default: '${k}'`); | ||
if (!e.length && b) { | ||
return `'${c}': '${b}'`; | ||
} | ||
if (f.length || b) { | ||
b && f.push(`short: '${b}'`); | ||
if (e.length || b) { | ||
b && e.push(`short: '${b}'`); | ||
} else { | ||
@@ -151,3 +168,3 @@ return `'${c}': { }`; | ||
return `'${c}': { | ||
${f.join(",\n ")}${f.length ? "," : ""} | ||
${e.join(",\n ")}${e.length ? "," : ""} | ||
}`; | ||
@@ -159,37 +176,37 @@ }).join(",\n")) && (a = `${a},`); | ||
}`; | ||
}, M = (a, c, b = "") => { | ||
const d = K(a), e = `argsConfig${b}`, g = `args${b}`; | ||
a = a.map(h => L(h, g)); | ||
return {i:g, h:` | ||
export const ${e} = ${d} | ||
const ${g} = argufy(${e}${c ? `, [process.argv[0], process.argv[1], ...${c}._argv]` : ""}) | ||
}, N = (a, c, b = "") => { | ||
const d = L(a), h = `argsConfig${b}`, l = `args${b}`; | ||
a = a.map(m => M(m, l)); | ||
return {i:l, h:` | ||
export const ${h} = ${d} | ||
const ${l} = argufy(${h}${c ? `, [process.argv[0], process.argv[1], ...${c}._argv]` : ""}) | ||
${a.join("\n\n")} | ||
`}; | ||
}, L = ({name:a, number:c, boolean:b, description:d, default:e, multiple:g}, h = "args") => { | ||
let m = "string"; | ||
c && (m = "number"); | ||
b && (m = "boolean"); | ||
g && (m = "(!Array<string>|string)"); | ||
g = a.replace(/-(.)/g, (l, f) => f.toUpperCase()); | ||
let k = `'${e}'`; | ||
}, M = ({name:a, number:c, boolean:b, description:d, default:h, multiple:l}, m = "args") => { | ||
let g = "string"; | ||
c && (g = "number"); | ||
b && (g = "boolean"); | ||
l && (g = "(!Array<string>|string)"); | ||
l = a.replace(/-(.)/g, (f, e) => e.toUpperCase()); | ||
let k = `'${h}'`; | ||
if (c || b) { | ||
k = e; | ||
k = h; | ||
} | ||
c = void 0 !== e; | ||
c = void 0 !== h; | ||
return `/** | ||
* ${d}${c ? ` Default \`${e}\`.` : ""} | ||
* ${d}${c ? ` Default \`${h}\`.` : ""} | ||
*/ | ||
export const _${g} = /** @type {${m}} */ (${h}['${a}']${c ? ` || ${k}` : ""})`; | ||
export const _${l} = /** @type {${g}} */ (${m}['${a}']${c ? ` || ${k}` : ""})`; | ||
}; | ||
if (F.help) { | ||
const a = E(); | ||
console.log(A({usage:a, description:"Generates the JavaScript file that exports all arguments\nbased on the configuration found in the arguments.xml file.\nhttps://artdecocode.com/argufy/", line:"argufy input [-o output] [-vh]", example:"argufy types/arguments.xml -o src/bin/get-args.js"})); | ||
if (G.help) { | ||
const a = F(); | ||
console.log(B({usage:a, description:"Generates the JavaScript file that exports all arguments\nbased on the configuration found in the arguments.xml file.\nhttps://artdecocode.com/argufy/", line:"argufy input [-o output] [-vh]", example:"argufy types/arguments.xml -o src/bin/get-args.js"})); | ||
process.exit(); | ||
} else { | ||
J && (console.log(require("../package.json").version), process.exit()); | ||
K && (console.log(require("../package.json").version), process.exit()); | ||
} | ||
const N = a => { | ||
a = `${t(a)}`; | ||
a = z("arguments", a); | ||
const O = a => { | ||
a = `${u(a)}`; | ||
a = A("arguments", a); | ||
if (!a.length) { | ||
@@ -199,3 +216,3 @@ throw Error("XML file should contain root arguments element."); | ||
const [{content:c, props:{name:b}}] = a; | ||
a = z("arg", c).map(({content:d, props:{command:e, name:g, "boolean":h, number:m, multiple:k, "short":l, "default":f}}) => ({description:d.trim(), command:e, boolean:h, number:m, multiple:k, short:l, name:g, default:f})); | ||
a = A("arg", c).map(({content:d, props:{command:h, name:l, "boolean":m, number:g, multiple:k, "short":f, "default":e}}) => ({description:d.trim(), command:h, boolean:m, number:g, multiple:k, short:f, name:l, default:e})); | ||
return {g:b, f:a}; | ||
@@ -207,6 +224,6 @@ }; | ||
const c = `import argufy from 'argufy' | ||
${(Array.isArray(H) ? H : [H]).map(b => N(b)).reduce((b, {g:d = "", f:e}) => { | ||
const {h:g, i:h} = M(e, a, d); | ||
a = h; | ||
b.push(g); | ||
${(Array.isArray(H) ? H : [H]).map(b => O(b)).reduce((b, {g:d = "", f:h}) => { | ||
const {h:l, i:m} = N(h, a, d); | ||
a = m; | ||
b.push(l); | ||
return b; | ||
@@ -218,3 +235,3 @@ }, []).join("")} | ||
export const _argv = /** @type {!Array<string>} */ (${a}._argv)`; | ||
I ? (u(I, c), console.log("Output saved to %s", I)) : console.log(c); | ||
I ? (v(I, c), console.log("Output saved to %s", I)) : console.log(c); | ||
} catch (a) { | ||
@@ -221,0 +238,0 @@ process.env.DEBUG ? console.log(a.stack) : console.log(a.message); |
{ | ||
"name": "argufy", | ||
"version": "1.7.2", | ||
"version": "1.7.3", | ||
"description": "Parses Command Line Arguments To Node.JS CLI Programs, Keeps Them In Arguments.xml File To Paste Into README Documentation And Generate Google Closure Compatible Exports.", | ||
@@ -47,3 +47,3 @@ "main": "build", | ||
}, | ||
"homepage": "https://github.com/artdecocode/argufy#readme", | ||
"homepage": "https://www.artd.eco", | ||
"devDependencies": { | ||
@@ -50,0 +50,0 @@ "@artdeco/clean-stack": "^1.1.1", |
@@ -25,6 +25,6 @@ # argufy | ||
- [API](#api) | ||
* [`argufy(config: Config, argv?: string[]): Object`](#argufyconfig-configargv-string-object) | ||
* [`argufy(config: !Config, argv=: !Array<string>): !Object`](#argufyconfig-configargv-arraystring-object) | ||
* [`Config`](#type-config) | ||
* [`Flag`](#type-flag) | ||
* [`reduceUsage(config: Config): Object`](#reduceusageconfig-config-object) | ||
* [`reduceUsage(config: !Config): !Object`](#reduceusageconfig-config-object) | ||
- [Copyright](#copyright) | ||
@@ -300,3 +300,3 @@ | ||
```js | ||
import argufy from 'argufy' | ||
import argufy, { reduceUsage } from 'argufy' | ||
``` | ||
@@ -310,6 +310,8 @@ | ||
### <code><ins>argufy</ins>(</code><sub><br/> `config: Config,`<br/> `argv?: string[],`<br/></sub><code>): <i>Object</i></code> | ||
### <code><ins>argufy</ins>(</code><sub><br/> `config: !Config,`<br/> `argv=: !Array<string>,`<br/></sub><code>): <i>!Object</i></code> | ||
The flags from the arguments will be extracted according to the configuration object and the arguments array. If arguments array is not passed, `process.argv` is used to find arguments. | ||
- <kbd><strong>config*</strong></kbd> <em><code><a href="#type-config" title="The configuration for parsing, where each key is a flag name and values are either strings, or objects with possible properties:">!Config</a></code></em>: The config. | ||
- <kbd>argv</kbd> <em><code>!Array<string></code></em> (optional): The `argv` to parse, by default will be looked up in the process. | ||
The package assumes that the arguments begin from the 3rd position, i.e., standard _Node.JS_ use such as `node example.js --title "Hello World"`, or `example --title "Hello World"` if the program has a shebang and/or is run via the `bin` _package.json_ property. | ||
@@ -373,6 +375,7 @@ | ||
### <code><ins>reduceUsage</ins>(</code><sub><br/> `config: Config,`<br/></sub><code>): <i>Object</i></code> | ||
### <code><ins>reduceUsage</ins>(</code><sub><br/> `config: !Config,`<br/></sub><code>): <i>!Object</i></code> | ||
Given the _Argufy_ config, creates an object that can be passed to [_Usually_](https://artdecocode.com/usually/). Can be used to reduce the config auto-generated and exported from the JavaScript file with the CLI. | ||
- <kbd><strong>config*</strong></kbd> <em><code><a href="#type-config" title="The configuration for parsing, where each key is a flag name and values are either strings, or objects with possible properties:">!Config</a></code></em>: The config. | ||
```js | ||
@@ -379,0 +382,0 @@ import { reduceUsage } from 'argufy' |
102
src/index.js
@@ -29,5 +29,3 @@ /** | ||
if (number) { | ||
value = parseInt(value, 10) | ||
} | ||
if (number) value = parseInt(value, 10) | ||
return { | ||
@@ -53,22 +51,22 @@ value, | ||
* @example | ||
``` | ||
const config = { | ||
title: 't', // program -t Title | ||
open: { // program -o | ||
short: 'o', | ||
boolean: true, | ||
}, | ||
delay: { // program -d 100 | ||
short: 'd', | ||
number: true, | ||
}, | ||
file: { // program File.txt | ||
command: true, | ||
}, | ||
file2: { // program File.txt File2.txt | ||
command: true, | ||
multiple: true, | ||
}, | ||
} | ||
``` | ||
``` | ||
const config = { | ||
title: 't', // program -t Title | ||
open: { // program -o | ||
short: 'o', | ||
boolean: true, | ||
}, | ||
delay: { // program -d 100 | ||
short: 'd', | ||
number: true, | ||
}, | ||
file: { // program File.txt | ||
command: true, | ||
}, | ||
file2: { // program File.txt File2.txt | ||
command: true, | ||
multiple: true, | ||
}, | ||
} | ||
``` | ||
* @param {!Array<string>} [args] Array with arguments to parse. `process.argv` is used by default. It is assumed that user arguments start from the 3rd position. | ||
@@ -82,23 +80,24 @@ * @returns {Object<string, string|Array<string>|boolean|number>} An object with all found values for the configuration request. | ||
let commandFound = !titles.length | ||
const res = Object.keys(config).reduce(({ _argv, ...acc }, key) => { | ||
const config2 = Object.entries(config).reduce((acc, [key, value]) => { | ||
if (typeof value == 'string') { | ||
acc[key] = { short: value } | ||
} else acc[key] = value | ||
return acc | ||
}, {}) | ||
const res = Object.entries(config2).reduce(({ _argv, ...acc }, [key, val]) => { | ||
if (_argv.length == 0 && commandFound) return { _argv, ...acc } | ||
const val = config[key] | ||
let value | ||
if (typeof val == 'string') { | ||
({ value, argv: _argv } = find(_argv, key, val)) | ||
} else { | ||
try { | ||
const { short, boolean, number, command, multiple } = val | ||
if (command && multiple && titles.length) { | ||
value = titles | ||
commandFound = true | ||
} else if (command && titles.length) { | ||
value = titles[0] | ||
commandFound = true | ||
} else { | ||
({ value, argv: _argv } = find(_argv, key, short, boolean, number)) | ||
} | ||
} catch (err) { | ||
return { _argv, ...acc } | ||
try { | ||
const { short, boolean, number, command, multiple } = val | ||
if (command && multiple && titles.length) { | ||
value = titles | ||
commandFound = true | ||
} else if (command && titles.length) { | ||
value = titles[0] | ||
commandFound = true | ||
} else { | ||
({ value, argv: _argv } = find(_argv, key, short, boolean, number)) | ||
} | ||
} catch (err) { | ||
return { _argv, ...acc } | ||
} | ||
@@ -111,2 +110,23 @@ if (value === undefined) return { _argv, ...acc } | ||
}) | ||
const shortReduced = Object.entries(config).reduce((acc, [key, val]) => { | ||
const { short } = val | ||
if (!short) return acc | ||
acc[short] = acc[short] || [] | ||
acc[short].push({ ...val, name: key }) | ||
return acc | ||
}, {}) | ||
Object.entries(res).reduce((acc, [key, val]) => { | ||
const c = config2[key] | ||
if (!c) return acc | ||
const { short, boolean } = c | ||
const otherRecords = shortReduced[short] || [] | ||
otherRecords.forEach(({ name, boolean: b, number: n }) => { | ||
if (name == key) return | ||
// check 4 type here | ||
if (boolean && !b) return | ||
if (n) val = parseInt(val, 10) | ||
else if (!b) val = `${val}` | ||
res[name] = val | ||
}) | ||
}, {}) | ||
return res | ||
@@ -113,0 +133,0 @@ } |
Sorry, the diff of this file is not supported yet
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
66844
911
417