Prompts users to enter values for required flags in Cobra CLI applications

Cobra Flag Prompt

Cobra Flag Prompt prompts users to enter values for required flags. It is an extension of Cobra, and requires that you use Cobra to set up the CLI for your program.

GoDoc

User Experience Before Cobra Flag Prompt

Without Cobra Flag Prompt, the program immediately terminates if the user did not enter a required flag and prints out which flag(s) were missing and all usage text for all flags. The user must then retype the command, add the new flag(s), identify the flag(s) usage text, and choose the correct value for the required flag. This isn't a big deal with programs which only have two options, but for programs with dozens of options the process becomes tedious.

Video: https://youtu.be/EhVW5Vl9KAE

User Experience After Cobra Flag Prompt

With Cobra Flag Prompt, the user is prompted for any missing required flags. To assist the user, the Usage information for the missing flag(s) is displayed when the user needs it.

Video: https://youtu.be/5sNhdYA5hc4

Usage

Usage is trivial. Replace calls to MarkFlagRequired with cobraFlagPrompt.MarkFlagRequired and calls to MarkPersistentFlagRequired with cobraFlagPrompt.MarkPersistentFlagRequired.

Advanced usage can be ascertained by reading the GoDoc or the source code.

Usage Walkthrough

First, get the package

go get github.com/Polyapp-LLC/cobraFlagPrompt

Then modify your existing code. Here is an example Cobra init() function for a program with 2 flags and which does NOT use cobraFlagPrompt:

func init() {
    rootCmd.Flags().StringVar(&EnvironmentName, "environmentName", "", `environment names can only be lowercase ascii characters and hyphens. Example: test-one`)
    rootCmd.PersistentFlags().StringVarP(&CloudName, "cloudName", "n", "", `cloud name must be one of the following strings: "AWS", "Azure", "Google", "localhost"`)
    
    err := rootCmd.MarkFlagRequired("environmentName")
    if err != nil {
        panic("rootCmd.MarkFlagRequired(environmentName): " + err.Error())
    }
    err = rootCmd.MarkPersistentFlagRequired("cloudName")
    if err != nil {
    	panic("rootCmd.MarkPersistentFlagRequired(cloudName): " + err.Error())
    }
}

Here is the same code, except it is using cobraFlagPrompt. Notice: rootCmd.MarkFlagRequired has been updated to: cobraFlagPrompt.MarkFlagRequired.

func init() {
    rootCmd.Flags().StringVar(&EnvironmentName, "environmentName", "", `environment names can only be lowercase ascii characters and hyphens. Example: test-one`)
    rootCmd.PersistentFlags().StringVarP(&CloudName, "cloudName", "n", "", `cloud name must be one of the following strings: "AWS", "Azure", "Google", "localhost"`)
    
    err := cobraFlagPrompt.MarkFlagRequired(rootCmd, "environmentName")
    if err != nil {
        panic("cobraFlagPrompt.MarkFlagRequired(environmentName): " + err.Error())
    }
    err = cobraFlagPrompt.MarkPersistentFlagRequired(rootCmd, "cloudName")
    if err != nil {
    	panic("cobraFlagPrompt.MarkPersistentFlagRequired(cloudName): " + err.Error())
    }
}

The results can be seen above (this is the code used in the example videos).

Support and Issues

Cobra Flag Prompt ought to work with all flag types supported by Cobra. Tests are in cobraFlagPrompt_test.go. Questions? Leave an Issue! Thanks :)

Similar Resources

Build an interactive CLI application with Go, Cobra and promptui. Video tutorial available on the Div Rhino YouTube channel.

Build an interactive CLI app with Go, Cobra and promptui Text tutorial: https://divrhino.com/articles/build-interactive-cli-app-with-go-cobra-promptui

Dec 8, 2022

Simple CLI using spf13/cobra and Flink REST API

Flinkctl Flinkctl is a simple CLI written on Go using Cobra created to facilitate my team's daily basis work with multiple Flink clusters at Azion Tec

Aug 16, 2022

Generate an interactive, autocompleting shell for any Cobra CLI

