Categorygithub.com/clagraff/argparse
modulepackage
1.0.2
Repository: https://github.com/clagraff/argparse.git
Documentation: pkg.go.dev

# README

argparse

Project Status

⚠️ No Longer Supported ⚠️

There are a variety of Golang command-line libraries currently available. Right now development on argparse has stagnated. I am planning on revamp the library to attempt to bring it back to its roots, as well as improve the underlying code.

Try one of the libraries here, instead: https://github.com/avelino/awesome-go#command-line




CircleCI GoDoc Go Report Card

Contents

Description

clagraff/argparse is a golang library for command-line argument parsing. It is heavily influenced by the functionallity found in Python3's argparse package.

clagraff/argparse places a focus on method-chaining for setting up options, flags, and parsers, and supports a variety of features.

Install

Stable V1.x.x version

go get gopkg.in/clagraff/argparse.v1

Development version

$ go get github.com/clagraff/argparse

Boom! All set! Feel free to read on for examples on getting started and using this package.

The Basics

Create a parser

Here we have a basic program which greets a user by a provided name, optionally in uppercase. We need create a parser, include our option and flag, and then parse our programs arguments. It could look something like:

package main

import (
	"fmt"
	"os"
	"strings"

	"github.com/clagraff/argparse"
)

func callback(p *argparse.Parser, ns *argparse.Namespace, leftovers []string, err error) {
	if err != nil {
		switch err.(type) {
		case argparse.ShowHelpErr, argparse.ShowVersionErr:
			// For either ShowHelpErr or ShowVersionErr, the parser has already
			// displayed the necessary text to the user. So we end the program
			// by returning.
			return
		default:
			fmt.Println(err, "\n")
			p.ShowHelp()
		}

		return // Exit program
	}

	name := ns.Get("name").(string)
	upper := ns.Get("upper").(string) == "true"

	if upper == true {
		name = strings.ToUpper(name)
	}

	fmt.Printf("Hello, %s!\n", name)
	if len(leftovers) > 0 {
		fmt.Println("\nUnused args:", leftovers)
	}
}

func main() {
	p := argparse.NewParser("Output a friendly greeting", callback).Version("1.3.0a")
	p.AddHelp().AddVersion() // Enable help and version flags

	upperFlag := argparse.NewFlag("u", "upper", "Use uppercase text").Default("false")
	nameOption := argparse.NewArg("n name", "name", "Name of person to greet").Default("John").Required()

	p.AddOptions(upperFlag, nameOption)

	// Parse all available program arguments (except for the program path).
	p.Parse(os.Args[1:]...)
}

You could then run it and receive the following output:

> go run main.go Luke
Hello, Luke!

> go run main.go Vader -u
Hello, VADER!

> go run main.go
n, name: too few arguments

usage: main [-h] [-v] [-u] n NAME

Output a friendly greeting

positional arguments:
  n NAME       Name of person to greet

optional arguments:
  -h, --help     Show program help
  -v, --version  Show program version
  -u, --upper    Use uppercase text

Arguments

Arguments are command-line values passed to the program when its execution starts. When these values are expected by the program, we use a convention of classifying these arguments into two types: Flags and Options.

Types

Flags

Flags represent non-positional, boolean arguments. These arguments are "false" by default, will utilize the argparse.StoreTrue action. They do not consume any additional arguments other than themselves. You can create a new flag using:

// Create a short and long flag for "use-default", -d --default, with a description.
use_default := argparse.NewFlag("d default", "use-default", "Enable the default mode")

argparse.Option is the struct used for creating parseable options.

Options

Options are arguments which store/represent one or more values. While options can represent a variety of input types, they are always serialized to a string. Options may be required or not, may be positional or not, can have a variable number of parameters, and can operate in a variety of manners.

Here is an example of an option foo, which has a default value of bar, is required, and expects 1 additional argument which will be stored as foo's value

// Create a required Option -foo, with a default value of "bar", that expects and stores 1 argument.
f := argparse.NewOption("-f --foo", "foo", "A foo option").Default("bar").Nargs("1").Required().Action(argparse.Store)

Methods

Options can be configured in a variety of ways. Therefore, method-chaining is heavily used to quickly create and setup an option. Consider the following example:

