Skip to content

zrhmn/argv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.
}

About

Simple command-line argument parser in Go.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages