cliutil

package
v0.5.6 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2022 License: MIT Imports: 10 Imported by: 24

README

Cli Util

  • 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

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] ")

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

Documentation

Overview

Package cliutil provides some util functions for CLI

Index

Constants

This section is empty.

Variables

This section is empty.

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 ...interface{})

Bluef print message with Blue color

func Blueln added in v0.5.3

func Blueln(a ...interface{})

Blueln print message line with Blue color

func Bluep added in v0.5.3

func Bluep(a ...interface{})

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 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 ...interface{})

Cyanf print message with Cyan color

func Cyanln added in v0.5.3

func Cyanln(a ...interface{})

Cyanln print message line with Cyan color

func Cyanp added in v0.5.3

func Cyanp(a ...interface{})

Cyanp print message with Cyan color

func Errorf added in v0.5.3

func Errorf(format string, a ...interface{})

Errorf print message with error style

func Errorln added in v0.5.3

func Errorln(a ...interface{})

Errorln print message with error style

func Errorp added in v0.5.3

func Errorp(a ...interface{})

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 Grayf added in v0.5.3

func Grayf(format string, a ...interface{})

Grayf print message with gray color

func Grayln added in v0.5.3

func Grayln(a ...interface{})

Grayln print message line with gray color

func Grayp added in v0.5.3

func Grayp(a ...interface{})

Grayp print message with gray color

func Greenf added in v0.5.3

func Greenf(format string, a ...interface{})

Greenf print message with green color

func Greenln added in v0.5.3

func Greenln(a ...interface{})

Greenln print message line with green color

func Greenp added in v0.5.3

func Greenp(a ...interface{})

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 ...interface{})

Infof print message with info style

func Infoln added in v0.5.3

func Infoln(a ...interface{})

Infoln print message with info style

func Infop added in v0.5.3

func Infop(a ...interface{})

Infop print message with info color

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 ...interface{})

Magentaf print message with magenta color

func Magentaln added in v0.5.3

func Magentaln(a ...interface{})

Magentaln print message line with magenta color

func Magentap added in v0.5.3

func Magentap(a ...interface{})

Magentap print message with magenta color

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

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 ...interface{})

Redf print message with Red color

func Redln added in v0.5.3

func Redln(a ...interface{})

Redln print message line with Red color

func Redp added in v0.5.3

func Redp(a ...interface{})

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 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 ...interface{})

Successf print message with success style

func Successln added in v0.5.6

func Successln(a ...interface{})

Successln print message with success style

func Successp added in v0.5.6

func Successp(a ...interface{})

Successp print message with success color

func Warnf added in v0.5.3

func Warnf(format string, a ...interface{})

Warnf print message with warn style

func Warnln added in v0.5.3

func Warnln(a ...interface{})

Warnln print message with warn style

func Warnp added in v0.5.3

func Warnp(a ...interface{})

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 ...interface{})

Yellowf print message with yellow color

func Yellowln added in v0.5.3

func Yellowln(a ...interface{})

Yellowln print message line with yellow color

func Yellowp added in v0.5.3

func Yellowp(a ...interface{})

Yellowp print message with yellow color

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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