• Stars
    star
    43
  • Rank 623,371 (Top 13 %)
  • Language
    Go
  • License
    The Unlicense
  • Created almost 6 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A collection of CLI argument types for the Go `flag` package.

flagvar

Test

A collection of CLI argument types for the flag package.

import "github.com/sgreben/flagvar"

Or just copy & paste what you need. It's public domain.

Example

package main

import (
	"flag"
	"fmt"
	"github.com/sgreben/flagvar"
)

var (
	fruit    = flagvar.Enum{Choices: []string{"apple", "banana"}}
	urls     flagvar.URLs
	settings flagvar.Assignments
)

func main() {
	flag.Var(&fruit, "fruit", fmt.Sprintf("set a fruit (%s)", fruit.Help()))
	flag.Var(&urls, "url", "add a URL")
	flag.Var(&settings, "set", fmt.Sprintf("specify a setting (%s)", settings.Help()))
	flag.Parse()
}
$ go run main.go -set abc=xyz -url https://github.com
# no error

$ go run main.go -set abc=xyz -url ://github.com
invalid value "://github.com" for flag -url: parse ://github.com: missing protocol scheme

$ go run main.go -fruit kiwi
invalid value "kiwi" for flag -fruit: "kiwi" must be one of [apple banana]

$ go run main.go -h
Usage:
  -fruit value
        set a fruit (one of [apple banana])
  -set value
        specify a setting (a key/value pair KEY=VALUE)
  -url value
        add a URL

Conventions

  • Pluralized argument types (e.g. Strings, Assignments) can be specified repeatedly, the values are collected in a slice.
  • The resulting value is stored in .Value for singular types and in .Values for plural types
  • The original argument string is stored in .Text for singular types and in .Texts for plural types
  • -Set types (EnumSet, StringSet) de-duplicate provided values.
  • -CSV types (IntsCSV, EnumsCSV) accept comma-separated values and accumulate values across flag instances if their .Accumulate field is set to true.
  • Most types implement interface{ Help() string }, which produces a string suitable for inclusion in a help message.

Types

Here's a compact overview:

