deno-cliffy icon indicating copy to clipboard operation
deno-cliffy copied to clipboard

fix(command): help no longer triggers error text for missing env vars

Open avery-pierce opened this issue 1 year ago • 0 comments

Test command (from #392):

import { Command } from "./command/mod.ts";

await new Command()
  .name("sample-cli")
  .version("0.0.1")
  .env("FOO=<value:string>", "FOO", { required: true })
  .parse(Deno.args);

Before

  Usage:   sample-cli
  Version: 0.0.1     

  Options:

    -h, --help     - Show this help.                            
    -V, --version  - Show the version number for this program.  

  Environment variables:

    FOO  <value>  - FOO  (required)

  error: Missing required environment variable "FOO".

Exit code: 1

After

 Usage:   sample-cli
  Version: 0.0.1     

  Options:

    -h, --help     - Show this help.                            
    -V, --version  - Show the version number for this program.  

  Environment variables:

    FOO  <value>  - FOO  (required)

Exit code: 0

Rationale

The default --help option is treated like any other standalone option, which means that environment vars, flags, and arguments are all processed and validated before execution. A missing environment variable throws an error, which triggers the help text and causes a non-zero exit code.

This PR adds an option to parseEnvVars to ignore missing required env vars, instead of immediately throwing an error. This is done when the parsed input indicates that the user is opening help.

Fixes #392

avery-pierce avatar Aug 01 '22 00:08 avery-pierce