cliutil

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2022 License: MIT Imports: 11 Imported by: 23

README

Cli Util

cliutil provides some extra util functions for CLI.

  • Helper util functions in cli
  • Color print in console terminal
  • Read terminal message input
  • Command line args string parse
  • Build command line string from []string

Install

go get github.com/gookit/goutil/cliutil

Go docs

Helper functions

cliutil.Workdir() // current workdir
cliutil.BinDir() // the program exe file dir
cliutil.QuickExec("echo $SHELL")

Color print

Quick color print in console:

cliutil.Redln("ln:red color message print in cli.")
cliutil.Blueln("ln:blue color message print in cli.")
cliutil.Cyanln("ln:cyan color message print in cli.")

color-print

Read input

name := cliutil.ReadInput("Your name: ")
name := cliutil.ReadLine("Your name: ")

ans, _ := cliutil.ReadFirst("continue?[y/n] ")
if cliutil.InputIsYes(ans) {
	// do something ...
}

ans, _ := cliutil.ReadFirstByte("continue?[y/n] ")
if cliutil.ByteIsYes(ans) {
	// do something ...
}
Read Password
pwd := cliutil.ReadPassword("Input password:")

Parse command line as args

parse input command line to []string, such as cli os.Args

package main

import (
	"github.com/gookit/goutil/cliutil"
	"github.com/gookit/goutil/dump"
)

func main() {
	args := cliutil.ParseLine(`./app top sub --msg "has multi words"`)
	dump.P(args)
}

output:

PRINT AT github.com/gookit/goutil/cliutil_test.TestParseLine(line_parser_test.go:30)
[]string [ #len=5
  string("./app"), #len=5
  string("top"), #len=3
  string("sub"), #len=3
  string("--msg"), #len=5
  string("has multi words"), #len=15
]

Build command line from args

	s := cliutil.BuildLine("./myapp", []string{
		"-a", "val0",
		"-m", "this is message",
		"arg0",
	})
	fmt.Println("Build line:", s)

output:

Build line: ./myapp -a val0 -m "this is message" arg0

Functions API

func BinDir() string
func BinFile() string
func BinName() string
func Bluef(format string, a ...interface{})
func Blueln(a ...interface{})
func Bluep(a ...interface{})
func BuildLine(binFile string, args []string) string
func BuildOptionHelpName(names []string) string
func CurrentShell(onlyName bool) (path string)
func Cyanf(format string, a ...interface{})
func Cyanln(a ...interface{})
func Cyanp(a ...interface{})
func Errorf(format string, a ...interface{})
func Errorln(a ...interface{})
func Errorp(a ...interface{})
func ExecCmd(binName string, args []string, workDir ...string) (string, error)
func ExecCommand(binName string, args []string, workDir ...string) (string, error)
func ExecLine(cmdLine string, workDir ...string) (string, error)
func FirstLine(output string) string
func Grayf(format string, a ...interface{})
func Grayln(a ...interface{})
func Grayp(a ...interface{})
func Greenf(format string, a ...interface{})
func Greenln(a ...interface{})
func Greenp(a ...interface{})
func HasShellEnv(shell string) bool
func Infof(format string, a ...interface{})
func Infoln(a ...interface{})
func Infop(a ...interface{})
func LineBuild(binFile string, args []string) string
func Magentaf(format string, a ...interface{})
func Magentaln(a ...interface{})
func Magentap(a ...interface{})
func OutputLines(output string) []string
func ParseLine(line string) []string
func QuickExec(cmdLine string, workDir ...string) (string, error)
func ReadFirst(question string) (string, error)
func ReadFirstByte(question string) (byte, error)
func ReadFirstRune(question string) (rune, error)
func ReadInput(question string) (string, error)
func ReadLine(question string) (string, error)
func ReadPassword(question ...string) string
func Redf(format string, a ...interface{})
func Redln(a ...interface{})
func Redp(a ...interface{})
func ShellExec(cmdLine string, shells ...string) (string, error)
func ShellQuote(s string) string
func String2OSArgs(line string) []string
func StringToOSArgs(line string) []string
func Successf(format string, a ...interface{})
func Successln(a ...interface{})
func Successp(a ...interface{})
func Warnf(format string, a ...interface{})
func Warnln(a ...interface{})
func Warnp(a ...interface{})
func Workdir() string
func Yellowf(format string, a ...interface{})
func Yellowln(a ...interface{})
func Yellowp(a ...interface{})

