cl

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

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

Go to latest
Published: Jul 6, 2025 License: BSL-1.0 Imports: 2 Imported by: 0

README

cl

Go Reference Go Report Card

About

cl is a package for Go to parse command line arguments. It is published on https://codeberg.org/vbsw/cl and https://github.com/vbsw/cl.

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

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

cl 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"
	"codeberg.org/vbsw/cl"
)

func main() {
	clArgs := cl.NewCLArgs()

	if clArgs.Argument("--help", "-h").Available() {
		fmt.Println("USAGE")
		fmt.Println("    --help         prints help")
		fmt.Println("    --version      prints version")

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

	} else {
		restArgs := clArgs.Rest()

		if restArgs.Count() == 1 {
			fmt.Printf("ERROR unknown parameter \"%s\"\n", restArgs.Params[0])

		} else if restArgs.Count() > 1 {
			fmt.Println("ERROR too many arguments")
		}
	}
}
Example B
package main

import (
	"fmt"
	"codeberg.org/vbsw/cl"
)

func main() {
	start := "0"
	end := "0"
	clArgs := cl.NewCLArgs()
	delimiter := []string{"="};

	startArg := clArgs.ArgumentByDelimiter(delimiter, "start")
	endArg := clArgs.ArgumentByDelimiter(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)
}

References

Documentation

Overview

Package cl parses 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 {
	Params []string
	Values []string
	Meta   []int
}

Arguments holds command line arguments.

func NewCLArgs

func NewCLArgs() *Arguments

NewCLArgs 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) Argument

func (clArgs *Arguments) Argument(argVariants ...string) *Result

Argument returns matches of different argument variations.

func (*Arguments) ArgumentByDelimiter

func (clArgs *Arguments) ArgumentByDelimiter(delimiter []string, argVariants ...string) *Result

ArgumentByDelimiter returns matches of different argument variations splitted by delimiter. First part of the split goes in Arguments.Params, the second in Arguments.Values. Space (" ") as delimiter does not split, but treats two arguments as one.

func (*Arguments) Available

func (clArgs *Arguments) Available() bool

Available returns true, if at least one argument is available.

func (*Arguments) Count

func (clArgs *Arguments) Count() int

Count returns number of arguments.

func (*Arguments) InitMeta

func (clArgs *Arguments) InitMeta()

InitMeta resets Arguments.Meta.

func (*Arguments) Rest

func (clArgs *Arguments) Rest() *Result

Rest returns arguments that haven't been returned by Argument*() function.

type Result

type Result = Arguments

Result represents one or more arguments extracted from command line arguments.

Jump to

Keyboard shortcuts

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