argv

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2021 License: MIT Imports: 1 Imported by: 0

README

Go Reference Coverage Status

argv is yet another command-line argument parser. However, this one doesn't expect anything from you. As a result, all of the responsibility of using this lies on you.

A simple example:

package main

import (
  "os"
  "log"

  "github.com/zrhmn/argv"
)

func main() {
  parsed := argv.New(os.Args[1:]).Parse()
  infile := os.Stdin
  verbosity := 0

  for _, optarg := range parsed.OptArgs {
    switch optarg[0] {
      case "-f", "--file":
        f, err := os.Open(optarg[1])
        if err != nil {
          log.Fatalf("could not open file: %v", err)
        }

        defer f.Close()
        infile = f

      case "-v":
        if len(optarg[1]) != 0 {
          log.Fatal("option -v does not accept a value")
        }

        verbosity += 1

      // ... etc.
      
      default:
        log.Fatalf("option provided but not recognized: %s", optarg[0])
    }
  }

  // ... do something with gathered information.
}

Documentation

Overview

Package argv implements a simple GNU-style command-line argument parser.

There are several other packages providing similar functionality, but most of them require a setup phase where a specification for the expected arguments is provided. In contrast, argv simply goes through a list of command-line arguments and classifies them as either positional arguments, flags or option-value pairs.

Simply do:

parsed := New(os.Args[1:]).Parse()

This simplicity has some limitations, for example not knowing whether an argument is a boolean flag or a list of strings before parsing leads to unexpected results. However, this can still be used to quickly parse and process a very simple list of arguments.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Argv

type Argv struct {
	PosArgs []string    // parsed positional arguments
	OptArgs [][2]string // parsed option:value pairs
	// contains filtered or unexported fields
}

Argv wraps a list of command-line arguments and facilitates parsing them.

func New

func New(args []string) *Argv

New returns a new Argv object.

func (*Argv) Parse

func (g *Argv) Parse() *Argv

Parse parses the command-line arguments contained in Argv.

Jump to

Keyboard shortcuts

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