Skip to content

Latest commit

Β 

History

History
101 lines (76 loc) Β· 2.47 KB

README.md

File metadata and controls

101 lines (76 loc) Β· 2.47 KB

scotty

πŸ––πŸ‘¨β€πŸ’»Scotty - Zero dependencies library to build simple commandline apps.

Basically it is a thin wrapper around standard flag.FlagSet type.

Documentation

Build codecov

Features

  • πŸ€“ Simple API.
  • πŸ‘Œ Zero dependencies.
  • 😘 Plays nice with standard flag package.
  • 😌 Nice default -help information.

Installation

go get github.com/heartwilltell/scotty

Usage

The usage is pretty simple:

  • πŸ‘‰ Declare the root command.
  • πŸ‘‰ Attach subcommands and flags to it.
  • πŸ‘‰ Write your stuff inside the Run function.
  • πŸ‘‰ Call Exec function of the root command somewhere in main function.
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/heartwilltell/scotty"
)

func main() {
	// Declare the root command. 
	rootCmd := scotty.Command{
		Name:  "app",
		Short: "Main command which holds all subcommands",
	}

	// Declare the subcommand.
	subCmd := scotty.Command{
		Name:  "subcommand",
		Short: "Subcommands that does something",
		Run: func(cmd *scotty.Command, args []string) error {
			// Do some your stuff here.
			return nil
		},
	}

	// And here how you bind some flags to your command.
	var logLVL string

	subCmd.Flags().StringVar(&logLVL, "loglevel", "info",
		"set logging level: 'debug', 'info', 'warning', 'error'",
	)

	// Or use the SetFlags function.

	subCmd2 := scotty.Command{
		Name:  "subcommand2",
		Short: "Subcommands that does something",
		SetFlags: func(flags *FlagSet) {
			flags.StringVar(&logLVL, "loglevel", "info",
				"set logging level: 'debug', 'info', 'warning', 'error'",
			)
        },
		Run: func(cmd *scotty.Command, args []string) error {
			// Do some your stuff here.
			return nil
		},
	}

	// Attach subcommand to the root command. 
	rootCmd.AddSubcommands(&subCmd)

	// Execute the root command.
	if err := rootCmd.Exec(); err != nil {
		fmt.Println(err)
		os.Exit(2)
	}
}

License

MIT License.