GithubHelp home page GithubHelp logo

rung's Introduction

rung

A code generating utility to make Go CLI flag parsing less painful.

Example

Firstly, the Go code (main.go):

package main

import (
	"fmt"
	"io"
)

//go:generate sh flags.sh

func run(in io.Reader, out io.Writer, flags flagSet) {
	fmt.Println(flags.fooflag.Value)
	if flags.barflag.WasSet {
		fmt.Printf("do something with %v\n", flags.barflag.Value)
	}
}

Then we just need a script to call the generation code (in flags.sh):

go run github.com/jamespwilliams/rung/rung_gen \
    -foo:int fooflag 10 "the foo flag" \
    -bar:bool barflag true "the bar flag" \
    -quux:float64 quuxflag 10.241 "the quux flag"

That's it.

Then, we can run for example:

[jpw@xyz]$ go generate .

[jpw@xyz]$ go run . -foo 10000
10000

[jpw@xyz]$ go run . -foo 10000 -bar false
10000
do something with true

[jpw@xyz]$ go run . -quux 3.1415
10

[jpw@xyz]$ go run . -invalid
flag provided but not defined: -invalid
Usage of /run/user/1000/go-build723213117/b001/exe/rungtest:
  -bar
        the bar flag (default true)
  -foo int
        the foo flag (default 10)
  -quux float
        the quux flag (default 10.241)

Note that I'm using a separate flags.sh script because go:generate directives don't support multiline commands.

See this article by Mat Ryer for an explanation of why abstracting main out into a separate run function is useful.

Usage

Really, this is more of a proof-of-concept, but anyway: the rung_gen binary accepts argument quadruples of the following form:

-flag:type name default "usage string"

Future Ideas

The expected run function signature could be changed such that an error is expected, which could then be log.Fatal'd, for example.

rung's People

Contributors

jamespwilliams avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.