pulsarctl icon indicating copy to clipboard operation
pulsarctl copied to clipboard

Show help text when positional arguments aren't supplied

Open EronWright opened this issue 5 years ago • 1 comments

Problem When a command expects positional arguments but the user didn't supply any, the following error message is shown:

$ go run ./main.go context set
[✖]  the context name is not specified or the context name is specified more than one

In this situation, it would be useful to show the help text, as would occur if a required flag was't supplied or wasn't parseable.

Suggestion Separate the validation from the execution, to be able to distinguish whether the help text should be shown.

EronWright avatar Jan 16 '20 20:01 EronWright

A simple way is to get the error and print help .

some like:

// SetRunFuncWithNameArg registers a command function with an optional name argument
func (vc *VerbCmd) SetRunFuncWithNameArg(cmd func() error, errMsg string) {
	vc.Command.Run = func(_ *cobra.Command, args []string) {
		vc.NameArg, vc.NameError = GetNameArg(args, errMsg)
		if vc.NameError != nil {
			vc.Command.Help()
		} else {
			err := run(cmd)
			if err != nil {
				vc.Command.Help()
				// ExecErrorHandler(err)
			}
		}

	}
}

And result

root@yh-console:~/lan/pulsarctl# go run main.go context set asd asd 

[✖]  the context name is not specified or the context name is specified more than one
USED FOR:
    Sets a context entry in pulsarconfig, Specifying a name that already exists will merge new fields on top of existing values for those fields.

REQUIRED PERMISSION:
    This command does not need any permission

OUTPUT:
    #normal output
    Set context successful

Usage: pulsarctl context set [flags]

Examples:
    #Sets the user field on the gce context entry without touching other values
    pulsarctl context set [options]



Aliases: set, create

Common flags:
  -s, --admin-service-url string           The admin web service url that pulsarctl connects to.
      --auth-params string                 Authentication parameters are used to configure the authentication provider specified by "AuthPlugin".
                                            Tls example: "tlsCertFile:val1,tlsKeyFile:val2"
                                            Token example: "authParams=file:///path/to/token/file" or "authParams=token:tokenVal"
      --auth-plugin string                 AuthPlugin is used to specify the plugin to use for authentication,
                                            the supported values are "org.apache.pulsar.client.impl.auth.AuthenticationTls"
                                            and "org.apache.pulsar.client.impl.auth.AuthenticationToken"
      --bookie-service-url string          The bookie web service url that pulsarctl connects to. (default "http://localhost:8080")
  -C, --fabulous string                    toggle colorized logs (true,false,fabulous) (default "true")
  -h, --help                               help for this command
      --tls-allow-insecure                 Allow TLS insecure connection
      --tls-cert-file string               File path for TLS cert used for authentication
      --tls-enable-hostname-verification   Enable TLS hostname verification
      --tls-key-file string                File path for TLS key used for authentication
      --tls-trust-cert-path string         Allow TLS trust cert file path
      --token string                       Using the token to authentication
      --token-file string                  Using the token file to authentication
  -v, --verbose int                        set log level, use 0 to silence, 4 for debugging (default 3)

Use 'pulsarctl context set [command] --help' for more information about a command.

liangyuanpeng avatar Jun 18 '20 06:06 liangyuanpeng