cmdl

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2021 License: BSL-1.0 Imports: 2 Imported by: 2

README

cmdl

GoDoc Go Report Card

About

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

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

cmdl 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)

cmdl 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/cmdl"
)

func main() {
	cl := cmdl.New()

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

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

	} else {
		unparsedArgs := cl.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/cmdl"
)

func main() {
	start := "0"
	end := "0"
	cl := cmdl.New()
	asgOp := cmdl.NewAsgOp(false, false, "=")

	paramStart := cl.NewParam().ParsePairs(asgOp, "start")
	paramEnd := cl.NewParam().ParsePairs(asgOp, "end")

	if paramStart.Available() {
		start = paramStart.Values()[0]
		end = start
	}
	if paramEnd.Available() {
		end = paramEnd.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 cmdl 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 AssignmentOperator

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

AssignmentOperator represents the operator between key and value in argument.

func NewAsgOp

func NewAsgOp(blankAllowed, emptyAllowed bool, ops ...string) *AssignmentOperator

NewAsgOp creates and returns a new instance of AssignmentOperator.

func (*AssignmentOperator) MatchingOp

func (asgOps *AssignmentOperator) MatchingOp(str string) (bool, string)

MatchingOp returns true and matching operator, if str starts with assignment operator.

type CommandLine

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

CommandLine holds command line arguments.

func New

func New() *CommandLine

New creates and returns a new instance of CommandLine.

func NewFrom

func NewFrom(args []string) *CommandLine

NewFrom creates and returns a new instance of CommandLine.

func (*CommandLine) Args

func (cl *CommandLine) Args() []string

Args returns command line arguments.

func (*CommandLine) NewParam

func (cl *CommandLine) NewParam() *Parameter

NewParam creates and returns a new instance of Parameter.

func (*CommandLine) UnparsedArgs

func (cl *CommandLine) UnparsedArgs() []string

UnparsedArgs returns unparsed arguments.

type Parameter

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

Parameter represents a parsed argument from command line.

func (*Parameter) Add

func (param *Parameter) Add(key, value string)

Add appends key and value.

func (*Parameter) Available

func (param *Parameter) Available() bool

Available returns true, if parameter has been found in command line.

func (*Parameter) Count

func (param *Parameter) Count() int

Count returns number of matches in command line.

func (*Parameter) Keys

func (param *Parameter) Keys() []string

Keys returns parsed keys.

func (*Parameter) Parse

func (param *Parameter) Parse(keys ...string) *Parameter

Parse searches for keys in command line. Returns itself.

func (*Parameter) ParsePairs

func (param *Parameter) ParsePairs(asgOps *AssignmentOperator, keys ...string) *Parameter

ParsePairs searches for keys in command line.

func (*Parameter) Values

func (param *Parameter) Values() []string

Values returns parsed values.

Jump to

Keyboard shortcuts

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