script_helper

package module
v0.0.0-...-46aa87a Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2024 License: MIT Imports: 3 Imported by: 1

README

Go Script Helper

A Go package providing utility functions for flag handling and formatted logging. This package is designed to help streamline script development by providing structured error, success, and warning logging, as well as a flag validation function.

Installation

To install the script_helper package, use:

go get github.com/cartersusi/script_helper

Functions

1. GetFlag
func GetFlag[T string | int | bool](long_flag, short_flag *T, value T, required bool, flag_name ...string) T {

The GetFlag function checks the values of the specified long and short flags. It enforces that only one flag should be set, returning the value of the flag that is set.

  • Parameters:
    • long_flag: Pointer to the long-form of the flag.
    • short_flag: Pointer to the short-form of the flag.
    • value: Default value for the flags.
    • required: Boolean flag to indicate if the flag is required.
    • flag_name: Optional name of the flag to be used in the error message.s
  • Returns: The value of the specified flag (either long_flag or short_flag).
  • Errors: Triggers an error if neither flag is set or if both flags are set.
2. Error
func Error(s string, exit ...bool)

Logs an error message with an "ERROR" prefix and optionally exits the program. Supports colored output to enhance readability.

  • Parameters:

  • s: The error message to be logged.

  • exit: Optional boolean slice. If the first value is true, the program exits after logging the error.

  • Behavior:

    • Logs the error message in red.
    • Exits the program if exit is true.
    • Continues execution if exit is not provided or is false.
3. Success
func Success(s string)

Logs a success message with a "SUCCESS" prefix, with the message displayed in green.

  • Parameters:
    • s: The success message to be logged.
  • Behavior:
    • Logs the message in green with a "SUCCESS" prefix.
4. Warning
func Warning(s string)

Logs a warning message with a "WARNING" prefix, with the message displayed in yellow.

  • Parameters:
    • s: The warning message to be logged. Behavior:
    • Logs the message in yellow with a "WARNING" prefix.

Color Utility Functions

The package also includes internal functions for color formatting of text:

  • red(s string): Formats text in red.
  • green(s string): Formats text in green.
  • yellow(s string): Formats text in yellow.

Example Usage

package main

import (
	sh "github.com/cartersusi/script_helper"
	"flag"
)

func main() {
	longFlag := flag.String("long", "", "long form flag")
	shortFlag := flag.String("short", "", "short form flag")

	flag.Parse()

	// Retrieve the flag value using GetFlag
	selectedFlag := sh.GetFlag(longFlag, shortFlag, "")

	// Log success if a flag is correctly provided
	sh.Success(fmt.Sprintf("Selected flag: %s", selectedFlag))

	// Log a warning if something unusual happens
	sh.Warning("This is a warning message.")

	// Log an error and exit
	sh.Error("This is an error message.", true)
}

License

This package is open-source and available under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Error

func Error(s string, exit ...bool)

Error logs an error message and optionally exits the program. Parameters:

  • s: The error message to be logged.
  • exit: An optional boolean slice. If the first value is true, the program exits after logging the error.

Behavior:

  • Logs the error message with an "ERROR" prefix in red.
  • If `exit` is provided and true, the program terminates with a log.Fatal call.
  • Otherwise, the error is logged and the program continues execution.

func GetFlag

func GetFlag[T string | int | bool](long_flag, short_flag *T, value T, required bool, flag_name ...string) T

GetFlag checks the values of the long and short flags. If both flags are set to the default value or both are set to non-default values, it triggers an error. Otherwise, it returns the value of the flag that is set. Parameters:

  • long_flag: Pointer to the long-form of the flag.
  • short_flag: Pointer to the short-form of the flag.
  • value: Default value for the flags.
  • required: Boolean flag to indicate if the flag is required.
  • flag_name: Optional name of the flag to be used in the error message.s

Returns:

  • The value of the specified flag (long_flag or short_flag).

func Success

func Success(s string)

Success logs a success message with a "SUCCESS" prefix. Parameters:

  • s: The success message to be logged.

Behavior:

  • Logs the message in green with a "SUCCESS" prefix.

func Warning

func Warning(s string)

Warning logs a warning message with a "WARNING" prefix. Parameters:

  • s: The warning message to be logged.

Behavior:

  • Logs the message in yellow with a "WARNING" prefix.

Types

This section is empty.

Jump to

Keyboard shortcuts

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