Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: help flag #532

Merged
merged 3 commits into from Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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