arguments

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

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

Go to latest
Published: Mar 26, 2022 License: Unlicense Imports: 3 Imported by: 0

README

Arguments

A command argument parser with some helpful extras. Supports syntax escapes, multilingual quotation marks, and the ability to mark an argument as escaped. As well as preservation of parsing metadata to allow fetching sections of raw unparsed string between arguments.

Also unofficially known as Greg.

Syntax


  • Argument Separation via spaces, a 'space' in this case being any unicode space, including linebreaks.

    • one two > one, two
    • Repeated spaces, Leading spaces, and Trailing spaces, are all ignored.
  • \ Escaping can cancel out any forms of parsed syntax, including spaces, and other escapes.

    • As an extra that may be useful in certain situations, an argument may be prefixed with an escape (\) to add a meta flag marking it as 'escaped', what this means if anything is up to the end application.
  • Quotation marks may be used to mark a section of text to be treated as a singular argument ignoring any spaces contained between the start and end quote. Supported quotation marks include:

    • Standard single and double quotes.
    • "Smart" quotes, sometimes seen in office software or used by mobile keyboards in place of standard quotes.
    • Chevrons.
    • Japanese angle quotes.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var QuotePairs = map[string]endQuote{

	"'":  "'",
	"\"": "\"",

	"\u201C\u201D\u201F\u201E": "\u201C\u201D\u201F",
	"\u2018\u2019\u201B\u201A": "\u2018\u2019\u201B",

	"\u00AB\u300A": "\u00BB\u300B",
	"\u00BB\u300B": "\u00AB\u300A",
	"\u2039\u3008": "\u203A\u3009",
	"\u203A\u3009": "\u2039\u3008",

	"\u300C\u300E": "\u300D\u300F",
}

Functions

This section is empty.

Types

type Argument

type Argument struct {
	// argument start and end positions
	// be aware that this is the rune positions, not the byte positions
	Start, End int

	Content string

	// if the argument was escaped via being prefixed with \
	Escaped bool
	Quoted  bool
}

Argument is a single argument

type Arguments

type Arguments struct {
	Cursor int // Cursor is the current position in the list of arguments

	Original  string
	Arguments []Argument
}

Arguments represents a list of arguments

func Parse

func Parse(s string) (_ Arguments, ok bool)

Parse is a function that parses the given string into a list of arguments using the following syntax. - Spaces are used as delimiters to separate arguments.

  • Duplicate spaces are ignored.

- Quotes are used to create a single argument ignoring spaces.

  • (see `quotePairs` for list of supported quote types)

- `\` is used to escape quotes and spaces or other escapes. in addition, an escape may be used at the beginning of an argument to mark it as escaped.

func (Arguments) Append

func (a Arguments) Append(b *Arguments) Arguments

Append combines two argument sets into one, returning the combined list

func (Arguments) AppendString

func (a Arguments) AppendString(s string) (Arguments, bool)

AppendString parses a string and appends it to the argument list returning the combined list

func (*Arguments) Current

func (a *Arguments) Current() (Argument, bool)

Current returns the current argument

func (*Arguments) Len

func (a *Arguments) Len() int

Len returns the number of arguments

func (*Arguments) Next

func (a *Arguments) Next() (Argument, bool)

Next returns the next argument

func (*Arguments) Peek

func (a *Arguments) Peek() (Argument, bool)

Peek returns the next argument without advancing the cursor

func (*Arguments) PeekBy

func (a *Arguments) PeekBy(offset int) (Argument, bool)

PeekBy returns the argument at the given index relative to the current cursor

func (*Arguments) Previous

func (a *Arguments) Previous() (Argument, bool)

Previous returns the previous argument

func (*Arguments) Range

func (a *Arguments) Range(start, end int) (Arguments, bool)

Range returns a copy of a containing only the arguments between the start and end positions

func (*Arguments) Raw

func (a *Arguments) Raw(index int) (string, bool)

Raw returns the original string between the start and end positions of an argument

func (*Arguments) RawBy

func (a *Arguments) RawBy(offset int) (string, bool)

RawBy returns the raw string of an argument at the given index relative to the current cursor

func (*Arguments) RawRange

func (a *Arguments) RawRange(start, end int) (string, bool)

RawRange returns the original string between a range of arguments

func (*Arguments) Reset

func (a *Arguments) Reset()

Reset the cursor to the beginning

func (*Arguments) Seek

func (a *Arguments) Seek(index int) bool

Seek moves the cursor to the given index if it returns false it means it cannot seek to that position

func (*Arguments) SeekBy

func (a *Arguments) SeekBy(offset int) bool

SeekBy moves the cursor to the given index relative to the current cursor

func (Arguments) Strings

func (a Arguments) Strings() []string

Strings returns the content of each argument as a slice of strings

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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