Skip to content

Commit

Permalink
fix: help flag (#532)
Browse files Browse the repository at this point in the history
* fix: help flag

* chore: remove context type

* chore: tests with node latest
  • Loading branch information
mdonnalley committed Oct 19, 2022
1 parent 9d96e5d commit 31d7045
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 28 deletions.
37 changes: 13 additions & 24 deletions README.md
Expand Up @@ -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(),
Expand All @@ -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.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -114,4 +114,4 @@
"pretest": "yarn build --noEmit && tsc -p test --noEmit --skipLibCheck"
},
"types": "lib/index.d.ts"
}
}
4 changes: 3 additions & 1 deletion 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<T = string>(opts: EnumFlagOptions<T, true> & {multiple: true} & ({required: true} | { default: Default<T[]> })): OptionFlag<T[]>
Expand Down Expand Up @@ -36,7 +37,8 @@ export const help = (opts: Partial<BooleanFlag<boolean>> = {}) => {
description: 'Show CLI help.',
...opts,
parse: async (_: any, cmd: Command) => {
(cmd as any)._help()
new Help(cmd.config).showHelp(cmd.argv)
cmd.exit(0)
},
})
}
2 changes: 0 additions & 2 deletions test/errors/handle.test.ts
Expand Up @@ -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('')
Expand All @@ -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`)
Expand Down

0 comments on commit 31d7045

Please sign in to comment.