@jill64/ts-cli
Advanced tools
Comparing version 0.0.0 to 0.0.1
@@ -6,15 +6,19 @@ import { alignmentArguments } from '../utils/alignmentArguments.js'; | ||
const { config } = pickRoute(schema, route); | ||
const generateUsage = (config, route) => [ | ||
name, | ||
route, | ||
'(options)', | ||
...alignmentArguments(config.args).map((x) => `<${x}>`), | ||
...Object.keys('optional' in config ? config.optional ?? {} : {}).map((x) => `<${x}?>`), | ||
...('rest' in config && config.rest | ||
? `<...${config.rest.placeholder}>` | ||
: []) | ||
] | ||
.filter((x) => x) | ||
.join(' '); | ||
const usage = [ | ||
name, | ||
generateUsage(root.config), | ||
...Object.entries(routes) | ||
.filter(([r]) => (typeof route === 'string' ? r.startsWith(route) : true)) | ||
.map(([r, { config }]) => [ | ||
name, | ||
r, | ||
...alignmentArguments(config.args).map((x) => `<${x}>`), | ||
...Object.keys(config.options ?? {}).map((x) => `<${x}?>`), | ||
...('rest' in config && config.rest | ||
? `<...${config.rest.placeholder}>` | ||
: []) | ||
].join(' ')) | ||
.map(([r, { config }]) => generateUsage(config, r)) | ||
].join('\n '); | ||
@@ -31,14 +35,11 @@ const options = Object.entries({ | ||
}) | ||
.map(([code, message]) => [`${code.padEnd(3)}`, message].join(' ')) | ||
.map(([code, message]) => [code.padEnd(3), message].join(' ')) | ||
.join('\n '); | ||
return ` | ||
Usage: | ||
${usage} | ||
Options: | ||
${options} | ||
Return Codes: | ||
${codes} | ||
`; | ||
return [ | ||
usage.trim() ? `Usage:\n ${usage}` : '', | ||
options.trim() ? `Options:\n ${options}` : '', | ||
codes.trim() ? `Return Codes:\n ${codes}` : '' | ||
] | ||
.join('\n\n') | ||
.trim(); | ||
}; |
@@ -17,5 +17,24 @@ import { transform } from '@jill64/transform'; | ||
]); | ||
const routeDepth = route?.split(' ').length ?? 0; | ||
const routed = route ? args.slice(routeDepth) : args; | ||
const { tokens } = parseArgs({ | ||
options, | ||
args: routed, | ||
allowPositionals: true, | ||
tokens: true, | ||
strict: false | ||
}); | ||
const alignedArgs = alignmentArguments(config.args); | ||
const positionals = tokens.filter((t) => t.kind === 'positional'); | ||
const hasRest = 'rest' in config && config.rest && positionals.length > alignedArgs.length; | ||
const restIndex = hasRest | ||
? alignedArgs.length | ||
? (positionals[alignedArgs.length - 1]?.index ?? 0) + 1 | ||
: 0 | ||
: routed.length; | ||
const truncated = routed.slice(0, restIndex); | ||
const rest = routed.slice(restIndex); | ||
const result = parseArgs({ | ||
options, | ||
args, | ||
args: truncated, | ||
allowPositionals: true, | ||
@@ -25,10 +44,5 @@ tokens: true, | ||
}); | ||
const routeDepth = route?.split(' ').length ?? 0; | ||
const alignedArgs = alignmentArguments(config.args); | ||
const param = { | ||
args: (config.args | ||
? Object.fromEntries(alignedArgs.map((k, index) => [ | ||
k, | ||
result.positionals[index + routeDepth] | ||
])) | ||
? Object.fromEntries(alignedArgs.map((k, index) => [k, result.positionals[index]])) | ||
: {}), | ||
@@ -41,10 +55,8 @@ options: (config.options | ||
k, | ||
result.positionals[alignedArgs.length + index + routeDepth] | ||
result.positionals[alignedArgs.length + index] | ||
])) | ||
: {}), | ||
rest: ('rest' in config && config.rest | ||
? result.positionals.slice(alignedArgs.length + routeDepth) | ||
: {}) | ||
rest: (hasRest ? rest : []) | ||
}; | ||
return param; | ||
}; |
export const matchRoute = (routes) => (args) => { | ||
const input = args | ||
.filter((x) => !x.startsWith('-')) | ||
.join(' ') | ||
.trim(); | ||
const input = args.join(' ').trim(); | ||
if (!input) { | ||
@@ -10,5 +7,5 @@ return undefined; | ||
const matched = Object.keys(routes) | ||
.toSorted((a, b) => a.length - b.length) | ||
.find((route) => route.startsWith(input)); | ||
.toSorted((a, b) => b.length - a.length) | ||
.find((route) => input.startsWith(route)); | ||
return matched; | ||
}; |
{ | ||
"name": "@jill64/ts-cli", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"type": "module", | ||
@@ -16,2 +16,3 @@ "files": [ | ||
"scripts": { | ||
"dev": "npx tsx src/command-test/mock.test.ts", | ||
"build": "tsc && npx publint", | ||
@@ -25,3 +26,3 @@ "prepack": "npm run build", | ||
"devDependencies": { | ||
"@jill64/eslint-config-ts": "1.0.7", | ||
"@jill64/eslint-config-ts": "1.0.8", | ||
"@jill64/prettier-config": "1.0.0", | ||
@@ -45,3 +46,3 @@ "@jill64/transform": "1.0.0", | ||
"url": "https://github.com/jill64/ts-cli.git", | ||
"image": "https://opengraph.githubassets.com/df00e132402814de4a1e106711df5da59396e4a92f56f03ca23a1471cc0c722b/jill64/ts-cli" | ||
"image": "https://opengraph.githubassets.com/59e6a4029f25b818e068d082b2969ac08df1cc6c7069522a275332dcbca835c1/jill64/ts-cli" | ||
}, | ||
@@ -48,0 +49,0 @@ "description": "> Easy Node CLI creation with opinionated POSIX-like defaults", |
@@ -197,8 +197,8 @@ <!----- BEGIN GHOST DOCS HEADER -----> | ||
### Subcommands | ||
### Route (Subcommands) | ||
The `add` function defines a subcommand. | ||
The `add` function defines a route. | ||
> [!NOTE] | ||
> Root `options` are inherited by all subcommands. | ||
> Root `options` and `codes` are inherited by all route. | ||
@@ -205,0 +205,0 @@ ```js |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37281
706