Code Check & Testing

gofmt -w -l ./
golint ./...

Testing:

go test -v ./cliutil/...

Test limit by regexp:

go test -v -run ^TestSetByKeys ./cliutil/...

Projects using cliutil

cliutil is used in these projects:

Documentation

Overview

Package cliutil provides some util functions for CLI

Index

Constants

This section is empty.

Variables

View Source
var (
	// Input global input stream
	Input io.Reader = os.Stdin
)

the global input output stream

Functions

func BinDir added in v0.4.5

func BinDir() string

BinDir get

func BinFile added in v0.4.5

func BinFile() string

BinFile get

func BinName added in v0.5.5

func BinName() string

BinName get

func Bluef added in v0.5.3

func Bluef(format string, a ...any)

Bluef print message with Blue color

func Blueln added in v0.5.3

func Blueln(a ...any)

Blueln print message line with Blue color

func Bluep added in v0.5.3

func Bluep(a ...any)

Bluep print message with Blue color

func BuildLine added in v0.3.12

func BuildLine(binFile string, args []string) string

BuildLine build command line string by given args.

func BuildOptionHelpName added in v0.5.5

func BuildOptionHelpName(names []string) string

BuildOptionHelpName for render flag help

func ByteIsYes added in v0.6.0

func ByteIsYes(ans byte) bool

ByteIsYes answer: yes, y, Yes, Y

func CurrentShell

func CurrentShell(onlyName bool) (path string)

CurrentShell get current used shell env file. eg "/bin/zsh" "/bin/bash"

func Cyanf added in v0.5.3

func Cyanf(format string, a ...any)

Cyanf print message with Cyan color

func Cyanln added in v0.5.3

func Cyanln(a ...any)

Cyanln print message line with Cyan color

func Cyanp added in v0.5.3

func Cyanp(a ...any)

Cyanp print message with Cyan color

func Errorf added in v0.5.3

func Errorf(format string, a ...any)

Errorf print message with error style

func Errorln added in v0.5.3

func Errorln(a ...any)

Errorln print message with error style

func Errorp added in v0.5.3

func Errorp(a ...any)

Errorp print message with error color

func ExecCmd

func ExecCmd(binName string, args []string, workDir ...string) (string, error)

ExecCmd a CLI bin file and return output.

Usage:

ExecCmd("ls", []string{"-al"})

func ExecCommand

func ExecCommand(binName string, args []string, workDir ...string) (string, error)

ExecCommand alias of the ExecCmd()

func ExecLine added in v0.3.11

func ExecLine(cmdLine string, workDir ...string) (string, error)

ExecLine quick exec an command line string

func FirstLine added in v0.5.11

func FirstLine(output string) string

FirstLine from command output

func GetTermSize added in v0.6.0

func GetTermSize(refresh ...bool) (w int, h int)

GetTermSize for current console terminal.

func Grayf added in v0.5.3

func Grayf(format string, a ...any)

Grayf print message with gray color

func Grayln added in v0.5.3

func Grayln(a ...any)

Grayln print message line with gray color

func Grayp added in v0.5.3

func Grayp(a ...any)

Grayp print message with gray color

func Greenf added in v0.5.3

func Greenf(format string, a ...any)

Greenf print message with green color

func Greenln added in v0.5.3

func Greenln(a ...any)

Greenln print message line with green color

func Greenp added in v0.5.3

func Greenp(a ...any)

Greenp print message with green color

func HasShellEnv

func HasShellEnv(shell string) bool

HasShellEnv has shell env check.

Usage:

HasShellEnv("sh")
HasShellEnv("bash")

func Infof added in v0.5.3

func Infof(format string, a ...any)

Infof print message with info style

