gocommand

package module
v0.0.0-...-c7e8bca Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2024 License: MIT Imports: 7 Imported by: 0

README

imageimage

gocommand

GitHub go.mod Go version GitHub GoDoc

Gocommand is the simplest way to create a command line interface throw stdin/out for your Go application. It provides a simple way to read commands and arguments from the standard input and execute them.

Usage

Simple command read
package main

import (
	"fmt"
	"github.com/nessai1/gocommand"
)

func main() {
	gocommand.ListenAndServe(func(cmd *gocommand.Command) error {
		if cmd.Name == "exit" {
			return gocommand.ErrGracefulExit
		}

		fmt.Printf("Command: %s\n", cmd.Name)
		fmt.Printf("Arguments: %v\n", cmd.Args)

		return nil
	})
}

Output:
shell_example_1

Prompting the user for input

Library allows you to ask user for input using the AskText and AskSecret functions

  • AskText function asks the user for a text input and returns it as a string
  • AskSecret function asks the user for a sensitive data and returns it as a string
package main

import (
	"fmt"
	"github.com/nessai1/gocommand"
)

func main() {
	gocommand.ListenAndServe(func(cmd *gocommand.Command) error {
		if cmd.Name == "exit" {
			return gocommand.ErrGracefulExit
		}

		if cmd.Name == "login" {
			login, err := gocommand.AskText("Enter your username")
			if err != nil {
				return fmt.Errorf("cannot ask login: %w", err)
			}

			password, err := gocommand.AskSecret("Enter your password")
			if err != nil {
				return fmt.Errorf("cannot ask password: %w", err)
			}

			fmt.Printf("make authentification with login '%s' and password '%s'...\n", login, password)
		}

		return nil
	})
}

Output:
shell_example_1

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrGracefulExit = fmt.Errorf("graceful exit")

Functions

func AskSecret

func AskSecret(prompt string) (string, error)

AskSecret prompts user to enter a secret, writes prompt to output as an anchor and separates it with a colon

func AskText

func AskText(prompt string) (string, error)

AskText prompts user to enter a text, writes prompt to output as an anchor and separates it with a colon

func ListenAndServe

func ListenAndServe(handler func(*Command) error)

ListenAndServe reads commands from input and calls handler for each command. If handler returns ErrGracefulExit, ListenAndServe stops without error message; If handler returns err != nil, ListenAndServe stops and print error message; If handler returns nil, ListenAndServe continues to read commands.

Types

type Command

type Command struct {
	// Name first word of given command line
	Name string
	// Args arguments of written command, separated by spaces
	Args []string
}

Command info about entered command

func ReadCommand

func ReadCommand() (*Command, error)

ReadCommand prompts user to enter a command to input, writes command anchor to output

Example
ListenAndServe(func(cmd *Command) error {
	if cmd.Name == "exit" {
		return ErrGracefulExit
	}

	fmt.Printf("Command: %s\n", cmd.Name)
	fmt.Printf("Arguments: %v\n", cmd.Args)

	return nil
})
Output:

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL