cl

package module
v0.0.0-...-24b01a2 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2025 License: BSL-1.0 Imports: 1 Imported by: 0

README

cl

Go Report Card

About

cl is a package for Go to parse command line arguments. It is published on https://github.com/vbsw/cl and https://codeberg.org/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"
	"github.com/vbsw/cl"
	"os"
)

func main() {
	osCmdLine := cl.New(os.Args[1:])

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

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

	} else {
		unmatched := osCmdLine.Unmatched()

		if unmatched.Count() > 1 {
			fmt.Println("ERROR too many arguments")

		} else if unmatched.Count() == 1 {
			fmt.Printf("ERROR unknown argument \"%s\"\n", unmatched.Keys[0])
		}
	}
}
Example B
package main

import (
	"fmt"
	"github.com/vbsw/cl"
	"os"
)

func main() {
	start := "0"
	end := "0"
	osCmdLine := cl.New(os.Args[1:])
	osCmdLine.Delimiter = cl.NewDelimiter("=")

	startArg := osCmdLine.SearchByDelimiter("start")
	endArg := osCmdLine.SearchByDelimiter("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 added in v1.0.1

type Arguments struct {
	Keys   []string
	Values []string
}

Arguments represents arguments returned by command line search.

func (*Arguments) Available added in v1.0.1

func (args *Arguments) Available() bool

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

func (*Arguments) Count added in v1.0.1

func (args *Arguments) Count() int

Count returns number of arguments.

type CommandLine

type CommandLine struct {
	Arguments []string
	Matched   []bool
	Delimiter *Delimiter
}

CommandLine represents command line.

func New

func New(args []string) *CommandLine

New returns a new instance of CommandLine.

func (*CommandLine) Search

func (cmdLine *CommandLine) Search(searchTerms ...string) *Arguments

Search compairs CommandLine.Arguments with searchTerms and returns matches.

func (*CommandLine) SearchByDelimiter

func (cmdLine *CommandLine) SearchByDelimiter(searchTerms ...string) *Arguments

SearchByDelimiter compairs CommandLine.Arguments with searchTerms and returns matches. The search considers a delimiter, that separates key and value within parameter. Is Delimiter.HasSpaceSeparator set, then two arguments are treated as one argument with key and value separated by space.

func (*CommandLine) Unmatched

func (cmdLine *CommandLine) Unmatched() *Arguments

Unmatched returns command line arguments that haven't been matched by the search.

type Delimiter

type Delimiter struct {
	Separators        []string
	HasSpaceSeparator bool
	HasEmptySeparator bool
}

Delimiter represents separators between key and value.

func NewDelimiter

func NewDelimiter(separators ...string) *Delimiter

NewDelimiter returns a new instance of Delimiter. An empty separator "" sets the HasEmptySeparator flag for delimiter, and the space separator " " sets the HasSpaceSeparator flag.

Jump to

Keyboard shortcuts

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