func Infoln added in v0.5.3

func Infoln(a ...any)

Infoln print message with info style

func Infop added in v0.5.3

func Infop(a ...any)

Infop print message with info color

func InputIsYes added in v0.6.0

func InputIsYes(ans string) bool

InputIsYes answer: yes, y, Yes, Y

func LineBuild added in v0.3.10

func LineBuild(binFile string, args []string) string

LineBuild build command line string by given args.

func Magentaf added in v0.5.3

func Magentaf(format string, a ...any)

Magentaf print message with magenta color

func Magentaln added in v0.5.3

func Magentaln(a ...any)

Magentaln print message line with magenta color

func Magentap added in v0.5.3

func Magentap(a ...any)

Magentap print message with magenta color

func OutputLines added in v0.5.11

func OutputLines(output string) []string

OutputLines split output to lines

func ParseLine added in v0.3.8

func ParseLine(line string) []string

ParseLine input command line text. alias of the StringToOSArgs()

func QuickExec added in v0.1.4

func QuickExec(cmdLine string, workDir ...string) (string, error)

QuickExec quick exec a simple command line

func ReadFirst added in v0.3.8

func ReadFirst(question string) (string, error)

ReadFirst read first char

Usage:

ans, _ := cliutil.ReadFirst("continue?[y/n] ")

func ReadFirstByte added in v0.3.12

func ReadFirstByte(question string) (byte, error)

ReadFirstByte read first byte char

Usage:

ans, _ := cliutil.ReadFirstByte("continue?[y/n] ")

func ReadFirstRune added in v0.3.12

func ReadFirstRune(question string) (rune, error)

ReadFirstRune read first rune char

func ReadInput added in v0.3.8

func ReadInput(question string) (string, error)

ReadInput read user input form Stdin

func ReadLine added in v0.3.8

func ReadLine(question string) (string, error)

ReadLine read one line from user input.

Usage:

in := cliutil.ReadLine("")
ans, _ := cliutil.ReadLine("your name?")

func ReadPassword added in v0.3.8

func ReadPassword(question ...string) string

ReadPassword from console terminal

func Redf added in v0.5.3

func Redf(format string, a ...any)

Redf print message with Red color

func Redln added in v0.5.3

func Redln(a ...any)

Redln print message line with Red color

func Redp added in v0.5.3

func Redp(a ...any)

Redp print message with Red color

func ShellExec

func ShellExec(cmdLine string, shells ...string) (string, error)

ShellExec exec command by shell

Usage: ret, err := cliutil.ShellExec("ls -al")

func ShellQuote added in v0.5.9

func ShellQuote(s string) string

ShellQuote quote a string on contains ', ", SPACE

func String2OSArgs added in v0.3.12

func String2OSArgs(line string) []string

String2OSArgs parse input command line text to os.Args

func StringToOSArgs added in v0.3.8

func StringToOSArgs(line string) []string

StringToOSArgs parse input command line text to os.Args

func Successf added in v0.5.6

func Successf(format string, a ...any)

Successf print message with success style

func Successln added in v0.5.6

func Successln(a ...any)

Successln print message with success style

func Successp added in v0.5.6

func Successp(a ...any)

Successp print message with success color

func Warnf added in v0.5.3

func Warnf(format string, a ...any)

Warnf print message with warn style

func Warnln added in v0.5.3

func Warnln(a ...any)

Warnln print message with warn style

func Warnp added in v0.5.3

func Warnp(a ...any)

Warnp print message with warn color

func Workdir added in v0.4.5

func Workdir() string

Workdir get

func Yellowf added in v0.5.3

func Yellowf(format string, a ...any)

Yellowf print message with yellow color

func Yellowln added in v0.5.3

func Yellowln(a ...any)

Yellowln print message line with yellow color

func Yellowp added in v0.5.3

func Yellowp(a ...any)

Yellowp print message with yellow color

Types

This section is empty.

Directories

Path Synopsis
Package cmdline provide quick build and parse cmd line string.
Package cmdline provide quick build and parse cmd line string.

Jump to

Keyboard shortcuts

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