README.md
This file
Path
README.md
111 lines · 4.3 KiB · Markdown
Last changed by
Changed branch name in the cobra generator link to 'main' (#1645)Neville Bokdawallabefore adoptiond622355
History of this fileTake it
The walk stopped after 200 commits: lines older than 0ea93dd point at it.
Fixed linter issuesJuan Lenibefore adoption0ea93dd
lines 1–2why, one step back12Removes viper dependency by removing cobra/ CLI tool (#1604)John McBridebefore adoption5b2b9e9
line 3why, one step back3Cobra is a library for creating powerful modern CLI applications.Fixed linter issuesJuan Lenibefore adoption0ea93dd
line 4why, one step back4Move projects using Cobra (#1090)Warren Fernandesbefore adoption090d94f
lines 5–7why, one step back5Cobra is used in many Go projects such as [Kubernetes](http://kubernetes.io/),6[Hugo](https://gohugo.io), and [Github CLI](https://github.com/cli/cli) to7name a few. [This list](./projects_using_cobra.md) contains a more extensive list of projects using Cobra.Fixed linter issuesJuan Lenibefore adoption0ea93dd
line 8why, one step back8ci: add GitHub Actions workflow 'Test' (#1339)Unai Martinez-Corralbefore adoptionb73b344
line 9why, one step back9[](https://github.com/spf13/cobra/actions?query=workflow%3ATest)Update Go Doc link and badge (#1593)Marian Steinbachbefore adoption7cabfeb
line 10why, one step back10[](https://pkg.go.dev/github.com/spf13/cobra)add goreportcard to readme (#971)Joshua Harshmanbefore adoption3745fcd
line 11why, one step back11[](https://goreportcard.com/report/github.com/spf13/cobra)Add slack badge to point to cobra slack channel (#1181)Warren Fernandesbefore adoption0e27f22
line 12why, one step back12[](https://gophers.slack.com/archives/CD3LP1199)Fixed linter issuesJuan Lenibefore adoption0ea93dd
lines 13–26why, one step back1314# Overview1516Cobra is a library providing a simple interface to create powerful modern CLI17interfaces similar to git & go tools.1819Cobra provides:20* Easy subcommand-based CLIs: `app server`, `app fetch`, etc.21* Fully POSIX-compliant flags (including short & long versions)22* Nested subcommands23* Global, local and cascading flags24* Intelligent suggestions (`app srver`... did you mean `app server`?)25* Automatic help generation for commands and flags26* Automatic help flag recognition of `-h`, `--help`, etc.Extend Go completions and revamp zsh comp (#1070) (#1070)Marc Khouzambefore adoption2c5a0d3
line 27why, one step back27* Automatically generated shell autocomplete for your application (bash, zsh, fish, powershell)Fixed linter issuesJuan Lenibefore adoption0ea93dd
lines 28–30why, one step back28* Automatically generated man pages for your application29* Command aliases so you can change things without breaking them30* The flexibility to define your own help, usage, etc.Update documentation to reflect the module aware generatorSteve Franciabefore adoptionc97b7ec
line 31why, one step back31* Optional seamless integration with [viper](http://github.com/spf13/viper) for 12-factor appsFixed linter issuesJuan Lenibefore adoption0ea93dd
lines 32–38why, one step back3233# Concepts3435Cobra is built on a structure of commands, arguments & flags.3637**Commands** represent actions, **Args** are things and **Flags** are modifiers for those actions.38README.md Readability Improvements (#1228)zaataylorbefore adoption39b5a91
lines 39–40why, one step back39The best applications read like sentences when used, and as a result, users40intuitively know how to interact with them.Fixed linter issuesJuan Lenibefore adoption0ea93dd
lines 41–64why, one step back4142The pattern to follow is43`APPNAME VERB NOUN --ADJECTIVE.`44 or45`APPNAME COMMAND ARG --FLAG`4647A few good real world examples may better illustrate this point.4849In the following example, 'server' is a command, and 'port' is a flag:5051 hugo server --port=13135253In this command we are telling Git to clone the url bare.5455 git clone URL --bare5657## Commands5859Command is the central point of the application. Each interaction that60the application supports will be contained in a Command. A command can61have children commands and optionally run an action.6263In the example above, 'server' is the command.64Updating generator documentation and linksSteve Franciabefore adoptioncf87fc4
line 65why, one step back65[More about cobra.Command](https://pkg.go.dev/github.com/spf13/cobra#Command)Fixed linter issuesJuan Lenibefore adoption0ea93dd
lines 66–81why, one step back6667## Flags6869A flag is a way to modify the behavior of a command. Cobra supports70fully POSIX-compliant flags as well as the Go [flag package](https://golang.org/pkg/flag/).71A Cobra command can define flags that persist through to children commands72and flags that are only available to that command.7374In the example above, 'port' is the flag.7576Flag functionality is provided by the [pflag77library](https://github.com/spf13/pflag), a fork of the flag standard library78which maintains the same interface while adding POSIX compliance.7980# Installing81Using Cobra is easy. First, use `go get` to install the latest versionRemoves viper dependency by removing cobra/ CLI tool (#1604)John McBridebefore adoption5b2b9e9
line 82why, one step back82of the library. Fixed linter issuesJuan Lenibefore adoption0ea93dd
line 83why, one step back83Removes viper dependency by removing cobra/ CLI tool (#1604)John McBridebefore adoption5b2b9e9
lines 84–86why, one step back84```85go get -u github.com/spf13/cobra@latest86```Fixed linter issuesJuan Lenibefore adoption0ea93dd
lines 87–93why, one step back8788Next, include Cobra in your application:8990```go91import "github.com/spf13/cobra"92```93readme: split 'Getting Started' into 'user_guide.md'umarcorbefore adoptionace6b14
line 94why, one step back94# UsageRemoves viper dependency by removing cobra/ CLI tool (#1604)John McBridebefore adoption5b2b9e9
lines 95–97why, one step back95`cobra-cli` is a command line program to generate cobra applications and command files.96It will bootstrap your application scaffolding to rapidly97develop a Cobra-based application. It is the easiest way to incorporate Cobra into your application.Fixed linter issuesJuan Lenibefore adoption0ea93dd
line 98why, one step back98Removes viper dependency by removing cobra/ CLI tool (#1604)John McBridebefore adoption5b2b9e9
lines 99–104why, one step back99It can be installed by running:100101```102go install github.com/spf13/cobra-cli@latest103```104Changed branch name in the cobra generator link to 'main' (#1645)Neville Bokdawallabefore adoptiond622355
line 105why, one step back105For complete details on using the Cobra-CLI generator, please read [The Cobra Generator README](https://github.com/spf13/cobra-cli/blob/main/README.md)Updating generator documentation and linksSteve Franciabefore adoptioncf87fc4
lines 106–107why, one step back106107For complete details on using the Cobra library, please read the [The Cobra User Guide](user_guide.md).Fixed linter issuesJuan Lenibefore adoption0ea93dd
lines 108–111why, one step back108109# License110111Cobra is released under the Apache 2.0 license. See [LICENSE.txt](https://github.com/spf13/cobra/blob/master/LICENSE.txt)