clp

package
v0.0.0-...-730ab69 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package clp implements command-line parsing.

Inspired by:

https://pkg.go.dev/flag
https://pkg.go.dev/github.com/jessevdk/go-flags

Supported features

The following features are supported:

Options with short names (-v)
Options with long names (--verbose)
Options with and without values
Supports multiple short options (-fr is equivalent to -f -r)
Supports the following types: bool, int{8..64}, uint{8..64}, float{32..64}, string
Supports slices

Usage

err := clp.HandleCommands(commands)
err := clp.HandleCommandsFromArgs(commands, args)
err := clp.ParseOptions(&options)
err := clp.ParseOptionsFromArgs(&options, args)

See cmd/server/main.go for example.

Command-line option syntax

The following forms are permitted:

-o              // short option without value (boolean option only)
-o value        // short option with value (non-boolean option only)
-o=value        // short option with value
-op             // multiple short options without value (is equivalent to -op=true)
-op=value       // multiple short options with value
--option        // long option without value (boolean option only)
--option value  // long option with value (non-boolean option only)
--option=value  // long option with value

Option parsing stops ather the terminator "--".

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandleCommands

func HandleCommands(handlers map[string]Handler) error

HandleCommands parses the command-line arguments from os.Args[1:] and handles the corresponding command.

func HandleCommandsFromArgs

func HandleCommandsFromArgs(handlers map[string]Handler, args []string) error

HandleCommandsFromArgs parses the given arguments and handles the corresponding command.

func ParseOptions

func ParseOptions(data interface{}) error

ParseOptions parses the command-line arguments from os.Args[1:] and updates the struct fields with the corresponding values.

func ParseOptionsFromArgs

func ParseOptionsFromArgs(data interface{}, args []string) error

ParseOptionsFromArgs parses the given arguments and updates the struct fields with the corresponding values.

Types

type Handler

type Handler func([]string) error

type ParsingError

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

func (ParsingError) Error

func (e ParsingError) Error() string

func (*ParsingError) Unwrap

func (e *ParsingError) Unwrap() error

Jump to

Keyboard shortcuts

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