flag

package
v0.1.111 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2026 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package flag provides utilities for managing command flags with environment variable fallback support. It implements a priority system: flag value > environment variable > default value.

Index

Constants

View Source
const (
	// RPCURL is the flag name for RPC URL
	RPCURL = "rpc-url"
	// RPCURLEnvVar is the environment variable name for RPC URL
	RPCURLEnvVar = "ETH_RPC_URL"
	// DefaultRPCURL is the default RPC URL when no flag or env var is set
	DefaultRPCURL = "http://localhost:8545"
	// PrivateKey is the flag name for private key
	PrivateKey = "private-key"
	// PrivateKeyEnvVar is the environment variable name for private key
	PrivateKeyEnvVar = "PRIVATE_KEY"
)

Variables

This section is empty.

Functions

func GetPrivateKey

func GetPrivateKey(cmd *cobra.Command) (string, error)

GetPrivateKey retrieves the private key from the command flag or environment variable. Returns the flag value if set, otherwise the environment variable value, otherwise the default. Returns empty string and nil error if none are set.

func GetRPCURL

func GetRPCURL(cmd *cobra.Command) (string, error)

GetRPCURL retrieves the RPC URL from the command flag or environment variable. Returns the flag value if set, otherwise the environment variable value, otherwise empty string. Validates the URL format if a non-empty value is provided and returns an error if validation fails. Returns empty string and nil error if no value is set.

func GetRequiredPrivateKey

func GetRequiredPrivateKey(cmd *cobra.Command) (string, error)

GetRequiredPrivateKey retrieves the private key from the command flag or environment variable. Returns an error if the value is not set or empty.

func GetRequiredRPCURL

func GetRequiredRPCURL(cmd *cobra.Command) (string, error)

GetRequiredRPCURL retrieves the RPC URL from the command flag or environment variable. Returns the flag value if set, otherwise the environment variable value. Validates the URL format and returns an error if the value is not set, empty, or invalid.

func MarkFlagsRequired

func MarkFlagsRequired(cmd *cobra.Command, flagNames ...string)

MarkFlagsRequired marks one or more regular flags as required and logs a fatal error if marking fails. This helper ensures consistent error handling across all commands when marking flags as required.

func MarkPersistentFlagsRequired

func MarkPersistentFlagsRequired(cmd *cobra.Command, flagNames ...string)

MarkPersistentFlagsRequired marks one or more persistent flags as required and logs a fatal error if marking fails. This helper ensures consistent error handling across all commands when marking persistent flags as required.

func ParseGasUnit added in v0.1.111

func ParseGasUnit(s string) (uint64, error)

ParseGasUnit parses a gas value string with optional unit and returns the value in wei. This is a convenience function for use outside of flag parsing.

Types

type BigIntValue

type BigIntValue struct {
	Val *big.Int
}

BigIntValue is a custom flag type for big.Int values. It implements the pflag.Value interface to enable using *big.Int with Cobra flags.

func (*BigIntValue) Set

func (b *BigIntValue) Set(s string) error

Set parses a decimal string and sets the big.Int value.

func (*BigIntValue) String

func (b *BigIntValue) String() string

String returns the decimal string representation of the big.Int value.

func (*BigIntValue) Type

func (b *BigIntValue) Type() string

Type returns the type string for this flag value.

type GasValue added in v0.1.111

type GasValue struct {
	Val *uint64
}

GasValue is a custom flag type for gas values with unit support. It implements the pflag.Value interface to enable parsing values like "100gwei" or "1ether".

func (*GasValue) Set added in v0.1.111

func (g *GasValue) Set(s string) error

Set parses a gas value string with optional unit (e.g., "100gwei", "1000000000").

func (*GasValue) String added in v0.1.111

func (g *GasValue) String() string

String returns the string representation of the gas value in wei.

func (*GasValue) Type added in v0.1.111

func (g *GasValue) Type() string

Type returns the type string for this flag value.

Jump to

Keyboard shortcuts

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