flagvar type example CLI arg type of resulting Go value
Alternative
Assignment KEY=VALUE struct{Key,Value}
Assignments KEY=VALUE []struct{Key,Value}
AssignmentsMap KEY=VALUE map[string]string
CIDR 127.0.0.1/24 struct{IPNet,IP}
CIDRs 127.0.0.1/24 []struct{IPNet,IP}
CIDRsCSV 127.0.0.1/16,10.1.2.3/8 []struct{IPNet,IP}
Enum apple string
Enums apple []string
EnumsCSV apple,banana []string
EnumSet apple []string
EnumSetCSV apple,banana []string
File ./README.md string
Files ./README.md []string
Floats 1.234 []float64
FloatsCSV 1.234,5.0 []float64
Glob src/**.js glob.Glob
Globs src/**.js []glob.Glob
Ints 1002 []int64
IntsCSV 123,1002 []int64
IP 127.0.0.1 net.IP
IPs 127.0.0.1 []net.IP
IPsCSV 127.0.0.1,10.1.2.3 []net.IP
JSON '{"a":1}' interface{}
JSONs '{"a":1}' []interface{}
Regexp [a-z]+ *regexp.Regexp
Regexps [a-z]+ []*regexp.Regexp
Strings "xyxy" []string
StringSet "xyxy" []string
StringSetCSV y,x,y []string
TCPAddr 127.0.0.1:10 net.TCPAddr
TCPAddrs 127.0.0.1:10 []net.TCPAddr
TCPAddrsCSV 127.0.0.1:10,:123 []net.TCPAddr
Template "{{.Size}}" *template.Template
Templates "{{.Size}}" []*template.Template
TemplateFile "/path/to/template.file" string
Time "10:30 AM" time.Time
Times "10:30 AM" []time.Time
TimeFormat "RFC3339" string
UDPAddr 127.0.0.1:10 net.UDPAddr
UDPAddrs 127.0.0.1:10 []net.UDPAddr
UDPAddrsCSV 127.0.0.1:10,:123 []net.UDPAddr
UnixAddr /example.sock net.UnixAddr
UnixAddrs /example.sock []net.UnixAddr
UnixAddrsCSV /example.sock,/other.sock []net.UnixAddr
URL https://github.com *url.URL
URLs https://github.com []*url.URL
Wrap
WrapCSV
WrapFunc
WrapPointer

Goals / design principles

  • Help avoid dependencies
    • Self-contained > DRY
    • Explicitly support copy & paste workflow
    • Copyable units should be easy to determine
    • Anonymous structs > shared types
  • "Code-you-own" feeling, even when imported as a package
    • No private fields / methods
    • No magic
    • Simple built-in types used wherever possible
    • Avoid introducing new concepts
  • Support "blind" usage
    • Zero values should be useful
    • Avoid introducing failure cases, handle any combination of parameters gracefully.
    • All "obvious things to try" should work.

More Repositories

1

jp

dead simple terminal plots from JSON data. single binary, no dependencies. linux, osx, windows.
Go
1,302
star
2

yeetgif

gif effects CLI. single binary, no dependencies. linux, osx, windows. #1 workplace productivity booster. #yeetgif #eggplant #golang
Go
551
star
3

tj

stdin line timestamps. single binary, no dependencies. osx & linux & windows. plays well with jq.
Go
230
star
4

http-file-server

tiny portable HTTP file server. single binary, no dependencies. linux, osx, windows. #golang
Go
199
star
5

ok

OCaml implementation of the K 2.0 array programming language
OCaml
80
star
6

regex-builder

Write regular expressions in pure Java
Java
61
star
7

slack-emoji-upload

Slack emoji uploader, CLI. single binary, no dependencies. linux, osx, windows.
Go
31
star
8

testing-with-gomock

Supporting materials for the blog post "Testing with GoMock"
Go
26
star
9

piecewiselinear

tiny linear interpolation library for go
Go
26
star
10

jira-project-export

Export issues and metadata for a single JIRA project as JSON.
Shell
19
star
11

subst

substitutes $variables in text. single binary, no dependencies. linux, osx, windows.
Go
14
star
12

render

A flexible go-template renderer.
Go
13
star
13

ezrgif

Easy GIF effects for Slack/Discord emoji, from the command line
Shell
13
star
14

sshtunnel

library providing a convenient dialer for SSH-tunneled TCP and Unix domain socket connections. #golang
Go
13
star
15

telegram-emoji-reactions-bot

Telegram bot for Slack-like emoji reactions. Single binary, no dependencies. linux, osx, windows. #golang #cli #telegram #slack
Go
11
star
16

image-palette-tools

extract palettes from images / cluster images by their palettes
Go
11
star
17

ocaml-rename

An .mli-aware command-line renaming tool for OCaml projects
OCaml
11
star
18

telegram-sticker-thief-bot

Telegram bot to clone/re-combine sticker packs. Single binary, no dependencies. linux, osx, windows. #golang #cli #telegram
Go
9
star
19

ocaml-redis-protocol

Redis Serialization Protocol (RESP) for OCaml
OCaml
9
star
20

FsTypeVis

Simple visualisation for F# types
F#
8
star
21

with-ssh-docker-socket

access a remote Docker daemon over SSH. single binary, no dependencies. linux, osx, windows.
Go
7
star
22

digitalproductid

decodes the Windows 7/8/10 license key from the registry
Go
7
star
23

tcp-time

measures TCP connection durations to a given target, prints JSON
Go
7
star
24

versions

command-line version operations. single static binary. osx, linux, windows.
Go
5
star
25

docker-awscli

Docker image for the AWS CLI (automatically tracks AWS CLI releases)
Dockerfile
4
star
26

terrafile-ify

Generate Terrafiles and (optionally) re-write Terraform source to use vendored modules
Go
4
star
27

symbolic-plc

Symbolic execution for IEC 61131 PLCs using F# and Z3
F#
4
star
28

url

command-line URL parser. single binary, no dependencies. osx & linux & windows.
Go
4
star
29

0sh

very simple (sub-POSIX) shell for scripting. single binary, no dependencies. osx, linux, windows. #golang
Go
3
star
30

stdin-spinner

terminal spinner indicating whether data is coming in on stdin. single binary, no dependencies. linux, osx, windows.
Go
3
star
31

gopass-jsonapi-encode

reads JSON on stdin and writes gopass JSONAPI messages on stdout. single binary, no dependencies. linux, osx, windows. #golang
Go
3
star
32

http-echo

tiny HTTP server that echoes the requests it receives. single binary, no dependencies. linux, osx, windows.
Go
3
star
33

caretStack

R package for stacking caret models
R
2
star
34

docker-awscli-with-assume-role

Docker image for the AWS CLI with a wrapper that automatically assumes an IAM role (upstream AWS CLI releases are tracked automatically)
Shell
2
star
35

watchfs

docker-aware nodemon-like filesystem event watcher. single binary, no dependencies. linux, osx, windows.
Go
1
star
36

rc

A rule checker library for Java. Checks sets of rules for completeness, overlap, and constraint satisfaction.
Java
1
star
37

docker-awsebcli-with-assume-role

Docker image for the AWS EB CLI with a wrapper that automatically assumes an IAM role (upstream AWS EB CLI releases are tracked automatically)
Shell
1
star
38

http-subst-server

tiny static file server, with dead simple templates (just $VARIABLES). single binary, no dependencies. linux, osx, windows. #golang
Go
1
star
39

docker-awsebcli

Docker image for the AWS Elastic Beanstalk CLI (automatically tracks AWS EB CLI releases)
Dockerfile
1
star
40

slack-wipe

deletes all your messages and/or files in a single Slack channel. single binary, no dependencies. linux, osx, windows. #golang
Go
1
star
41

csv-dl

csv-dl reads CSV on stdin, and downloads linked files. single binary, no dependencies. linux, osx, windows. #golang
Go
1
star