From 3e31d9ba559c382a99a4b8d64ccee4d059948766 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ey=C3=BE=C3=B3r=20Magn=C3=BAsson?= Date: Thu, 13 Jun 2019 14:59:29 +0200 Subject: [PATCH] fix(cli): ensure cli exits with code 0 when help/version called --- docs/reference/commands.md | 2 -- garden-service/src/cli/cli.ts | 12 +++++++++--- garden-service/src/commands/options.ts | 11 +++++++---- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/docs/reference/commands.md b/docs/reference/commands.md index 2cacc7c534..7b7aba4635 100644 --- a/docs/reference/commands.md +++ b/docs/reference/commands.md @@ -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. diff --git a/garden-service/src/cli/cli.ts b/garden-service/src/cli/cli.ts index fc2572278d..15ae006210 100644 --- a/garden-service/src/cli/cli.ts +++ b/garden-service/src/cli/cli.ts @@ -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).", diff --git a/garden-service/src/commands/options.ts b/garden-service/src/commands/options.ts index a1ea77fdd4..8bd2499cfa 100644 --- a/garden-service/src/commands/options.ts +++ b/garden-service/src/commands/options.ts @@ -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" @@ -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 { - 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 = >GLOBAL_OPTIONS[optName] + const option = >allOpts[optName] const alias = option.alias ? `-${option.alias}, ` : "" return chalk.green(` ${alias}--${optName} `) }) const helpTexts = sortedOpts.map(optName => { - const option = >GLOBAL_OPTIONS[optName] + const option = >allOpts[optName] let out = option.help let hints = "" if (option.hints) {