This repository has been archived by the owner on Apr 21, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for custom help classes (#141)
- Loading branch information
Showing
23 changed files
with
405 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {HelpBase} from '@oclif/plugin-help' | ||
import {Command} from '@oclif/config' | ||
|
||
interface MaybeCompatibleHelp extends HelpBase { | ||
formatCommand?: (command: Command) => string; | ||
command?: (command: Command) => string; | ||
} | ||
|
||
class IncompatibleHelpError extends Error { | ||
message = 'Please implement `formatCommand` in your custom help class.\nSee https://oclif.io/docs/help_classes for more.' | ||
} | ||
|
||
export class HelpCompatibilityWrapper { | ||
inner: MaybeCompatibleHelp | ||
|
||
constructor(inner: MaybeCompatibleHelp) { | ||
this.inner = inner | ||
} | ||
|
||
formatCommand(command: Command) { | ||
if (this.inner.formatCommand) { | ||
return this.inner.formatCommand(command) | ||
} | ||
|
||
if (this.inner.command) { | ||
return command.description + '\n\n' + this.inner.command(command) | ||
} | ||
|
||
throw new IncompatibleHelpError() | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
test/fixtures/cli-with-custom-help-no-format-command/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# cli-with-custom-help | ||
|
||
This file is a test for running `oclif-dev readme` in the presence of | ||
a custom help class. It should use the custom help class to generate | ||
the command documentation below. The test suite resets this file after | ||
each test. | ||
|
||
<!-- toc --> | ||
<!-- tocstop --> | ||
|
||
# Usage | ||
|
||
<!-- usage --> | ||
<!-- usagestop --> | ||
|
||
# Commands | ||
|
||
<!-- commands --> | ||
<!-- commandsstop --> |
11 changes: 11 additions & 0 deletions
11
test/fixtures/cli-with-custom-help-no-format-command/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "cli-with-custom-help-no-format-command", | ||
"files": [ | ||
"/lib" | ||
], | ||
"oclif": { | ||
"commands": "./lib/commands", | ||
"bin": "cli-with-custom-help", | ||
"helpClass": "./lib/help" | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
test/fixtures/cli-with-custom-help-no-format-command/src/commands/hello.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {Command, flags} from '@oclif/command' | ||
|
||
export default class Hello extends Command { | ||
static description = 'describe the command here' | ||
|
||
static examples = [ | ||
`$ cli-with-custom-help hello | ||
hello world from ./src/hello.ts! | ||
`, | ||
] | ||
|
||
static flags = { | ||
help: flags.help({char: 'h'}), | ||
// flag with a value (-n, --name=VALUE) | ||
name: flags.string({char: 'n', description: 'name to print'}), | ||
// flag with no value (-f, --force) | ||
force: flags.boolean({char: 'f'}), | ||
} | ||
|
||
static args = [{name: 'file'}] | ||
|
||
async run() { | ||
const {args, flags} = this.parse(Hello) | ||
|
||
const name = flags.name ?? 'world' | ||
this.log(`hello ${name} from ./src/commands/hello.ts`) | ||
if (args.file && flags.force) { | ||
this.log(`you input --force and --file: ${args.file}`) | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
test/fixtures/cli-with-custom-help-no-format-command/src/help.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {HelpBase} from '@oclif/plugin-help' | ||
import {Command} from '@oclif/config' | ||
|
||
export default class CustomHelp extends HelpBase { | ||
showHelp() { | ||
console.log('TODO: showHelp') | ||
} | ||
|
||
showCommandHelp(command: Command) { | ||
console.log(`Custom help for ${command.id}`) | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
test/fixtures/cli-with-custom-help-no-format-command/src/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {run} from '@oclif/command' |
14 changes: 14 additions & 0 deletions
14
test/fixtures/cli-with-custom-help-no-format-command/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"compilerOptions": { | ||
"declaration": true, | ||
"importHelpers": true, | ||
"module": "commonjs", | ||
"outDir": "lib", | ||
"rootDir": "src", | ||
"strict": true, | ||
"target": "es2017" | ||
}, | ||
"include": [ | ||
"src/**/*" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# cli-with-custom-help | ||
|
||
This file is a test for running `oclif-dev readme` in the presence of | ||
a custom help class. It should use the custom help class to generate | ||
the command documentation below. The test suite resets this file after | ||
each test. | ||
|
||
<!-- toc --> | ||
<!-- tocstop --> | ||
|
||
# Usage | ||
|
||
<!-- usage --> | ||
<!-- usagestop --> | ||
|
||
# Commands | ||
|
||
<!-- commands --> | ||
<!-- commandsstop --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "cli-with-custom-help", | ||
"files": [ | ||
"/lib" | ||
], | ||
"oclif": { | ||
"commands": "./lib/commands", | ||
"bin": "cli-with-custom-help", | ||
"helpClass": "./lib/help" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {Command, flags} from '@oclif/command' | ||
|
||
export default class Hello extends Command { | ||
static description = 'describe the command here' | ||
|
||
static examples = [ | ||
`$ cli-with-custom-help hello | ||
hello world from ./src/hello.ts! | ||
`, | ||
] | ||
|
||
static flags = { | ||
help: flags.help({char: 'h'}), | ||
// flag with a value (-n, --name=VALUE) | ||
name: flags.string({char: 'n', description: 'name to print'}), | ||
// flag with no value (-f, --force) | ||
force: flags.boolean({char: 'f'}), | ||
} | ||
|
||
static args = [{name: 'file'}] | ||
|
||
async run() { | ||
const {args, flags} = this.parse(Hello) | ||
|
||
const name = flags.name ?? 'world' | ||
this.log(`hello ${name} from ./src/commands/hello.ts`) | ||
if (args.file && flags.force) { | ||
this.log(`you input --force and --file: ${args.file}`) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {Help} from '@oclif/plugin-help' | ||
import {Command} from '@oclif/config' | ||
|
||
export default class CustomHelp extends Help { | ||
formatCommand(command: Command) { | ||
return `Custom help for ${command.id}` | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {run} from '@oclif/command' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"compilerOptions": { | ||
"declaration": true, | ||
"importHelpers": true, | ||
"module": "commonjs", | ||
"outDir": "lib", | ||
"rootDir": "src", | ||
"strict": true, | ||
"target": "es2017" | ||
}, | ||
"include": [ | ||
"src/**/*" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# cli-with-custom-help | ||
|
||
This file is a test for running `oclif-dev readme` in the presence of | ||
a custom help class. It should use the custom help class to generate | ||
the command documentation below. The test suite resets this file after | ||
each test. | ||
|
||
<!-- toc --> | ||
<!-- tocstop --> | ||
|
||
# Usage | ||
|
||
<!-- usage --> | ||
<!-- usagestop --> | ||
|
||
# Commands | ||
|
||
<!-- commands --> | ||
<!-- commandsstop --> |
11 changes: 11 additions & 0 deletions
11
test/fixtures/cli-with-old-school-custom-help/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "cli-with-old-school-custom-help", | ||
"files": [ | ||
"/lib" | ||
], | ||
"oclif": { | ||
"commands": "./lib/commands", | ||
"bin": "cli-with-custom-help", | ||
"helpClass": "./lib/help" | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
test/fixtures/cli-with-old-school-custom-help/src/commands/hello.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {Command, flags} from '@oclif/command' | ||
|
||
export default class Hello extends Command { | ||
static description = 'describe the command here' | ||
|
||
static examples = [ | ||
`$ cli-with-custom-help hello | ||
hello world from ./src/hello.ts! | ||
`, | ||
] | ||
|
||
static flags = { | ||
help: flags.help({char: 'h'}), | ||
// flag with a value (-n, --name=VALUE) | ||
name: flags.string({char: 'n', description: 'name to print'}), | ||
// flag with no value (-f, --force) | ||
force: flags.boolean({char: 'f'}), | ||
} | ||
|
||
static args = [{name: 'file'}] | ||
|
||
async run() { | ||
const {args, flags} = this.parse(Hello) | ||
|
||
const name = flags.name ?? 'world' | ||
this.log(`hello ${name} from ./src/commands/hello.ts`) | ||
if (args.file && flags.force) { | ||
this.log(`you input --force and --file: ${args.file}`) | ||
} | ||
} | ||
} |
Oops, something went wrong.