cobra

Types from the Go package cobra (a library PocketBase builds on), as JavaScript sees them: they turn up as values of PocketBase's types and of hook events.

cobra.ShellCompDirective

ShellCompDirective is a bit map representing the different behaviors the shell can be instructed to have once completions have been provided.

Has everything Number has.

cobra.Command

Command is just that, a command for your application. E.g. 'go run ...' - 'run' is the command. Cobra requires you to define the usage and description as part of your command definition to ensure usability.

Member Type
genBashCompletion() (w: io.Writer): void GenBashCompletion generates bash completion file and writes to the passed writer.
genBashCompletionFile() (filename: string): void GenBashCompletionFile generates bash completion file.
genBashCompletionFileV2() (filename: string, includeDesc: boolean): void GenBashCompletionFileV2 generates Bash completion version 2.
genBashCompletionV2() (w: io.Writer, includeDesc: boolean): void GenBashCompletionV2 generates Bash completion file version 2 and writes it to the passed writer.
use string Use is the one-line usage message. Recommended syntax is as follows:
aliases Array<string> Aliases is an array of aliases that can be used instead of the first word in Use.
suggestFor Array<string> SuggestFor is an array of command names for which this command will be suggested - similar to aliases but only suggests.
short string Short is the short description shown in the 'help' output.
groupID string The group id under which this subcommand is grouped in the 'help' output of its parent.
long string Long is the long message shown in the 'help ' output.
example string Example is examples of how to use the command.
validArgs Array<Completion> ValidArgs is list of all valid non-flag arguments that are accepted in shell completions
validArgsFunction CompletionFunc ValidArgsFunction is an optional function that provides valid non-flag arguments for shell completion. It is a dynamic version of using ValidArgs. Only one of ValidArgs and ValidArgsFunction can be used for a command.
args PositionalArgs Expected arguments
argAliases Array<string> ArgAliases is List of aliases for ValidArgs. These are not suggested to the user in the shell completion, but accepted if entered manually.
bashCompletionFunction string BashCompletionFunction is custom bash functions used by the legacy bash autocompletion generator. For portability with other shells, it is recommended to instead use ValidArgsFunction
deprecated string Deprecated defines, if this command is deprecated and should print this string when used.
annotations { [key: string]: any } Annotations are key/value pairs that can be used by applications to identify or group commands or set special options.
version string Version defines the version for this command. If this value is non-empty and the command does not define a "version" flag, a "version" boolean flag will be added to the command and, if specified, will print content of the "Version" variable. A shorthand "v" flag will also be added if the command does not define one.
persistentPreRun (cmd: Command, args: Array<string>) => void The *Run functions are executed in the following order:
persistentPreRunE (cmd: Command, args: Array<string>) => void PersistentPreRunE: PersistentPreRun but returns an error.
preRun (cmd: Command, args: Array<string>) => void PreRun: children of this command will not inherit.
preRunE (cmd: Command, args: Array<string>) => void PreRunE: PreRun but returns an error.
run (cmd: Command, args: Array<string>) => void Run: Typically the actual work function. Most commands will only implement this.
runE (cmd: Command, args: Array<string>) => void RunE: Run but returns an error.
postRun (cmd: Command, args: Array<string>) => void PostRun: run after the Run command.
postRunE (cmd: Command, args: Array<string>) => void PostRunE: PostRun but returns an error.
persistentPostRun (cmd: Command, args: Array<string>) => void PersistentPostRun: children of this command will inherit and execute after PostRun.
persistentPostRunE (cmd: Command, args: Array<string>) => void PersistentPostRunE: PersistentPostRun but returns an error.
fParseErrWhitelist FParseErrWhitelist FParseErrWhitelist flag parse errors to be ignored
completionOptions CompletionOptions CompletionOptions is a set of options to control the handling of shell completion
traverseChildren boolean TraverseChildren parses flags on all parents before executing child command.
hidden boolean Hidden defines, if this command is hidden and should NOT show up in the list of available commands.
silenceErrors boolean SilenceErrors is an option to quiet errors down stream.
silenceUsage boolean SilenceUsage is an option to silence usage when an error occurs.
disableFlagParsing boolean DisableFlagParsing disables the flag parsing. If this is true all flags will be passed to the command as arguments.
disableAutoGenTag boolean DisableAutoGenTag defines, if gen tag ("Auto generated by spf13/cobra...") will be printed by generating docs for this command.
disableFlagsInUseLine boolean DisableFlagsInUseLine will disable the addition of [flags] to the usage line of a command when printing help or generating docs
disableSuggestions boolean DisableSuggestions disables the suggestions based on Levenshtein distance that go along with 'unknown command' messages.
suggestionsMinimumDistance number SuggestionsMinimumDistance defines minimum levenshtein distance to display suggestions. Must be > 0.
context() (): context.Context Context returns underlying command context. If command was executed with ExecuteContext or the context was set with SetContext, the previously set context will be returned. Otherwise, nil is returned.
setContext() (ctx: context.Context): void SetContext sets context for the command. This context will be overwritten by Command.ExecuteContext or Command.ExecuteContextC.
setArgs() (a: Array<string>): void SetArgs sets arguments for the command. It is set to os.Args[1:] by default, if desired, can be overridden particularly useful when testing.
setOutput() (output: io.Writer): void SetOutput sets the destination for usage and error messages. If output is nil, os.Stderr is used.
setOut() (newOut: io.Writer): void SetOut sets the destination for usage messages. If newOut is nil, os.Stdout is used.
setErr() (newErr: io.Writer): void SetErr sets the destination for error messages. If newErr is nil, os.Stderr is used.
setIn() (newIn: io.Reader): void SetIn sets the source for input data If newIn is nil, os.Stdin is used.
setUsageFunc() (f: (_arg0: Command) => void): void SetUsageFunc sets usage function. Usage can be defined by application.
setUsageTemplate() (s: string): void SetUsageTemplate sets usage template. Can be defined by Application.
setFlagErrorFunc() (f: (_arg0: Command, _arg1: Error) => void): void SetFlagErrorFunc sets a function to generate an error when flag parsing fails.
setHelpFunc() (f: (_arg0: Command, _arg1: Array<string>) => void): void SetHelpFunc sets help function. Can be defined by Application.
setHelpCommand() (cmd: Command): void SetHelpCommand sets help command.
setHelpCommandGroupID() (groupID: string): void SetHelpCommandGroupID sets the group id of the help command.
setCompletionCommandGroupID() (groupID: string): void SetCompletionCommandGroupID sets the group id of the completion command.
setHelpTemplate() (s: string): void SetHelpTemplate sets help template to be used. Application can use it to set custom template.
setVersionTemplate() (s: string): void SetVersionTemplate sets version template to be used. Application can use it to set custom template.
setErrPrefix() (s: string): void SetErrPrefix sets error message prefix to be used. Application can use it to set custom prefix.
setGlobalNormalizationFunc() (n: (f: any, name: string) => any): void SetGlobalNormalizationFunc sets a normalization function to all flag sets and also to child commands. The user should not have a cyclic dependency on commands.
outOrStdout() (): io.Writer OutOrStdout returns output to stdout.
outOrStderr() (): io.Writer OutOrStderr returns output to stderr
errOrStderr() (): io.Writer ErrOrStderr returns output to stderr
inOrStdin() (): io.Reader InOrStdin returns input to stdin
usageFunc() (): (_arg0: Command) => void UsageFunc returns either the function set by SetUsageFunc for this command or a parent, or it returns a default usage function.
usage() (): void Usage puts out the usage for the command. Used when a user provides invalid input. Can be defined by user by overriding UsageFunc.
helpFunc() (): (_arg0: Command, _arg1: Array<string>) => void HelpFunc returns either the function set by SetHelpFunc for this command or a parent, or it returns a function with default help behavior.
help() (): void Help puts out the help for the command. Used when a user calls help [command]. Can be defined by user by overriding HelpFunc.
usageString() (): string UsageString returns usage string.
flagErrorFunc() (): (_arg0: Command, _arg1: Error) => void FlagErrorFunc returns either the function set by SetFlagErrorFunc for this command or a parent, or it returns a function which returns the original error.
usagePadding() (): number UsagePadding return padding for the usage.
commandPathPadding() (): number CommandPathPadding return padding for the command path.
namePadding() (): number NamePadding returns padding for the name.
usageTemplate() (): string UsageTemplate returns usage template for the command. This function is kept for backwards-compatibility reasons.
helpTemplate() (): string HelpTemplate return help template for the command. This function is kept for backwards-compatibility reasons.
versionTemplate() (): string VersionTemplate return version template for the command. This function is kept for backwards-compatibility reasons.
errPrefix() (): string ErrPrefix return error message prefix for the command
find() (args: Array<string>): [(Command), Array<string>] Find the target command given the args and command tree Meant to be run on the highest node. Only searches down.
traverse() (args: Array<string>): [(Command), Array<string>] Traverse the command tree to find the command, and parse args for each parent.
suggestionsFor() (typedName: string): Array<string> SuggestionsFor provides suggestions for the typedName.
visitParents() (fn: (_arg0: Command) => void): void VisitParents visits all parents of the command and invokes fn on each parent.
root() (): (Command) Root finds root command.
argsLenAtDash() (): number ArgsLenAtDash will return the length of c.Flags().Args at the moment when a -- was found during args parsing.
executeContext() (ctx: context.Context): void ExecuteContext is the same as Execute(), but sets the ctx on the command. Retrieve ctx by calling cmd.Context() inside your *Run lifecycle or ValidArgs functions.
execute() (): void Execute uses the args (os.Args[1:] by default) and run through the command tree finding appropriate matches for commands and then corresponding flags.
executeContextC() (ctx: context.Context): (Command) ExecuteContextC is the same as ExecuteC(), but sets the ctx on the command. Retrieve ctx by calling cmd.Context() inside your *Run lifecycle or ValidArgs functions.
executeC() (): (Command) ExecuteC executes the command.
validateArgs() (args: Array<string>): void
validateRequiredFlags() (): void ValidateRequiredFlags validates all required flags are present and returns an error otherwise
initDefaultHelpFlag() (): void InitDefaultHelpFlag adds default help flag to c. It is called automatically by executing the c or by calling help and usage. If c already has help flag, it will do nothing.
initDefaultVersionFlag() (): void InitDefaultVersionFlag adds default version flag to c. It is called automatically by executing the c. If c already has a version flag, it will do nothing. If c.Version is empty, it will do nothing.
initDefaultHelpCmd() (): void InitDefaultHelpCmd adds default help command to c. It is called automatically by executing the c or by calling help and usage. If c already has help command or c has no subcommands, it will do nothing.
resetCommands() (): void ResetCommands delete parent, subcommand and help command from c.
commands() (): Array<(Command | undefined)> Commands returns a sorted slice of child commands.
addCommand() (...cmds: (Command | undefined)[]): void AddCommand adds one or more commands to this parent command.
groups() (): Array<(Group | undefined)> Groups returns a slice of child command groups.
allChildCommandsHaveGroup() (): boolean AllChildCommandsHaveGroup returns if all subcommands are assigned to a group
containsGroup() (groupID: string): boolean ContainsGroup return if groupID exists in the list of command groups.
addGroup() (...groups: (Group | undefined)[]): void AddGroup adds one or more command groups to this parent command.
removeCommand() (...cmds: (Command | undefined)[]): void RemoveCommand removes one or more commands from a parent command.
print() (...i: { }[]): void Print is a convenience method to Print to the defined output, fallback to Stderr if not set.
println() (...i: { }[]): void Println is a convenience method to Println to the defined output, fallback to Stderr if not set.
printf() (format: string, ...i: { }[]): void Printf is a convenience method to Printf to the defined output, fallback to Stderr if not set.
printErr() (...i: { }[]): void PrintErr is a convenience method to Print to the defined Err output, fallback to Stderr if not set.
printErrln() (...i: { }[]): void PrintErrln is a convenience method to Println to the defined Err output, fallback to Stderr if not set.
printErrf() (format: string, ...i: { }[]): void PrintErrf is a convenience method to Printf to the defined Err output, fallback to Stderr if not set.
commandPath() (): string CommandPath returns the full path to this command.
displayName() (): string DisplayName returns the name to display in help text. Returns command Name() If CommandDisplayNameAnnoation is not set
useLine() (): string UseLine puts out the full usage for a given command (including parents).
debugFlags() (): void DebugFlags used to determine which flags have been assigned to which commands and which persist.
name() (): string Name returns the command's name: the first word in the use line.
hasAlias() (s: string): boolean HasAlias determines if a given string is an alias of the command.
calledAs() (): string CalledAs returns the command name or alias that was used to invoke this command or an empty string if the command has not been called.
nameAndAliases() (): string NameAndAliases returns a list of the command name and all aliases
hasExample() (): boolean HasExample determines if the command has example.
runnable() (): boolean Runnable determines if the command is itself runnable.
hasSubCommands() (): boolean HasSubCommands determines if the command has children commands.
isAvailableCommand() (): boolean IsAvailableCommand determines if a command is available as a non-help command (this includes all non deprecated/hidden commands).
isAdditionalHelpTopicCommand() (): boolean IsAdditionalHelpTopicCommand determines if a command is an additional help topic command; additional help topic command is determined by the fact that it is NOT runnable/hidden/deprecated, and has no sub commands that are runnable/hidden/deprecated. Concrete example: https://github.com/spf13/cobra/issues/393#issuecomment-282741924.
hasHelpSubCommands() (): boolean HasHelpSubCommands determines if a command has any available 'help' sub commands that need to be shown in the usage/help default template under 'additional help topics'.
hasAvailableSubCommands() (): boolean HasAvailableSubCommands determines if a command has available sub commands that need to be shown in the usage/help default template under 'available commands'.
hasParent() (): boolean HasParent determines if the command is a child command.
globalNormalizationFunc() (): (f: any, name: string) => any GlobalNormalizationFunc returns the global normalization function or nil if it doesn't exist.
flags() (): (any) Flags returns the complete FlagSet that applies to this command (local and persistent declared here and by all parents).
localNonPersistentFlags() (): (any) LocalNonPersistentFlags are flags specific to this command which will NOT persist to subcommands. This function does not modify the flags of the current command, it's purpose is to return the current state.
localFlags() (): (any) LocalFlags returns the local FlagSet specifically set in the current command. This function does not modify the flags of the current command, it's purpose is to return the current state.
inheritedFlags() (): (any) InheritedFlags returns all flags which were inherited from parent commands. This function does not modify the flags of the current command, it's purpose is to return the current state.
nonInheritedFlags() (): (any) NonInheritedFlags returns all flags which were not inherited from parent commands. This function does not modify the flags of the current command, it's purpose is to return the current state.
persistentFlags() (): (any) PersistentFlags returns the persistent FlagSet specifically set in the current command.
resetFlags() (): void ResetFlags deletes all flags from command.
hasFlags() (): boolean HasFlags checks if the command contains any flags (local plus persistent from the entire structure).
hasPersistentFlags() (): boolean HasPersistentFlags checks if the command contains persistent flags.
hasLocalFlags() (): boolean HasLocalFlags checks if the command has flags specifically declared locally.
hasInheritedFlags() (): boolean HasInheritedFlags checks if the command has flags inherited from its parent command.
hasAvailableFlags() (): boolean HasAvailableFlags checks if the command contains any flags (local plus persistent from the entire structure) which are not hidden or deprecated.
hasAvailablePersistentFlags() (): boolean HasAvailablePersistentFlags checks if the command contains persistent flags which are not hidden or deprecated.
hasAvailableLocalFlags() (): boolean HasAvailableLocalFlags checks if the command has flags specifically declared locally which are not hidden or deprecated.
hasAvailableInheritedFlags() (): boolean HasAvailableInheritedFlags checks if the command has flags inherited from its parent command which are not hidden or deprecated.
flag() (name: string): (any) Flag climbs up the command tree looking for matching flag.
parseFlags() (args: Array<string>): void ParseFlags parses persistent flag tree and local flags.
parent() (): (Command) Parent returns a commands parent command.
registerFlagCompletionFunc() (flagName: string, f: CompletionFunc): void RegisterFlagCompletionFunc should be called to register a function to provide completion for a flag.
getFlagCompletionFunc() (flagName: string): [CompletionFunc, boolean] GetFlagCompletionFunc returns the completion function for the given flag of the command, if available.
initDefaultCompletionCmd() (...args: string[]): void InitDefaultCompletionCmd adds a default 'completion' command to c. This function will do nothing if any of the following is true: 1- the feature has been explicitly disabled by the program, 2- c has no subcommands (to avoid creating one), 3- c already has a 'completion' command provided by the program.
genFishCompletion() (w: io.Writer, includeDesc: boolean): void GenFishCompletion generates fish completion file and writes to the passed writer.
genFishCompletionFile() (filename: string, includeDesc: boolean): void GenFishCompletionFile generates fish completion file.
markFlagsRequiredTogether() (...flagNames: string[]): void MarkFlagsRequiredTogether marks the given flags with annotations so that Cobra errors if the command is invoked with a subset (but not all) of the given flags.
markFlagsOneRequired() (...flagNames: string[]): void MarkFlagsOneRequired marks the given flags with annotations so that Cobra errors if the command is invoked without at least one flag from the given set of flags.
markFlagsMutuallyExclusive() (...flagNames: string[]): void MarkFlagsMutuallyExclusive marks the given flags with annotations so that Cobra errors if the command is invoked with more than one flag from the given set of flags.
validateFlagGroups() (): void ValidateFlagGroups validates the mutuallyExclusive/oneRequired/requiredAsGroup logic and returns the first error encountered.
genPowerShellCompletionFile() (filename: string): void GenPowerShellCompletionFile generates powershell completion file without descriptions.
genPowerShellCompletion() (w: io.Writer): void GenPowerShellCompletion generates powershell completion file without descriptions and writes it to the passed writer.
genPowerShellCompletionFileWithDesc() (filename: string): void GenPowerShellCompletionFileWithDesc generates powershell completion file with descriptions.
genPowerShellCompletionWithDesc() (w: io.Writer): void GenPowerShellCompletionWithDesc generates powershell completion file with descriptions and writes it to the passed writer.
markFlagRequired() (name: string): void MarkFlagRequired instructs the various shell completion implementations to prioritize the named flag when performing completion, and causes your command to report an error if invoked without the flag.
markPersistentFlagRequired() (name: string): void MarkPersistentFlagRequired instructs the various shell completion implementations to prioritize the named persistent flag when performing completion, and causes your command to report an error if invoked without the flag.
markFlagFilename() (name: string, ...extensions: string[]): void MarkFlagFilename instructs the various shell completion implementations to limit completions for the named flag to the specified file extensions.
markFlagCustom() (name: string, f: string): void MarkFlagCustom adds the BashCompCustom annotation to the named flag, if it exists. The bash completion script will call the bash function f for the flag.
markPersistentFlagFilename() (name: string, ...extensions: string[]): void MarkPersistentFlagFilename instructs the various shell completion implementations to limit completions for the named persistent flag to the specified file extensions.
markFlagDirname() (name: string): void MarkFlagDirname instructs the various shell completion implementations to limit completions for the named flag to directory names.
markPersistentFlagDirname() (name: string): void MarkPersistentFlagDirname instructs the various shell completion implementations to limit completions for the named persistent flag to directory names.
genZshCompletionFile() (filename: string): void GenZshCompletionFile generates zsh completion file including descriptions.
genZshCompletion() (w: io.Writer): void GenZshCompletion generates zsh completion file including descriptions and writes it to the passed writer.
genZshCompletionFileNoDesc() (filename: string): void GenZshCompletionFileNoDesc generates zsh completion file without descriptions.
genZshCompletionNoDesc() (w: io.Writer): void GenZshCompletionNoDesc generates zsh completion file without descriptions and writes it to the passed writer.
markZshCompPositionalArgumentFile() (argPosition: number, ...patterns: string[]): void MarkZshCompPositionalArgumentFile only worked for zsh and its behavior was not consistent with Bash completion. It has therefore been disabled. Instead, when no other completion is specified, file completion is done by default for every argument. One can disable file completion on a per-argument basis by using ValidArgsFunction and ShellCompDirectiveNoFileComp. To achieve file extension filtering, one can use ValidArgsFunction and ShellCompDirectiveFilterFileExt.
markZshCompPositionalArgumentWords() (argPosition: number, ...words: string[]): void MarkZshCompPositionalArgumentWords only worked for zsh. It has therefore been disabled. To achieve the same behavior across all shells, one can use ValidArgs (for the first argument only) or ValidArgsFunction for any argument (can include the first one also).