// Create a required Option -foo, with a default value of "bar", that expects and stores 1 argument.
f := argparse.NewOption("-f --foo", "foo", "A foo option").Default("bar").Nargs("1").Required().Action(argparse.Store)

Options can have the following attributes:

  • Is required or not required
  • Is positional or not positional
  • Has a default value
  • Has a constant value
  • Expects a specified number of arguments (or no arguments)
  • Is identified by one or more public qualifiers (e.g.: -f or --foo)
  • Can require arguments to match specified choices

Nargs

Nargs, a shortening of "numer of arguments", represents the number of arguments a flag expects after its presence in a programs complete list of parameters. This could be an actual number, such as 0 or 5, or it could be any of the following characters: *+?.

The * character represents "any and all arguments" following the flag.

The + character represents "one or more arguments" following the flag.

The ? character represents "no arguments or only one argument" following the flag.

The r or R characters represent "all remaining arguments" that were not consumed during parsing. These narg choices do not consume the parse arguments they are applicable to.

Actions

A flag's action defines what should occur when a flag is parsed. All flags must have an action. By default, a flag will store true in the parser when present, and false when not. The following are the currently available actions:

  • argparse.StoreTrue will store true in the parser when the flag is present.
  • argparse.StoreFalse will store false in the parser when the flag is present.
  • argparse.StoreConst will store the flag's constant value into the parser when the flag is present.
  • argparse.Store will store the appropriate number of arguments into the parser when the flag & arguments are present.
  • argparse.AppendConst will append the flag's constant to the flag's slice within the parser.
  • argparse.Append will append the appropriate number of arguments into the flag's slice within the parser.
  • argparse.ShowHelp will print the parser's generate help text to stdout.

# Functions

Append retrives the appropriate number of argumnents for the current option, (if any), and appends them individually into the parser.
AppendConst appends the option's constant value into the parser.
NewArg initializes a new Option pointer, and sets its Nargs to 1, its action to Store, and makes it a positional option.
NewFlag initializes a new Option pointer, sets its Nargs to 0, its action to StoreTrue, and its default value to false.
NewNamespace will return a pointer to a new Namespace instance.
NewOption instantiates a new Option pointer, initializing it as a boolean flag.
NewParser returns an instantiated pointer to a new parser instance, with a description matching the provided string.
ShowHelp calls the parser's ShowHelp function to output parser usage information and help information for each option to stdout.
ShowVersion calls the parser's ShowVersion function to output parser/program version information.
Store will attempt to store the appropriate number of arguments for the option, (if any), into the parser.
StoreConst stores the option's constant value into the parser.
StoreFalse stores a boolean `false` into the parser.
StoreTrue stores a boolean `true` into the parser.
ValidateChoice returns an error if the provided interface value does not exists as valid choice for the provided flag.
ValidateType attempt to type-convert the string argument to the flag's desired type.

# Structs

InvalidChoiceErr indicates that an argument is not among the valid choices for the option.
InvalidFlagNameErr indicates that an argument with the provided public name not exist.
InvalidOptionErr indicates that an option is invalid.
InvalidParserNameErr indicates that a Command name has already been assigned and cannot be re-assigned.
InvalidTypeErr indicates that an argument cannot be casted the the option's expected type.
MissingEnvVarErr indicates that an environmental variable could not be found with the provided variable name.
MissingOneOrMoreArgsErr indicated that not enough arguments were provided, when one or more arguments were expected, for the option.
MissingOptionErr indicated that an option was required but is missing.
MissingParserErr indicated that commands were available, but none were used.
Option contains the necessary attributes for representing a parsable option.
Parser contains program-level settings and information, stores options, and values collected upon parsing.
ShowHelpErr indicates that the program was instructed to show it's help text.
ShowVersionErr indicates that the program was instructed to show it's versioning text.
SubParser contains a Parser pointer and the public name for the sub command.
TooFewArgsErr indicated that not enough arguments were provided for the option.

# Type aliases

Action is type to represent a callable function which will operate on a parser, a option, and an array of argument strings.
Namespace is a map of key-value pairs, used for storing pairings between options' destinations and their associated values.