cliutil

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

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/zhangyiming748/pretty/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/zhangyiming748/pretty/cliutil"
	"github.com/zhangyiming748/pretty/dump"
)

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

output:

PRINT AT github.com/zhangyiming748/pretty/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 FirstLine = strutil.FirstLine

FirstLine from command output

Deprecated: please use strutil.FirstLine

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

the global input output stream

Functions

func BinDir

func BinDir() string

BinDir get

func BinFile

func BinFile() string

BinFile get

func BinName

func BinName() string

BinName get

func Bluef

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

Bluef print message with Blue color

func Blueln

func Blueln(a ...any)

Blueln print message line with Blue color

func Bluep

func Bluep(a ...any)

Bluep print message with Blue color

func BuildLine

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

BuildLine build command line string by given args.

func BuildOptionHelpName

func BuildOptionHelpName(names []string) string

BuildOptionHelpName for render flag help

func ByteIsYes

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

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

Cyanf print message with Cyan color

func Cyanln

func Cyanln(a ...any)

Cyanln print message line with Cyan color

func Cyanp

func Cyanp(a ...any)

Cyanp print message with Cyan color

func Errorf

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

Errorf print message with error style

func Errorln

func Errorln(a ...any)

Errorln print message with error style

func Errorp

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

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

ExecLine quick exec an command line string

func GetTermSize

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

GetTermSize for current console terminal.

func Grayf

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

Grayf print message with gray color

func Grayln

func Grayln(a ...any)

Grayln print message line with gray color

func Grayp

func Grayp(a ...any)

Grayp print message with gray color

func Greenf

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

Greenf print message with green color

func Greenln

func Greenln(a ...any)

Greenln print message line with green color

func Greenp

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

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

Infof print message with info style

func Infoln

func Infoln(a ...any)

Infoln print message with info style

func Infop

func Infop(a ...any)

Infop print message with info color

func InputIsYes

func InputIsYes(ans string) bool

InputIsYes answer: yes, y, Yes, Y

func LineBuild

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

LineBuild build command line string by given args.

func Magentaf

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

Magentaf print message with magenta color

func Magentaln

func Magentaln(a ...any)

Magentaln print message line with magenta color

func Magentap

func Magentap(a ...any)

Magentap print message with magenta color

func OutputLines

func OutputLines(output string) []string

OutputLines split output to lines

func ParseLine

func ParseLine(line string) []string

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

func QuickExec

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

QuickExec quick exec a simple command line

func ReadFirst

func ReadFirst(question string) (string, error)

ReadFirst read first char

Usage:

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

func ReadFirstByte

func ReadFirstByte(question string) (byte, error)

ReadFirstByte read first byte char

Usage:

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

func ReadFirstRune

func ReadFirstRune(question string) (rune, error)

ReadFirstRune read first rune char

func ReadInput

func ReadInput(question string) (string, error)

ReadInput read user input form Stdin

func ReadLine

func ReadLine(question string) (string, error)

ReadLine read one line from user input.

Usage:

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

func ReadPassword

func ReadPassword(question ...string) string

ReadPassword from console terminal

func Redf

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

Redf print message with Red color

func Redln

func Redln(a ...any)

Redln print message line with Red color

func Redp

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

func ShellQuote(s string) string

ShellQuote quote a string on contains ', ", SPACE

func String2OSArgs

func String2OSArgs(line string) []string

String2OSArgs parse input command line text to os.Args

func StringToOSArgs

func StringToOSArgs(line string) []string

StringToOSArgs parse input command line text to os.Args

func Successf

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

Successf print message with success style

func Successln

func Successln(a ...any)

Successln print message with success style

func Successp

func Successp(a ...any)

Successp print message with success color

func Warnf

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

Warnf print message with warn style

func Warnln

func Warnln(a ...any)

Warnln print message with warn style

func Warnp

func Warnp(a ...any)

Warnp print message with warn color

func Workdir

func Workdir() string

Workdir get

func Yellowf

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

Yellowf print message with yellow color

func Yellowln

func Yellowln(a ...any)

Yellowln print message line with yellow color

func Yellowp

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.
Package termctrl provide some simple term control utils
Package termctrl provide some simple term control utils

Jump to

Keyboard shortcuts

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