goopts

command module
v1.2.4 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2025 License: MIT Imports: 2 Imported by: 0

README

goopts, a Go library to parse arguments given in command line to a program.
Build and Release GitHub release (latest by date) YouTube Channel Subscribers

goopts is a powerful and flexible library designed to simplify the parsing of command-line arguments in Go applications. Inspired by popular argument parsing libraries from other languages, goopts aims to provide developers with a robust, easy-to-use interface for handling various argument types, including positional arguments, named options, flags, and more. The library also supports advanced features like argument groups, mutually exclusive options, and dependency handling, making it an excellent choice for building feature-rich CLI tools.

Whether you are developing a simple utility or a complex command-line application, goopts offers the tools you need to parse and manage user input efficiently and effectively.

Purpose

The purpose of goopts is to:

  • Provide a straightforward and intuitive way to define and parse command-line arguments.
  • Support multiple types of arguments including strings, integers, booleans, lists, and maps.
  • Enable the grouping of arguments, including mutually exclusive groups and dependent arguments.
  • Offer clear, customizable help messages for your command-line tools.
  • Allow for flexible configuration and easy integration into existing Go projects.

Documentation

Example code

Here is an example code to illustrate a few of the features of goopts:

import (
	"fmt"

	"github.com/p0dalirius/goopts/parser"
)

var (
	// Positional arguments
	filePath     string
	outputFolder string

	// Authentication flags
	dbHost     string
	dbUsername string
	dbPassword string
	dbPort     int

	// Server settings flags
	serverIP   string
	serverPort int

	// Feature flags
	enableLogging     bool
	disableEncryption bool
)

func parseArgs() {
	// Create a new arguments parser with a custom banner
	ap := parser.ArgumentsParser{Banner: "PoC of goopts parsing v.1.1 - by Remi GASCOU (Podalirius)"}

	// Define positional arguments
	ap.NewStringPositionalArgument(&filePath, "filepath", "Path to the input file.")
	ap.NewStringPositionalArgument(&outputFolder, "outputfolder", "Destination folder for output.")

	// Define global flags
	ap.NewBoolArgument(&enableLogging, "-l", "--enable-logging", true, "Enable logging during execution.")
	ap.NewBoolArgument(&disableEncryption, "-e", "--disable-encryption", false, "Disable encryption for data transfer.")

	// Define an argument group for database authentication
	group_dbAuth, err := ap.NewArgumentGroup("Database Authentication")
	if err != nil {
		fmt.Printf("[error] Error creating ArgumentGroup: %s\n", err)
	} else {
		group_dbAuth.NewStringArgument(&dbHost, "-H", "--db-host", "", true, "Hostname or IP of the database server.")
		group_dbAuth.NewStringArgument(&dbUsername, "-U", "--db-username", "", true, "Username for database authentication.")
		group_dbAuth.NewStringArgument(&dbPassword, "-P", "--db-password", "", true, "Password for database authentication.")
		group_dbAuth.NewIntArgument(&dbPort, "-p", "--db-port", 3306, false, "Port number of the database server.")
	}

	// Define an argument group for server settings
	group_serverSettings, err := ap.NewArgumentGroup("Server Settings")
	if err != nil {
		fmt.Printf("[error] Error creating ArgumentGroup: %s\n", err)
	} else {
		group_serverSettings.NewStringArgument(&serverIP, "-i", "--server-ip", "", true, "IP address of the server to connect.")
		group_serverSettings.NewTcpPortArgument(&serverPort, "-s", "--server-port", 8080, false, "Port on which the server listens.")
	}

	// Parse the flags
	ap.Parse()
}

This results in the following arguments and usage printing:

Contributing

Pull requests are welcome. Feel free to open an issue if you want to add other features.

Credits

  • @p0dalirius for the creation of the goopts library before transferring it to TheManticoreProject.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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