cobra README

The project's README from github.com/spf13/cobra. As one markdown file: README.md.

cobra-logo

Cobra is a library for creating powerful modern CLI applications.

Visit Cobra.dev for extensive documentation

Cobra is used in many Go projects such as Kubernetes, Hugo, and GitHub CLI to name a few. This list contains a more extensive list of projects using Cobra.

Go Reference Go Report Card Slack


Supported by:

Warp sponsorship

Warp, the AI terminal for devs

Try Cobra in Warp today


Overview

Cobra is a library providing a simple interface to create powerful modern CLI interfaces similar to git & go tools.

Cobra provides:

Concepts

Cobra is built on a structure of commands, arguments & flags.

Commands represent actions, Args are things and Flags are modifiers for those actions.

The best applications read like sentences when used, and as a result, users intuitively know how to interact with them.

The pattern to follow is APPNAME VERB NOUN --ADJECTIVE or APPNAME COMMAND ARG --FLAG.

A few good real world examples may better illustrate this point.

In the following example, 'server' is a command, and 'port' is a flag:

hugo server --port=1313

In this command we are telling Git to clone the url bare.

git clone URL --bare

Commands

Command is the central point of the application. Each interaction that the application supports will be contained in a Command. A command can have children commands and optionally run an action.

In the example above, 'server' is the command.

More about cobra.Command

Flags

A flag is a way to modify the behavior of a command. Cobra supports fully POSIX-compliant flags as well as the Go flag package. A Cobra command can define flags that persist through to children commands and flags that are only available to that command.

In the example above, 'port' is the flag.

Flag functionality is provided by the pflag library, a fork of the flag standard library which maintains the same interface while adding POSIX compliance.

Installing

Using Cobra is easy. First, use go get to install the latest version of the library.

go get -u github.com/spf13/cobra@latest

Next, include Cobra in your application:

import "github.com/spf13/cobra"

Usage

cobra-cli is a command line program to generate cobra applications and command files. It will bootstrap your application scaffolding to rapidly develop a Cobra-based application. It is the easiest way to incorporate Cobra into your application.

It can be installed by running:

go install github.com/spf13/cobra-cli@latest

For complete details on using the Cobra-CLI generator, please read The Cobra Generator README

For complete details on using the Cobra library, please read The Cobra User Guide.

License

Cobra is released under the Apache 2.0 license. See LICENSE.txt

cobra (spf13/cobra)

Searching Database…

44.7K ·GitHub
这是什么?Cobra 是一个 Go 语言库,用于构建带子命令的现代命令行应用,类似 git 或 go 工具。Kubernetes、Hugo 和 GitHub CLI 的命令行都基于它构建。
它是库还是独立应用?它是一个 Go 库,需要导入到你自己的应用中;另外还有一个单独的 'cobra-cli' 生成工具,可以为你搭建样板代码。
怎样把它加到项目里?用 go get 安装这个库,在你的 Go 应用中导入它;如果需要,还可以安装 cobra-cli 生成工具来搭建命令的基础代码。
除了 Go,使用它还需要别的东西吗?只需要 Go 工具链即可。参数解析由内置的 pflag 库在内部处理(它是 Go 标准库 flag 包的一个符合 POSIX 规范的分支)。如果需要按十二要素应用的方式管理配置,可以选择性地集成 Viper。
它要求严格遵循特定的命令结构吗?Cobra 要求应用遵循 APPNAME VERB NOUN --ADJECTIVE 这一格式(命令、参数、标志),因此你需要按照这个约定来设计命令行,而不是随意解析参数。
在 Linux、macOS 和 Windows 上表现一致吗?是的。它可以生成自动补全脚本,并且支持跨平台,兼容 bash、zsh、fish 和 PowerShell 等 shell。
开箱即用有哪些功能?嵌套子命令、全局/本地/级联标志、智能命令建议、自动生成帮助信息、生成 man 手册页、命令别名,以及生成 shell 自动补全。
命令能给拼写错误提供修正建议吗?可以。Cobra 提供智能建议,比如输入错误的命令 'app srver' 时,会提示“你是不是想输入 app server?”
能自动生成 man 手册页和 shell 补全吗?可以。Cobra 会自动为你的应用生成 man 手册页,以及 bash、zsh、fish 和 PowerShell 的 shell 自动补全脚本。
Cobra 能和配置库集成吗?可以。它可选择性地与 Viper 无缝集成,便于构建按十二要素风格、带分层配置的应用。
What are similar projects?hexyl, ipatool, oh-my-posh, gh-dash, kitty, posting, coreutils, neofetch
Creator
spf13
Size
2.1 MB
Stars
44.7K
Age
13y
Commits · 90 Days