Giter Site home page Giter Site logo

goargs's Introduction

goargs

Use command-line arguments as easy as:

var args struct {
	Host string `goargs:"short:h"`
	User string
}
goargs.Bind(&args)
goargs.Parse()

fmt.Printf("Host: %s\n", args.Host)
See full file
package main

import (
	"fmt"
	"os"

	"github.com/PKeidel/goargs"
)

var args struct {
	Host string `goargs:"short:h"`
	User string
}

func main() {
	goargs.Bind(&args)
	goargs.Parse()

	fmt.Printf("Host: %s\n", args.Host)
	fmt.Printf("User: %s\n", args.User)

	fmt.Println("To rerun, run:", os.Args[0], goargs.String())
}

Now you can run your program for example with: program --user PKeidel -h host01

strcase.ToKebab is applied to all "long" names. So a value like UserName will become --user-name as an arg.

Tags

There are a few tags to set/overrite the long form, short form and the required flag per field. long defaults to the field name in lowercase.

var args struct {
	Host string `goargs:"long:host,short:h,required"`
	User string
}

String()

goargs.String() returns a command-line variant of the current values. This can be usefull if the values are combined from different inputs but you want to rerun your program with the exact same arguments.

fmt.Println("To rerun, run:", os.Args[0], goargs.String())
// To rerun, run: program --host host01

Sources

goargs will search for the values in your os.Args, os.Environ and in the future in config files (json, yaml).

The order in which values overrite eachother can be configured with goargs.DefaultSourceOrder and defaults to: SourceFile, SourceEnv, SourceArgs with args overriding all other values.

Without structs

You can also register single values. For example:

var host string

func main() {
	goargs.WithString("host", &host)
	goargs.Parse()

	fmt.Printf("Host: %s\n", host)
}

To set for example the Required flag, you need to use the reference returned from the With* functions:

argHost := goargs.WithStringS("host", "h", &host)
argHost.Required = true
goargs.Parse()

Roadmap

[x] Support strings
[x] Support ints
[x] Support flags (bool)
[x] Support require flag
[x] Support os.Args parsing
[x] Support env parsing
[ ] Support multi flag to allow arrays like `--host host01 host02`
[ ] Support config file parsing
[ ] Detect undefined args
[ ] Detect wrong types
[x] Support externally changed values
[ ] Support values with spaces, quotes, ...
[ ] Print useage

goargs's People

Contributors

pkeidel avatar

Watchers

 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.