argparse

package
v2.0.0-...-4b7107c Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2023 License: MIT Imports: 14 Imported by: 0

README

argparse

Description

A python-approximate golang commandline argument parser. The goal is to create a Command-line argument parser similar to python argparse. Obviously, golang will have some differences.

Usage

See example.

package main

import (
	"fmt"
	"github.com/sam-caldwell/go/v2/projects/argparse/argparse"
	"github.com/sam-caldwell/go/v2/projects/argparse/types"
	"log"
)

// main - Demonstration program

func main() {
	log.Println("starting example...")
	namespace := func() []string {
		// We only need argparse to parse then reduce the arguments...
		var args argparse.Arguments
		return args.
			ProgramName().
			Copyright(2023, "Sam Caldwell", "mail@samcaldwell.net>").
			Preamble("This is a description of our Arguments Object.").
			Postscript("This is the postfix string after help is dumped.").
			Add("all", "-a", "--all", types.Flag, true, false, "All flag").
			Add("number", "-n", "--number", types.Integer, true, 1337, "set number").
			Add("bool", "-b", "--bool", types.Boolean, true, false, "set boolean").
			ExitOnError().
			Parse().
			Reduce()
	}()
	// Afterward, we can deal with just the parsed information...
	fmt.Println(namespace.GetValue("all"))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Arguments

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

Arguments - Top-Level ArgParse struct

func (*Arguments) Add

func (arg *Arguments) Add(
	name string,
	short string,
	long string,
	typ types.ArgTypes,
	required bool,
	argDefault any,
	help string) *Arguments

Add - append the given Argument object to the list of Arguments.

func (*Arguments) Copyright

func (arg *Arguments) Copyright(year int, author string, email string) *Arguments

Copyright - Append the Copyright string to the preamble.

func (*Arguments) Count

func (arg *Arguments) Count() int

Count - Return a descriptor count

func (*Arguments) Dump

func (arg *Arguments) Dump() (output []string)

Dump - Dump the descriptor names as a list of strings and their values

func (*Arguments) ErrorCount

func (arg *Arguments) ErrorCount() int

ErrorCount - return a count of errors

func (*Arguments) Errors

func (arg *Arguments) Errors() (result []string)

Errors return a list of errors

func (*Arguments) ExitOnError

func (arg *Arguments) ExitOnError() *Arguments

ExitOnError - Exit on any error

func (*Arguments) Get

func (arg *Arguments) Get(name string) *descriptor.Descriptor

Get - append the given Argument object to the list of Arguments.

func (*Arguments) HasErrors

func (arg *Arguments) HasErrors() bool

HasErrors - return true if there are any errors in the Arguments struct

func (*Arguments) Help

func (arg *Arguments) Help() (text string)

Help - Print Argument usage (help text)

func (*Arguments) Parse

func (arg *Arguments) Parse() *Arguments

Parse - Parse os.Args

func (*Arguments) Postscript

func (arg *Arguments) Postscript(line string) *Arguments

Postscript - Add another line to the postfix set

func (*Arguments) Preamble

func (arg *Arguments) Preamble(line string) *Arguments

Preamble - Add another line to the prefix set

func (*Arguments) ProgramName

func (arg *Arguments) ProgramName() *Arguments

ProgramName - Set the program name from os.Args[0]

func (*Arguments) Reduce

func (arg *Arguments) Reduce() (result parsed.Namespace)

Reduce - Reduce the parsed arguments to a Namespace struct and return the same

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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