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/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/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/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`)