Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed Jun 27, 2024
1 parent 7b584d3 commit e5451e1
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 73 deletions.
8 changes: 4 additions & 4 deletions node_modules/jackspeak/LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ from liability.
## Acceptance

In order to receive this license, you must agree to its
rules. The rules of this license are both obligations
rules. The rules of this license are both obligations
under that agreement and conditions to your license.
You must not do anything with this software that triggers
a rule that you cannot or will not follow.
Expand All @@ -34,7 +34,7 @@ changes, also gets the text of this license or a link to
If anyone notifies you in writing that you have not
complied with [Notices](#notices), you can keep your
license by taking all practical steps to comply within 30
days after the notice. If you do not do so, your license
days after the notice. If you do not do so, your license
ends immediately.

## Patent
Expand All @@ -49,7 +49,7 @@ No contributor can revoke this license.

## No Liability

***As far as the law allows, this software comes as is,
**_As far as the law allows, this software comes as is,
without any warranty or condition, and no contributor
will be liable to anyone for any damages related to this
software or this license, under any kind of legal claim.***
software or this license, under any kind of legal claim._**
106 changes: 77 additions & 29 deletions node_modules/jackspeak/dist/commonjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,13 @@ class Jack {
* an explicit CLI setting.
*/
parse(args = process.argv) {
if (args === process.argv) {
args = args.slice(process._eval !== undefined ? 1 : 2);
}
this.loadEnvDefaults();
const p = this.parseRaw(args);
this.applyDefaults(p);
this.writeEnv(p);
return p;
}
loadEnvDefaults() {
if (this.#envPrefix) {
for (const [field, my] of Object.entries(this.#configSet)) {
const ek = toEnvKey(this.#envPrefix, field);
Expand All @@ -377,6 +381,25 @@ class Jack {
}
}
}
}
applyDefaults(p) {
for (const [field, c] of Object.entries(this.#configSet)) {
if (c.default !== undefined && !(field in p.values)) {
//@ts-ignore
p.values[field] = c.default;
}
}
}
/**
* Only parse the command line arguments passed in.
* Does not strip off the `node script.js` bits, so it must be just the
* arguments you wish to have parsed.
* Does not read from or write to the environment, or set defaults.
*/
parseRaw(args) {
if (args === process.argv) {
args = args.slice(process._eval !== undefined ? 1 : 2);
}
const options = toParseArgsOptionsConfig(this.#configSet);
const result = (0, parse_args_js_1.parseArgs)({
args,
Expand All @@ -393,9 +416,10 @@ class Jack {
for (const token of result.tokens) {
if (token.kind === 'positional') {
p.positionals.push(token.value);
if (this.#options.stopAtPositional) {
if (this.#options.stopAtPositional ||
this.#options.stopAtPositionalTest?.(token.value)) {
p.positionals.push(...args.slice(token.index + 1));
return p;
break;
}
}
else if (token.kind === 'option') {
Expand Down Expand Up @@ -469,12 +493,6 @@ class Jack {
}
}
}
for (const [field, c] of Object.entries(this.#configSet)) {
if (c.default !== undefined && !(field in p.values)) {
//@ts-ignore
p.values[field] = c.default;
}
}
for (const [field, value] of Object.entries(p.values)) {
const valid = this.#configSet[field]?.validate;
const validOptions = this.#configSet[field]?.validOptions;
Expand All @@ -489,7 +507,6 @@ class Jack {
throw new Error(`Invalid value provided for --${field}: ${JSON.stringify(value)}`, { cause });
}
}
this.#writeEnv(p);
return p;
}
/**
Expand Down Expand Up @@ -557,7 +574,7 @@ class Jack {
}
}
}
#writeEnv(p) {
writeEnv(p) {
if (!this.#env || !this.#envPrefix)
return;
for (const [field, value] of Object.entries(p.values)) {
Expand Down Expand Up @@ -912,6 +929,7 @@ class Jack {
...(def.validate ? { validate: def.validate } : {}),
...(def.validOptions ? { validOptions: def.validOptions } : {}),
...(def.default !== undefined ? { default: def.default } : {}),
...(def.hint ? { hint: def.hint } : {}),
},
]));
}
Expand All @@ -925,22 +943,52 @@ class Jack {
exports.Jack = Jack;
// Unwrap and un-indent, so we can wrap description
// strings however makes them look nice in the code.
const normalize = (s, pre = false) => pre ?
// prepend a ZWSP to each line so cliui doesn't strip it.
s
.split('\n')
.map(l => `\u200b${l}`)
.join('\n')
: s
// remove single line breaks, except for lists
.replace(/([^\n])\n[ \t]*([^\n])/g, (_, $1, $2) => !/^[-*]/.test($2) ? `${$1} ${$2}` : `${$1}\n${$2}`)
// normalize mid-line whitespace
.replace(/([^\n])[ \t]+([^\n])/g, '$1 $2')
// two line breaks are enough
.replace(/\n{3,}/g, '\n\n')
// remove any spaces at the start of a line
.replace(/\n[ \t]+/g, '\n')
.trim();
const normalize = (s, pre = false) => {
if (pre)
// prepend a ZWSP to each line so cliui doesn't strip it.
return s
.split('\n')
.map(l => `\u200b${l}`)
.join('\n');
return s
.split(/^\s*```\s*$/gm)
.map((s, i) => {
if (i % 2 === 1) {
if (!s.trim()) {
return `\`\`\`\n\`\`\`\n`;
}
// outdent the ``` blocks, but preserve whitespace otherwise.
const split = s.split('\n');
// throw out the \n at the start and end
split.pop();
split.shift();
const si = split.reduce((shortest, l) => {
/* c8 ignore next */
const ind = l.match(/^\s*/)?.[0] ?? '';
if (ind.length)
return Math.min(ind.length, shortest);
else
return shortest;
}, Infinity);
/* c8 ignore next */
const i = isFinite(si) ? si : 0;
return ('\n```\n' +
split.map(s => `\u200b${s.substring(i)}`).join('\n') +
'\n```\n');
}
return (s
// remove single line breaks, except for lists
.replace(/([^\n])\n[ \t]*([^\n])/g, (_, $1, $2) => !/^[-*]/.test($2) ? `${$1} ${$2}` : `${$1}\n${$2}`)
// normalize mid-line whitespace
.replace(/([^\n])[ \t]+([^\n])/g, '$1 $2')
// two line breaks are enough
.replace(/\n{3,}/g, '\n\n')
// remove any spaces at the start of a line
.replace(/\n[ \t]+/g, '\n')
.trim());
})
.join('\n');
};
// normalize for markdown printing, remove leading spaces on lines
const normalizeMarkdown = (s, pre = false) => {
const n = normalize(s, pre).replace(/\\/g, '\\\\');
Expand Down
6 changes: 3 additions & 3 deletions node_modules/jackspeak/dist/commonjs/parse-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ var __importStar = (this && this.__importStar) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseArgs = void 0;
const util = __importStar(require("util"));
const pv = typeof process === 'object' &&
const pv = (typeof process === 'object' &&
!!process &&
typeof process.version === 'string'
? process.version
typeof process.version === 'string') ?
process.version
: 'v0.0.0';
const pvs = pv
.replace(/^v/, '')
Expand Down
106 changes: 77 additions & 29 deletions node_modules/jackspeak/dist/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,13 @@ export class Jack {
* an explicit CLI setting.
*/
parse(args = process.argv) {
if (args === process.argv) {
args = args.slice(process._eval !== undefined ? 1 : 2);
}
this.loadEnvDefaults();
const p = this.parseRaw(args);
this.applyDefaults(p);
this.writeEnv(p);
return p;
}
loadEnvDefaults() {
if (this.#envPrefix) {
for (const [field, my] of Object.entries(this.#configSet)) {
const ek = toEnvKey(this.#envPrefix, field);
Expand All @@ -369,6 +373,25 @@ export class Jack {
}
}
}
}
applyDefaults(p) {
for (const [field, c] of Object.entries(this.#configSet)) {
if (c.default !== undefined && !(field in p.values)) {
//@ts-ignore
p.values[field] = c.default;
}
}
}
/**
* Only parse the command line arguments passed in.
* Does not strip off the `node script.js` bits, so it must be just the
* arguments you wish to have parsed.
* Does not read from or write to the environment, or set defaults.
*/
parseRaw(args) {
if (args === process.argv) {
args = args.slice(process._eval !== undefined ? 1 : 2);
}
const options = toParseArgsOptionsConfig(this.#configSet);
const result = parseArgs({
args,
Expand All @@ -385,9 +408,10 @@ export class Jack {
for (const token of result.tokens) {
if (token.kind === 'positional') {
p.positionals.push(token.value);
if (this.#options.stopAtPositional) {
if (this.#options.stopAtPositional ||
this.#options.stopAtPositionalTest?.(token.value)) {
p.positionals.push(...args.slice(token.index + 1));
return p;
break;
}
}
else if (token.kind === 'option') {
Expand Down Expand Up @@ -461,12 +485,6 @@ export class Jack {
}
}
}
for (const [field, c] of Object.entries(this.#configSet)) {
if (c.default !== undefined && !(field in p.values)) {
//@ts-ignore
p.values[field] = c.default;
}
}
for (const [field, value] of Object.entries(p.values)) {
const valid = this.#configSet[field]?.validate;
const validOptions = this.#configSet[field]?.validOptions;
Expand All @@ -481,7 +499,6 @@ export class Jack {
throw new Error(`Invalid value provided for --${field}: ${JSON.stringify(value)}`, { cause });
}
}
this.#writeEnv(p);
return p;
}
/**
Expand Down Expand Up @@ -549,7 +566,7 @@ export class Jack {
}
}
}
#writeEnv(p) {
writeEnv(p) {
if (!this.#env || !this.#envPrefix)
return;
for (const [field, value] of Object.entries(p.values)) {
Expand Down Expand Up @@ -904,6 +921,7 @@ export class Jack {
...(def.validate ? { validate: def.validate } : {}),
...(def.validOptions ? { validOptions: def.validOptions } : {}),
...(def.default !== undefined ? { default: def.default } : {}),
...(def.hint ? { hint: def.hint } : {}),
},
]));
}
Expand All @@ -916,22 +934,52 @@ export class Jack {
}
// Unwrap and un-indent, so we can wrap description
// strings however makes them look nice in the code.
const normalize = (s, pre = false) => pre ?
// prepend a ZWSP to each line so cliui doesn't strip it.
s
.split('\n')
.map(l => `\u200b${l}`)
.join('\n')
: s
// remove single line breaks, except for lists
.replace(/([^\n])\n[ \t]*([^\n])/g, (_, $1, $2) => !/^[-*]/.test($2) ? `${$1} ${$2}` : `${$1}\n${$2}`)
// normalize mid-line whitespace
.replace(/([^\n])[ \t]+([^\n])/g, '$1 $2')
// two line breaks are enough
.replace(/\n{3,}/g, '\n\n')
// remove any spaces at the start of a line
.replace(/\n[ \t]+/g, '\n')
.trim();
const normalize = (s, pre = false) => {
if (pre)
// prepend a ZWSP to each line so cliui doesn't strip it.
return s
.split('\n')
.map(l => `\u200b${l}`)
.join('\n');
return s
.split(/^\s*```\s*$/gm)
.map((s, i) => {
if (i % 2 === 1) {
if (!s.trim()) {
return `\`\`\`\n\`\`\`\n`;
}
// outdent the ``` blocks, but preserve whitespace otherwise.
const split = s.split('\n');
// throw out the \n at the start and end
split.pop();
split.shift();
const si = split.reduce((shortest, l) => {
/* c8 ignore next */
const ind = l.match(/^\s*/)?.[0] ?? '';
if (ind.length)
return Math.min(ind.length, shortest);
else
return shortest;
}, Infinity);
/* c8 ignore next */
const i = isFinite(si) ? si : 0;
return ('\n```\n' +
split.map(s => `\u200b${s.substring(i)}`).join('\n') +
'\n```\n');
}
return (s
// remove single line breaks, except for lists
.replace(/([^\n])\n[ \t]*([^\n])/g, (_, $1, $2) => !/^[-*]/.test($2) ? `${$1} ${$2}` : `${$1}\n${$2}`)
// normalize mid-line whitespace
.replace(/([^\n])[ \t]+([^\n])/g, '$1 $2')
// two line breaks are enough
.replace(/\n{3,}/g, '\n\n')
// remove any spaces at the start of a line
.replace(/\n[ \t]+/g, '\n')
.trim());
})
.join('\n');
};
// normalize for markdown printing, remove leading spaces on lines
const normalizeMarkdown = (s, pre = false) => {
const n = normalize(s, pre).replace(/\\/g, '\\\\');
Expand Down
6 changes: 3 additions & 3 deletions node_modules/jackspeak/dist/esm/parse-args.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as util from 'util';
const pv = typeof process === 'object' &&
const pv = (typeof process === 'object' &&
!!process &&
typeof process.version === 'string'
? process.version
typeof process.version === 'string') ?
process.version
: 'v0.0.0';
const pvs = pv
.replace(/^v/, '')
Expand Down
4 changes: 2 additions & 2 deletions node_modules/jackspeak/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jackspeak",
"version": "3.1.2",
"version": "3.4.0",
"description": "A very strict and proper argument parser.",
"tshy": {
"main": true,
Expand Down Expand Up @@ -38,7 +38,7 @@
"presnap": "npm run prepare",
"test": "tap",
"snap": "tap",
"format": "prettier --write . --loglevel warn",
"format": "prettier --write . --log-level warn",
"typedoc": "typedoc --tsconfig .tshy/esm.json ./src/*.ts"
},
"license": "BlueOak-1.0.0",
Expand Down
Loading

0 comments on commit e5451e1

Please sign in to comment.