Coder Social home page Coder Social logo

goclap's Introduction

goclap

Go Reference GitHub CI Go Report Card

go install github.com/steverusso/goclap@latest

A pre-build tool to generate command line argument parsing code from Go comments. The idea is inspired by the clap Rust crate, specifically its use of documentation and proc macros.

Example

The following is taken from examples/simple/main.go.

//go:generate goclap -type mycli

...

// Print a string with the option to make it uppercase.
type mycli struct {
	// Make the input string all uppercase.
	//
	// clap:opt upper
	toUpper bool
	// The input string.
	//
	// clap:arg_required
	input string
}

func main() {
	c := mycli{}
	c.Parse(os.Args[1:])

	s := c.input
	if c.toUpper {
		s = strings.ToUpper(s)
	}

	fmt.Println(s)
}

By running go generate (assuming goclap is installed), the mycli struct, its fields, and their comments will be used to generate code for parsing command line arguments into a mycli. That code will be placed in a file named clap.gen.go (see the simple example's one). The program can then be built with go build.

Running ./simple -u hello will output "HELLO", and running ./simple -h will output the following help message:

simple - Print a string with the option to make it uppercase

usage:
   simple [options] <input>

options:
   -upper   Make the input string all uppercase
   -h       Show this help message

arguments:
   <input>   The input string

Building

To just build the project as is, run go build. If you have task installed, you can run task get-tools to install the latest versions of (goimports, gofumpt, and staticcheck). Once you have those tools, you can run task to format, build, and lint the code, or you can run task install to format, lint, and install goclap.

Projects Using Goclap

License

This is free and unencumbered software released into the public domain. Please see the UNLICENSE file for more information.

goclap's People

Contributors

russoj88 avatar steverusso avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

goclap's Issues

Add Environment Variable Support

I like the idea of using comments to get program configuration, but I use environment variables instead of command line arguments. Can goclap support this, similar to env?

Add an Extension to Generated Code File

To make it additionally obvious that the generated code file is generated, can goclap add a file extension (ex: file.gen.go)?

As an example, protobuf's generated files have pb in their name (ex: file.pb.go).

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.