getopt

package module
v0.0.0-...-a434eb5 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2020 License: GPL-3.0 Imports: 3 Imported by: 0

README

getopt

a traditional getopt library for golang.

example

package main

import (
    "fmt"
    "os"
    "github.com/chiyouhen/getopt"
)

func Usage() {
    fmt.Println("usage: xxx")
}

func main() {
    var configPath = "/user/local/etc/hello.conf"
    var opts, args, err = getopt.Getopt(os.Args[1:], "hc:", []string{"--help", "--config="})
    if err != nil {
        fmt.Printf("error while getopt: %v\n", err)
        Usage()
        os.Exit(1)
    }

    for _, opt := range opts {
        var k, v = opt[0], opt[1]
        switch k {
            case "-h", "--help":
                Usage()
                os.Exit(0)
            case "-c", "--config":
                configPath = v
        }
    }
    fmt.Printf("configPath: %s\n", configPath)
    fmt.Println(opts, args)
}

functions

func Getopt(tokens []string, shortopts string, longopts []string) (opts [][]string, args []string, err error)

arguments

tokens

usually os.Args[1:], developer can also specify a different string array.

shortopts

a traditional shortopt definition. a singal letter means this is a flag, as shown in the example '-h'. a single letter followed by a ':' means this is a option, user must specify option value while execute program, as shown in the example '-c'.

longopts

an array of string. a option defined as --option_name[=]. '--' prefix is required. with '=' as suffix means this is a option, without '=' followed means this is a flag.

return values

opts

an array of key-value pair. in each key-value pair, item 0 is key, item 1 is value, both are string. the sequense is the same they appears in command line arguments.

args

an array of string contains all arguments which are not option nor value of option. and all arguments after '--'.

others

can any body help me to improve my English! it is the biggest difficulty for me while coding!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DoLongs

func DoLongs(longoptions map[string]bool, cmd string, tokens *[]string, opts *[][]string, args *[]string) (err error)

func DoShorts

func DoShorts(shortoptions map[string]bool, cmd string, tokens *[]string, opts *[][]string, args *[]string) (err error)

func Getopt

func Getopt(tokens []string, shortopts string, longopts []string) (opts [][]string, args []string, err error)

func ParseLongOptions

func ParseLongOptions(longopts []string) (longoptions map[string]bool, err error)

func ParseShortOptions

func ParseShortOptions(shortopts string) (shortoptions map[string]bool, err error)

Types

type GetoptError

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

func (*GetoptError) Error

func (err *GetoptError) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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