From 76d09d765270c6efd343d885d9986ad3e8b3d50f Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Wed, 19 Oct 2022 10:02:17 -0600 Subject: [PATCH 1/3] fix: help flag --- README.md | 37 +++++++++++++------------------------ src/flags.ts | 4 +++- src/interfaces/parser.ts | 7 ++++--- src/parser/index.ts | 2 +- 4 files changed, 21 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 5e21aa54..8e00918b 100644 --- a/README.md +++ b/README.md @@ -21,20 +21,20 @@ The [cli-ux README](./src/cli-ux/README.md) also contains detailed usage example Usage ===== -Without the generator, you can create a simple CLI like this: +We strongly encourage you generate an oclif CLI using the [oclif cli](https://github.com/oclif/oclif). The generator will generate an npm package with `@oclif/core` as a dependency. -**TypeScript** -```js +You can, however, use `@oclif/core` in a standalone script like this: +```typescript #!/usr/bin/env ts-node import * as fs from 'fs' import {Command, Flags} from '@oclif/core' class LS extends Command { + static description = 'List the files in a directory.' static flags = { version: Flags.version(), help: Flags.help(), - // run with --dir= or -d= dir: Flags.string({ char: 'd', default: process.cwd(), @@ -43,34 +43,23 @@ class LS extends Command { async run() { const {flags} = await this.parse(LS) - let files = fs.readdirSync(flags.dir) - for (let f of files) { + const files = fs.readdirSync(flags.dir) + for (const f of files) { this.log(f) } } } -LS.run() -.catch(require('@oclif/core/handle')) +LS.run().then(() => { + require('@oclif/core/flush') +}, () => { + require('@oclif/core/handle') +}) ``` -Then run either of these with: +Then run it like this: ```sh-session -$ ./myscript +$ ts-node myscript.ts ...files in current dir... -$ ./myscript --dir foobar -...files in ./foobar... -$ ./myscript --version -myscript/0.0.0 darwin-x64 node-v9.5.0 -$ ./myscript --help -USAGE - $ @oclif/core - -OPTIONS - -d, --dir=dir [default: /Users/jdickey/src/github.com/oclif/core] - --help show CLI help - --version show CLI version ``` - -See the [generator](https://github.com/oclif/oclif) for all the options you can pass to the command. diff --git a/src/flags.ts b/src/flags.ts index 24433967..6fa1fde9 100644 --- a/src/flags.ts +++ b/src/flags.ts @@ -1,6 +1,7 @@ import {OptionFlag, BooleanFlag, EnumFlagOptions, Default} from './interfaces' import {custom, boolean} from './parser' import Command from './command' +import {Help} from './help' export {boolean, integer, url, directory, file, string, build, option, custom} from './parser' export function _enum(opts: EnumFlagOptions & {multiple: true} & ({required: true} | { default: Default })): OptionFlag @@ -36,7 +37,8 @@ export const help = (opts: Partial> = {}) => { description: 'Show CLI help.', ...opts, parse: async (_: any, cmd: Command) => { - (cmd as any)._help() + new Help(cmd.config).showHelp(cmd.argv) + cmd.exit(0) }, }) } diff --git a/src/interfaces/parser.ts b/src/interfaces/parser.ts index 54090a90..993fa653 100644 --- a/src/interfaces/parser.ts +++ b/src/interfaces/parser.ts @@ -1,4 +1,5 @@ import {AlphabetLowercase, AlphabetUppercase} from './alphabet' +import Command from '../command' import {Config} from './config' export type ParseFn = (input: string) => Promise @@ -171,7 +172,7 @@ export type OptionFlagProps = FlagProps & { multiple?: boolean; } -export type FlagParser = (input: I, context: any, opts: P & OptionFlag) => Promise +export type FlagParser = (input: I, context: Command, opts: P & OptionFlag) => Promise export type FlagBase = FlagProps & { parse: FlagParser; @@ -227,7 +228,7 @@ export type Input = { globalFlags?: FlagInput; args?: ArgInput; strict?: boolean; - context?: any; + context?: Command; '--'?: boolean; } @@ -236,7 +237,7 @@ export interface ParserInput { flags: FlagInput; args: ParserArg[]; strict: boolean; - context: any; + context: Command; '--'?: boolean; } diff --git a/src/parser/index.ts b/src/parser/index.ts index ec4c95ce..c29365cc 100644 --- a/src/parser/index.ts +++ b/src/parser/index.ts @@ -16,7 +16,7 @@ const m = Deps() export async function parse, GFlags extends FlagOutput, TArgs extends { [name: string]: string }>(argv: string[], options: Input): Promise> { const input = { argv, - context: options.context, + context: options.context!, args: (options.args || []).map((a: any) => args.newArg(a as any)), '--': options['--'], flags: { From 4add7f8b00d32cc84a4871b28f550b636d6b9ad6 Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Wed, 19 Oct 2022 10:11:46 -0600 Subject: [PATCH 2/3] chore: remove context type --- src/interfaces/parser.ts | 7 +++---- src/parser/index.ts | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/interfaces/parser.ts b/src/interfaces/parser.ts index 993fa653..54090a90 100644 --- a/src/interfaces/parser.ts +++ b/src/interfaces/parser.ts @@ -1,5 +1,4 @@ import {AlphabetLowercase, AlphabetUppercase} from './alphabet' -import Command from '../command' import {Config} from './config' export type ParseFn = (input: string) => Promise @@ -172,7 +171,7 @@ export type OptionFlagProps = FlagProps & { multiple?: boolean; } -export type FlagParser = (input: I, context: Command, opts: P & OptionFlag) => Promise +export type FlagParser = (input: I, context: any, opts: P & OptionFlag) => Promise export type FlagBase = FlagProps & { parse: FlagParser; @@ -228,7 +227,7 @@ export type Input = { globalFlags?: FlagInput; args?: ArgInput; strict?: boolean; - context?: Command; + context?: any; '--'?: boolean; } @@ -237,7 +236,7 @@ export interface ParserInput { flags: FlagInput; args: ParserArg[]; strict: boolean; - context: Command; + context: any; '--'?: boolean; } diff --git a/src/parser/index.ts b/src/parser/index.ts index c29365cc..ec4c95ce 100644 --- a/src/parser/index.ts +++ b/src/parser/index.ts @@ -16,7 +16,7 @@ const m = Deps() export async function parse, GFlags extends FlagOutput, TArgs extends { [name: string]: string }>(argv: string[], options: Input): Promise> { const input = { argv, - context: options.context!, + context: options.context, args: (options.args || []).map((a: any) => args.newArg(a as any)), '--': options['--'], flags: { From 182dedbf5a2727a46d005b86e37c656d27c0d450 Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Wed, 19 Oct 2022 11:01:58 -0600 Subject: [PATCH 3/3] chore: tests with node latest --- package.json | 2 +- test/errors/handle.test.ts | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/package.json b/package.json index 27b25690..65aea5c6 100644 --- a/package.json +++ b/package.json @@ -114,4 +114,4 @@ "pretest": "yarn build --noEmit && tsc -p test --noEmit --skipLibCheck" }, "types": "lib/index.d.ts" -} \ No newline at end of file +} diff --git a/test/errors/handle.test.ts b/test/errors/handle.test.ts index dda5ce8a..556408ba 100644 --- a/test/errors/handle.test.ts +++ b/test/errors/handle.test.ts @@ -63,7 +63,6 @@ describe('handle', () => { fancy .stdout() .stderr() - .finally(() => delete process.exitCode) .it('hides an exit error', ctx => { handle(new ExitError(0)) expect(ctx.stdout).to.equal('') @@ -79,7 +78,6 @@ describe('handle', () => { .finally(() => { config.errlog = undefined }) - .finally(() => delete process.exitCode) .it('logs when errlog is set', async ctx => { handle(new CLIError('uh oh!')) expect(ctx.stderr).to.equal(` ${x} Error: uh oh!\n`)