cmdline

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2025 License: MIT Imports: 5 Imported by: 8

README

cmdline

Parse command-line args

Report Card Go Reference Coverage

Package cmdline provides an interface for defining and accessing arguments to a Go program that are specified on the command line of the program execution.

Copyright 2024 Board of Trustees of the University of Illinois. See the license for details.

Documentation

Overview

package cmdline provides an interface for defining and accessing arguments to a go program that are specified on the command line of the program execution

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FlagTypeString

func FlagTypeString(type_name FlagArgType) string

FlagTypeString converts a command line argument enumerated type into a string representation

Types

type CmdParser

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

A CmdParser struct maps the flag names of command variables to their type specific representations

func NewCmdParser

func NewCmdParser() *CmdParser

NewCmdParser is a constructor, initializes an empty CmdParser data structure

func (*CmdParser) AddFlag

func (cp *CmdParser) AddFlag(arg_type FlagArgType, arg_name string, arg_req bool)

AddFlag includes a new command flag to the parser. The arguments give the type of the flag in enumerated type form, the name of the flag, and whether the flag is required

func (*CmdParser) GetVar

func (cp *CmdParser) GetVar(name string) any

GetVar returns the type-unspecified value of a command variable that was created in the CmdParser, and given a value from the command line. It is assumed that the application calls IsLoaded before GetVar to ascertain that a value is indeed present

func (*CmdParser) IsFlag

func (cp *CmdParser) IsFlag(name string) bool

IsFlag returns a bool indicating whether the input argument string 'name' has been used to create a command variable in the CmdParser

func (*CmdParser) IsLoaded

func (cp *CmdParser) IsLoaded(name string) bool

IsLoaded returns a bool indicating whether a command variable with the input argument string 'name' was recognized on the command line and so had a value stored

func (*CmdParser) IsRequired

func (cp *CmdParser) IsRequired(name string) bool

IsRequired returns a bool indicating whether a command variable with the input argument 'name' was declared to be required

func (*CmdParser) Parse

func (cp *CmdParser) Parse() bool

Parse looks for a leading "-is" on the command line to determine whether to parse from a file (e.g., "-is" is present), or get the arguments from the command line itself

func (*CmdParser) ParseFromCmdLine

func (cp *CmdParser) ParseFromCmdLine() bool

ParseFromCmdLine gets the command line string from os.Args, i.e., the run-time command line

func (*CmdParser) ParseFromFile

func (cp *CmdParser) ParseFromFile(filename string) bool

ParseFromFile gets the command line flags from a file. This enables separation across lines and comments

func (*CmdParser) ParseFromString

func (cp *CmdParser) ParseFromString(cmd_string string) bool

ParseFromString separates the command line string into individual command statements and stores them in the CmdParser

func (*CmdParser) SetVar

func (cp *CmdParser) SetVar(name string, value string)

SetVar calls an arg interface function with a command variable name and string-encoded value from the command line to set the value in the type-specific struct.

type FlagArgType

type FlagArgType int

FlagArgType is the type basis for an enumerated type of command-line flags

const (
	IntFlag FlagArgType = iota
	Int64Flag
	FloatFlag
	StringFlag
	BoolFlag
	None
)

IntFlag, Int64Flag, FloatFlag, StringFlag, and BoolFlag are the enumerated types of scalar types of arguments declared on the command line

Jump to

Keyboard shortcuts

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