Skip to content

[Feature Request] User provided Help exit code #2338

@vipcxj

Description

@vipcxj

There is a project argonaut I'm working on; this tool is used to simplify defining command-line arguments in the shell, similar to what cobra does in Go. Here is an example to help illustrate the issue.

This is a script the user wrote: cool.sh

#!/bin/bash
set -e
EVAL_CONTENT=`argonaut bind \
  --flag=flag1 \
  --flag-flag1-default=hello \\
  --flag-flag1-choices=hello,world \
  --flag=flags2 \
  --flag-flags2-multi=true \
  --flag-flags2-choices=one,two,three
  -- "$0" "$@"
`
eval "$EVAL_CONTENT"

Then run

./cool.sh --flags2 one --flags2 three

EVAL_CONTENT will be assigned

FLAG1='hello'
FLAG2='one,three'

So after executing eval, the user can directly use these two environment variables in their own scripts.
If you enter the wrong parameters, for example

./cool.sh --flags2 one --flags2 four

will output

Error: value four for flag flags2 is not in allowed choices [one two three]
Usage:
  cool.sh [flags]

Flags:
--flag1 string          (default "hello")
      --flags2 stringArray
  -h, --help                 help for cool.sh

So far, everything has worked perfectly, until I executed

./cool.sh --help

It's a complete disaster. Because EVAL_CONTENT will become the help message, and eval will obviously throw an error. The workaround I can think of right now is

  1. Redirect the help message output to stderr
  2. Return a nonzero exit code

As for redirecting the help output, I've already found the API, but when dealing with the latter I discovered that cobra doesn't support customizing the help's return value, because help never reaches the Run method and is handled by cobra beforehand, even if I define the help flag myself. I even think this is a bug: if the user defines help themselves, it indicates they want to handle help requests themselves, not have the framework do it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions