misc Flashcards
(81 cards)
Front
Back
What is spf13/cobra
used for?
It’s a Go library for building powerful CLI applications using commands, subcommands, and flags.
How do you define a new Cobra command?
Create a *cobra.Command
with fields like Use
, Short
, Run
, and optionally Long
, Args
, etc.
How do you add a subcommand in Cobra?
Use rootCmd.AddCommand(childCmd)
where both are *cobra.Command
objects.
What is Run
in a Cobra command?
Run
is a function that executes when the command is called. It’s usually of the form func(cmd *cobra.Command, args []string)
.
How do you parse command-line flags in Cobra?
Use cmd.Flags().String(...)
, Int(...)
, Bool(...)
, etc., to define flags and bind to variables.
How do you access parsed flag values in Cobra?
Use methods like cmd.Flags().GetString("flagname")
inside your Run
function.
How do you enforce required flags in Cobra?
Use cmd.MarkFlagRequired("flagname")
after defining the flag.
How do you define arguments validation in Cobra?
Use the Args
field in the command struct, such as cobra.ExactArgs(1)
or custom functions.
What function starts Cobra command execution?
rootCmd.Execute()
is called from main()
to start CLI parsing and command routing.
How do you print help text in a Cobra command?
Use cmd.Help()
or run the command with --help
to print usage and flag info.
Front
Back
What is Charmbracelet’s huh
library used for?
huh
is a TUI (text UI) form library that makes it easy to collect structured CLI input using forms and steps.
How do you start a new huh
form?
Use form.New()
and chain AddFields(...)
to define input steps.
How are field values accessed after running a huh
form?
Each field is passed by reference (like &name
) and populated after form.Run()
completes.
What types of fields does huh
support?
Input
, Select
, MultiSelect
, Confirm
, Group
, Text
, Spacer
, and custom fields.
How do you customize a field’s behavior in huh
?
Use options like .Title(...)
, .Validate(...)
, .Options(...)
, or .Key(...)
to control layout and behavior.
How do you validate user input in huh
?
Use .Validate(func(string) error)
to reject invalid input and show an error message.
What does form.WithTheme(...)
do in huh
?
It sets the visual theme for the entire form UI.
What function runs the huh
form and returns an error?
form.Run()
runs the form and returns an error if the user cancels or there’s a display issue.
Front
Back
What is the purpose of go generate
?
It runs arbitrary shell commands specified in special //go:generate
comments in Go source files.
What’s a typical comment format for go generate
?
//go:generate command args
— written above the code it relates to, usually above a type or package.