shlex

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 15, 2022 License: MIT Imports: 5 Imported by: 2 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-shlex

go-shlex is a library to make a lexical analyzer like Unix shell for Go.

Install

go get -u "github.com/chroblert/go-shlex"

description

Split(<目标字符串>,<是否posix模式>,<是否保留所有字面量>,[指定分隔符])
    当未指定分隔符,且不保留所有字面量时,即Split(<目标字符串>,<是否posix模式>,false),与原来的函数作用相同(desertbit/go-shlex)
OriginSplit(<目标字符串>,<是否posix模式>)
是否保留所有字面量:指是否保留',",\这些特殊字符。
假设输入:set arg="argV1\"",argV2
- 若为true:  set arg "argV1\"",argV2
- 若为false: set arg argV1",argV2

Usage

package main

import (
    "fmt"
    "log"

    "github.com/chroblert/go-shlex"
)

func main() {
    cmd := `cp -Rdp "file name" 'file name2' dir\ name`
    words, err := shlex.Split(cmd, true,false)
    if err != nil {
        log.Fatal(err)
    }

    for _, w := range words {
        fmt.Println(w)
    }
}

output

cp
-Rdp
file name
file name2
dir name

Documentation

http://godoc.org/github.com/anmitsu/go-shlex

Documentation

Overview

Package shlex provides a simple lexical analysis like Unix shell.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrNoClosing = errors.New("no closing quotation")
	ErrNoEscaped = errors.New("no escaped character")
)

Functions

func OriginSplit added in v0.1.2

func OriginSplit(s string, posix bool) ([]string, error)

Split splits a string according to posix or non-posix rules. 未修改的Split()

func Split

func Split(s string, posix bool, preserveLiteral bool, delimiter ...rune) ([]string, error)

Split splits a string according to posix or non-posix rules. 默认分隔符为空白(不只是空格),否则为填写的第一个rune

Example
package main

import (
	"fmt"
	"log"

	shlex "github.com/chroblert/go-shlex"
)

func main() {
	cmd := `cp -Rdp "file name" 'file name2' dir\ name`

	// Split of cmd with POSIX mode.
	words1, err := shlex.Split(cmd, true, false)
	if err != nil {
		log.Fatal(err)
	}
	// Split of cmd with Non-POSIX mode.
	words2, err := shlex.Split(cmd, false, false)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Source command:")
	fmt.Println(`cp -Rdp "file name" 'file name2' dir\ name`)
	fmt.Println()

	fmt.Println("POSIX mode:")
	for _, word := range words1 {
		fmt.Println(word)
	}
	fmt.Println()
	fmt.Println("Non-POSIX mode:")
	for _, word := range words2 {
		fmt.Println(word)
	}

}
Output:
Source command:
cp -Rdp "file name" 'file name2' dir\ name

POSIX mode:
cp
-Rdp
file name
file name2
dir name

Non-POSIX mode:
cp
-Rdp
"file name"
'file name2'
dir\
name

Types

type DefaultTokenizer

type DefaultTokenizer struct{}

DefaultTokenizer implements a simple tokenizer like Unix shell.

func (*DefaultTokenizer) IsDelimiter

func (t *DefaultTokenizer) IsDelimiter(r rune, delimiter rune, delimiterSpecific bool) bool

func (*DefaultTokenizer) IsEscape

func (t *DefaultTokenizer) IsEscape(r rune) bool

func (*DefaultTokenizer) IsEscapedQuote

func (t *DefaultTokenizer) IsEscapedQuote(r rune, preserveLiteral bool) bool

delimiterSpecific 是否指定了分隔符,若未指定,则按照标准的posix模式

func (*DefaultTokenizer) IsQuote

func (t *DefaultTokenizer) IsQuote(r rune) bool

func (*DefaultTokenizer) IsWord

func (t *DefaultTokenizer) IsWord(r rune) bool

type Lexer

type Lexer struct {
	// contains filtered or unexported fields
}

Lexer represents a lexical analyzer.

func NewLexer

func NewLexer(posix, whitespaceSplit bool, delimiter rune, r io.Reader, preserveLiteral bool, delimiterSpecific bool) *Lexer

NewLexer creates a new Lexer reading from io.Reader. This Lexer has a DefaultTokenizer according to posix and whitespaceSplit rules.

func NewLexerString

func NewLexerString(posix, whitespaceSplit bool, delimiter rune, s string, preserveLiteral, delimiterSpecific bool) *Lexer

NewLexerString creates a new Lexer reading from a string. This Lexer has a DefaultTokenizer according to posix and whitespaceSplit rules.

func (*Lexer) SetTokenizer

func (l *Lexer) SetTokenizer(t Tokenizer)

SetTokenizer sets a Tokenizer.

func (*Lexer) Split

func (l *Lexer) Split() ([]string, error)

type Tokenizer

type Tokenizer interface {
	IsWord(rune) bool
	IsDelimiter(rune, rune, bool) bool
	IsQuote(rune) bool
	IsEscape(rune) bool
	IsEscapedQuote(rune, bool) bool
}

Tokenizer is the interface that classifies a token according to words, whitespaces, quotations, escapes and escaped quotations.

Jump to

Keyboard shortcuts

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