Skip to content

A simple, intuitive, and kinda fun command-line application framework for Go.

License

Notifications You must be signed in to change notification settings

cipherdothost/cmdkit-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cmdkit

Go Documentation Go Report Card Tests

Package cmdkit provides a simple, intuitive, and kinda fun command-line application framework that extends Go's flag package with support for subcommands, better help formatting, and a simple-to-use API.

The package features:

  • Built on Go's standard flag package.
  • No external dependencies.
  • Subcommand support with proper flag handling.
  • Unix-style short and long flags (-h, --help).
  • Automatic help text generation and formatting.
  • Before/After hooks for setup and cleanup.

The package also provide utilities that:

  • Add environment variable support for flags.
  • Add proper exit code handling.
  • Add support for repeatable flags.
  • Add smart terminal detection.
  • Add color output detection with NO_COLOR compliance.

Installation

To install cmdkit and use it in your project, run:

go get go.cipher.host/cmdkit@latest

Usage

Here's a simple example that creates a greeting application:

package main

import (
    "fmt"
    "os"

    "go.cipher.host/cmdkit"
)

func main() {
    // Create a new application
    app := cmdkit.New("greet", "A friendly greeting app", "1.0.0")

    // Add a "hello" command
    hello := cmdkit.NewCommand("hello", "Say hello to someone", "[--formal] <name>")
    
    var formal bool
    hello.Flags.BoolVar(&formal, "formal", false, "use formal greeting")
    
    hello.RunE = func(args []string) error {
        if len(args) < 1 {
            return fmt.Errorf("name is required")
        }

        if formal {
            fmt.Printf("Good day, %s!\n", args[0])
        } else {
            fmt.Printf("Hi, %s!\n", args[0])
        }

        return nil
    }

    app.AddCommand(hello)

    // Run the application
    cmdkit.Exit(app.Run(os.Args[1:]))
}

This will create a command-line application with the following interface:

$ greet --help
Usage: greet [global flags] command [command flags] [arguments...]

A friendly greeting app

Commands:
  hello         Say hello to someone

Global Flags:
  -h, --help    show help

Use "greet [command] --help" for more information about a command.

$ greet hello --help
Usage: greet hello [--formal] <name>

Say hello to someone

Flags:
  -h, --help     show help
  --formal       use formal greeting

$ greet hello Alice
Hi, Alice!

$ greet hello --formal Bob
Good day, Bob!

Contributing

Anyone can help make cmdkit better. Check out the contribution guidelines for more information.


The work in this repository complies with the REUSE specification. While the default license is MIT, individual files may be licensed differently.

Please see the individual files for details and the LICENSES directory for a full list of licenses used in this repository.