cobra.PositionalArgs

cobra.FParseErrWhitelist

FParseErrWhitelist configures Flag parse errors to be ignored

Has everything any has.

cobra.Group

Group Structure to manage groups for commands

Member Type
id string
title string

cobra.CompletionOptions

CompletionOptions are the options to control shell completion

Member Type
disableDefaultCmd boolean DisableDefaultCmd prevents Cobra from creating a default 'completion' command
disableNoDescFlag boolean DisableNoDescFlag prevents Cobra from creating the '--no-descriptions' flag for shells that support completion descriptions
disableDescriptions boolean DisableDescriptions turns off all completion descriptions for shells that support them
hiddenDefaultCmd boolean HiddenDefaultCmd makes the default 'completion' command hidden
defaultShellCompDirective? ShellCompDirective DefaultShellCompDirective sets the ShellCompDirective that is returned if no special directive can be determined
setDefaultShellCompDirective() (directive: ShellCompDirective): void

cobra.Completion

Completion is a string that can be used for completions

two formats are supported:

  - the completion choice
  - the completion choice with a textual description (separated by a TAB).

CompletionWithDesc can be used to create a completion string with a textual description.

Note: Go type alias is used to provide a more descriptive name in the documentation, but any string can be used.

Has everything String has.

cobra.CompletionFunc

CompletionFunc is a function that provides completion results.