Skip to content

Commit

Permalink
fix(cli): ensure cli exits with code 0 when help/version called
Browse files Browse the repository at this point in the history
  • Loading branch information
eysi09 committed Jun 13, 2019
1 parent 358a7b3 commit 3e31d9b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
2 changes: 0 additions & 2 deletions docs/reference/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ The following option flags can be used with any of the CLI commands:

| Argument | Alias | Type | Description |
| -------- | ----- | ---- | ----------- |
| `--version` | `-v` | string | Show the current CLI version.
| `--help` | `-h` | string | Show help
| `--root` | `-r` | string | Override project root directory (defaults to working directory).
| `--silent` | `-s` | boolean | Suppress log output. Same as setting --logger-type=quiet.
| `--env` | `-e` | string | The environment (and optionally namespace) to work against.
Expand Down
12 changes: 9 additions & 3 deletions garden-service/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,21 @@ export const MOCK_CONFIG: ProjectConfig = {
variables: {},
}

export const GLOBAL_OPTIONS = {
"version": new StringParameter({
// The help text for these commands is only displayed when calling `garden options`.
// However, we can't include them with the global options since that causes the CLI
// to exit with code 1 when they're called.
export const HIDDEN_OPTIONS = {
version: new StringParameter({
alias: "v",
help: "Show the current CLI version.",
}),
"help": new StringParameter({
help: new StringParameter({
alias: "h",
help: "Show help",
}),
}

export const GLOBAL_OPTIONS = {
"root": new StringParameter({
alias: "r",
help: "Override project root directory (defaults to working directory).",
Expand Down
11 changes: 7 additions & 4 deletions garden-service/src/commands/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import stringWidth = require("string-width")
import { maxBy, zip } from "lodash"
import * as CliTable from "cli-table3"
import { GLOBAL_OPTIONS } from "../cli/cli"
import { GLOBAL_OPTIONS, HIDDEN_OPTIONS } from "../cli/cli"
import { helpTextMaxWidth } from "../cli/helpers"
import chalk from "chalk"

Expand All @@ -33,19 +33,22 @@ const tableConfig: CliTable.TableConstructorOptions = {
export class OptionsCommand extends Command {
name = "options"
help = "Print global options"
noProject = true

description = "Prints all global options (options that can be applied to any command)."

async action({ log }: CommandParams): Promise<CommandResult> {
const sortedOpts = Object.keys(GLOBAL_OPTIONS).sort()
// Show both global options and hidden commands (version and help) in the output
const allOpts = { ...GLOBAL_OPTIONS, ...HIDDEN_OPTIONS }
const sortedOpts = Object.keys(allOpts).sort()
const optNames = sortedOpts.map(optName => {
const option = <Parameter<any>>GLOBAL_OPTIONS[optName]
const option = <Parameter<any>>allOpts[optName]
const alias = option.alias ? `-${option.alias}, ` : ""
return chalk.green(` ${alias}--${optName} `)
})

const helpTexts = sortedOpts.map(optName => {
const option = <Parameter<any>>GLOBAL_OPTIONS[optName]
const option = <Parameter<any>>allOpts[optName]
let out = option.help
let hints = ""
if (option.hints) {
Expand Down

0 comments on commit 3e31d9b

Please sign in to comment.