Skip to content
/ argv Public
forked from whatl3y/argv

Small Go CLI argument parser

Notifications You must be signed in to change notification settings

Mrs4s/argv

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 

Repository files navigation

argv

Extremely minimalistic argument parser for Go CLI apps. There is only a single function Parse that returns a struct of all key/value pairs for arguments provided in addition to any arguments provided without corresponding keys.

Rules

  1. Any arguments that start with one or two hyphens (-arg or --arg) are considered keys
  2. The value immediately following a key is considered that key's value.
  • NOTE: if the value of a key starts with a hyphen, you need to wrap the value in quotes, otherwise it will be considered another key.
  1. If an argument is provided without a key, it will be included in a []string field of the returned struct called Nokeys.

Install

$ go get github.com/whatl3y/argv

Usage

The following script is an example of how to use the library.

// ./test.go
package main

import (
  "fmt"
  "os"
  "github.com/whatl3y/argv"
)

func main() {
  // theArgs is a:
  // struct{
  //  Nokeys []string
  //  Keys   map[string]string
  //}
  theArgs := argv.Parse(os.Args[1:])
  fmt.Println("Arguments:", theArgs)
}
$ go build
$ ./test -arg1 123 --arg2 456 nokeyarg
# Arguments: {[nokeyarg] map[arg1:123 arg2:456]}

$ ./test -arg1 123 --arg2 --arg3 456 nokeyarg --arg4
# Arguments: {[nokeyarg] map[arg1:123 arg2: arg3:456 arg4:]}

About

Small Go CLI argument parser

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%