Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

CLI framework for scale and configurability inspired by Cobra

License

NotificationsYou must be signed in to change notification settings

coder/serpent

Go Reference

serpent is a Go CLI configuration framework based oncobra and used bycoder/coder.It's designed for large-scale CLIs with dozens of commands and hundredsof options. If you're building a small, self-contained tool, go withcobra.

help example

When compared to cobra, serpent strives for:

  • Better default help output inspired by the Go toolchain
  • Greater flexibility in accepting options that span across multiple sources
  • Composition via middleware
  • Testability (e.g. OS Stdout and Stderr is only available to commands explicitly)

Basic Usage

Seeexample/echo:

package mainimport ("os""strings""github.com/coder/serpent")funcmain() {varupperboolcmd:= serpent.Command{Use:"echo <text>",Short:"Prints the given text to the console.",Options: serpent.OptionSet{{Name:"upper",Value:serpent.BoolOf(&upper),Flag:"upper",Description:"Prints the text in upper case.",},},Handler:func(inv*serpent.Invocation)error {iflen(inv.Args)==0 {inv.Stderr.Write([]byte("error: missing text\n"))os.Exit(1)}text:=inv.Args[0]ifupper {text=strings.ToUpper(text)}inv.Stdout.Write([]byte(text))returnnil},}err:=cmd.Invoke().WithOS().Run()iferr!=nil {panic(err)}}

Design

This Design section assumes you have a good understanding of howcobra works.

Options

Serpent is designed for high-configurability. To us, that means providingmany ways to configure the same value (env, YAML, flags, etc.) and keepingthe code clean and testable as you scale the number of options.

Serpent'sOption type looks like:

typeOptionstruct {NamestringFlagstringEnvstringDefaultstringValue pflag.Value// ...}

And is used by eachCommand whenpassed as an array to theOptions field.

Comparison with Cobra

Here is a comparison of thehelp output between a simpleecho command in Cobra and Serpent.

Cobra

echo is for echoing anything back. Echo works a lot like print, except it has a child command.Usage:  echo [string to echo] [flags]Flags:  -h, --help    help for echo  -u, --upper   make the output uppercase

Serpent

USAGE:  echo <text>  Prints the given text to the console.OPTIONS:      --upper bool          Prints the text in upper case.

Migrating from Cobra

Serpent is designed to be a replacement for Cobra and Viper. If you are familiar with Cobra, the transition to Serpent should be relatively straightforward. The main differences are:

  • Command Structure: Serpent uses aserpent.Command struct which is similar tocobra.Command.
  • Options: Serpent has a more flexible and powerful option system that allows you to define options from multiple sources (flags, environment variables, config files, etc.) in a single place.
  • Middleware: Serpent has a middleware system that allows you to compose functionality and apply it to your commands.
  • Testability: Serpent is designed to be more testable than Cobra. For example, OS stdout and stderr are only available to commands explicitly.

Serpent vs. Cobra and Viper

Serpent is intended to be a complete replacement for both Cobra and Viper. While Viper is often used with Cobra to provide environment and config file support, Serpent has this functionality built-in. This results in a more integrated and streamlined experience.

Examples

For a more comprehensive example of a large-scale CLI built with Serpent, please see thecoder/coder repository.

About

CLI framework for scale and configurability inspired by Cobra

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors10


[8]ページ先頭

©2009-2025 Movatter.jp