osargs

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2021 License: BSL-1.0 Imports: 2 Imported by: 2

README

osargs

GoDoc Go Report Card

About

Package osargs provides functions to parse command line arguments. It is published on https://github.com/vbsw/osargs and https://gitlab.com/vbsw/osargs.

Copyright 2021, Vitali Baumtrok (vbsw@mailbox.org).

osargs is distributed under the Boost Software License, version 1.0. (See accompanying file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)

osargs is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Boost Software License for more details.

Usage

Example A
package main

import (
	"fmt"
	"github.com/vbsw/osargs"
)

func main() {
	args := osargs.New()

	if args.Parse("--help", "-h").Available() {
		fmt.Println("valid parameters are -h or -v.")

	} else if args.Parse("--version", "-v").Available() {
		fmt.Println("version 1.0.0")

	} else {
		unparsedArgs := args.UnparsedArgs()

		if len(unparsedArgs) == 1 {
			fmt.Println("error: unknown parameter", unparsedArgs[0])

		} else if len(unparsedArgs) > 1 {
			fmt.Println("error: too many arguments")
		}
	}
}
Example B
package main

import (
	"fmt"
	"github.com/vbsw/osargs"
)

func main() {
	start := "0"
	end := "0"
	args := osargs.New()
	delimiter := osargs.NewDelimiter(false, false, "=")

	startArg := args.ParsePairs(delimiter, "start")
	endArg := args.ParsePairs(delimiter, "end")

	if startArg.Available() {
		start = startArg.Values[0]
		end = start
	}
	if endArg.Available() {
		end = endArg.Values[0]
	}
	fmt.Println("processing from", start, "to", end)
}

Command line:

$ ./test start=1 end=10
$ processing from 1 to 10

References

Documentation

Overview

Package osargs provides functions to parse command line arguments.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Arguments

type Arguments struct {
	Values []string
	Parsed []bool
}

Arguments holds command line arguments.

func New

func New() *Arguments

New creates and returns a new instance of Arguments. Arguments have all command line arguments except the first one, which is the program name.

func (*Arguments) Parse

func (args *Arguments) Parse(flags ...string) *Result

Parse returns available flags in command line.

func (*Arguments) ParsePairs

func (args *Arguments) ParsePairs(delimiter *Delimiter, flags ...string) *Result

ParsePairs returns the values of available flags in command line. The values are the ones that are separated from flags by the delimiter.

func (*Arguments) UnparsedArgs

func (args *Arguments) UnparsedArgs() []string

UnparsedArgs returns unparsed arguments.

type Delimiter

type Delimiter struct {
	Tokens []string
	Blank  bool
	Empty  bool
}

Delimiter is the separator between flag and value in command line argument.

func NewDelimiter

func NewDelimiter(blank, empty bool, Tokens ...string) *Delimiter

NewDelimiter creates and returns a new instance of Delimiter.

func (*Delimiter) MatchingToken

func (delimiter *Delimiter) MatchingToken(str string) (bool, string)

MatchingToken returns true and the delimiter token, if str starts with token.

type Result

type Result struct {
	Values []string
}

Result represents parsed argument in command line.

func (*Result) Available

func (result *Result) Available() bool

Available returns true, if argument is in command line.

func (*Result) Count

func (result *Result) Count() int

Count returns number of matches in command line.

Jump to

Keyboard shortcuts

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