Generate an interactive, autocompleting shell for any Cobra CLI

cobra-shell Description Leverages the Cobra completion API to generate an interactive shell for any Cobra CLI, powered by go-prompt. On-the-fly autoco

Dec 19, 2022

A cli client-server app with cobra

A cli client-server app with cobra

cli-client-server-calculator a cli client-server app with cobra overview this project is a cli client-server app in which client gives a bunch of numb

Dec 7, 2021

Cobra CLI challenge Segsalerty

Banking app done using Cobra CLI - Segsalerty challenge c/o Segun Mustafa It uses a database.json file as datastore for queries - createCustomer, Upda

Dec 14, 2021

This project is used to get familiar with GoLang cli apps, along with cobra generator.

SecretCTL SecretCTL About the project Status Getting started Layout Notes About the project This project is used to get familiar with GoLang cli apps,

Jan 11, 2022

Automatically sets up command line flags based on struct fields and tags.

Automatically sets up command line flags based on struct fields and tags.

Commandeer Commandeer sets up command line flags based on struct fields and tags. Do you... like to develop Go apps as libraries with tiny main packag

Dec 1, 2022

Drop-in replacement for Go's flag package, implementing POSIX/GNU-style --flags.

Description pflag is a drop-in replacement for Go's flag package, implementing POSIX/GNU-style --flags. pflag is compatible with the GNU extensions to

Dec 30, 2022

Generate flags by parsing structures

Flags based on structures. The sflags package uses structs, reflection and struct field tags to allow you specify command line options. It supports di

Nov 24, 2022
Idiomatic Go input parsing with subcommands, positional values, and flags at any position. No required project or package layout and no external dependencies.
Idiomatic Go input parsing with subcommands, positional values, and flags at any position. No required project or package layout and no external dependencies.

Sensible and fast command-line flag parsing with excellent support for subcommands and positional values. Flags can be at any position. Flaggy has no

Jan 1, 2023
gopsa is a CLI tool to enter timecard entries to the Salesforce PSA platform.
gopsa is a CLI tool to enter timecard entries to the Salesforce PSA platform.

gopsa Overview gopsa is a CLI tool to enter timecard entries to the Salesforce PSA platform. Credit This project is a rewrite of the excellent work fr

Nov 22, 2021
A rich tool for parsing flags and values in pure Golang

A rich tool for parsing flags and values in pure Golang. No additional library is required and you can use everywhere.

Jan 25, 2022
Cobra CLI tool to generate applications and commands

Cobra Generator Cobra provides its own program that will create your application and add any commands you want. It's the easiest way to incorporate Co

Jan 3, 2023
CLI for SendGrid, which helps in managing SSO users, can install and update users from yaml config

Sendgrid API This script is needed to add new users to SendGrid as SSO teammates. Previously, all users were manually added and manually migrating the

Jul 20, 2022
Building powerful interactive prompts in Go, inspired by python-prompt-toolkit.
Building powerful interactive prompts in Go, inspired by python-prompt-toolkit.

go-prompt A library for building powerful interactive prompts inspired by python-prompt-toolkit, making it easier to build cross-platform command line

Jan 3, 2023
A golang library for building interactive prompts with full support for windows and posix terminals.
A golang library for building interactive prompts with full support for windows and posix terminals.

Survey A library for building interactive prompts on terminals supporting ANSI escape sequences. package main import ( "fmt" "github.com/Alec

Jan 6, 2023
CLI to run a docker image with R. CLI built using cobra library in go.
CLI  to run a docker image with R. CLI built using cobra library in go.

BlueBeak Installation Guide Task 1: Building the CLI The directory structure looks like Fastest process: 1)cd into bbtools 2)cd into bbtools/bin 3)I h

Dec 20, 2021
Go-file-downloader-ftctl - A file downloader cli built using golang. Makes use of cobra for building the cli and go concurrent feature to download files.

ftctl This is a file downloader cli written in Golang which uses the concurrent feature of go to download files. The cli is built using cobra. How to

Jan 2, 2022