GithubHelp home page GithubHelp logo

wrfly / gua Goto Github PK

View Code? Open in Web Editor NEW
8.0 2.0 0.0 22 KB

simple Golang command-line parser, if you don't need some fancy features, just guá it.

License: Apache License 2.0

Go 100.00%
golang flags go cli cli-parser

gua's Introduction

guá

Somehow I saw this repo and want to rewrite a same one using ecp.

If you want to convert a golang structure into some command line flags, just guá.

It's small and useful for some simple command line tools.

Example

package main

import (
    "encoding/json"
    "fmt"
    "time"

    "github.com/wrfly/gua"
)

type cliFlags struct {
    Name     string        `name:"nnnnname" default:"wrfly" desc:"just a name"`
    Age      int           `desc:"the age"`
    Slice    []string      `desc:"test string slice"`
    SliceInt []int         `desc:"test int slice" default:"1 2 3"`
    Time     time.Duration `desc:"test time duration"`
    Extra    struct {
        Loc   string `default:"home" desc:"location"`
        Valid bool   `default:"true"`
        Debug bool
    }
    Type string `desc:"A|B|C"`
}

func main() {
    cli := new(cliFlags)
    if err := gua.Parse(cli); err != nil {
        panic(err)
    }

    bs, _ := json.MarshalIndent(cli, "", "  ")
    fmt.Printf("%s\n", bs)
}
# run the example
./gua
{
  "Name": "wrfly",
  "Age": 0,
  "Slice": null,
  "SliceInt": [
    1,
    2,
    3
  ],
  "Time": 0,
  "Extra": {
    "Loc": "home",
    "Valid": false,
    "Debug": false
  },
  "Type": ""
}


# show some help message
./gua -h
Usage of ./gua:
 -age           the age
 -extra.debug   [false]
 -extra.loc     location   [home]
 -extra.valid   [false]
 -nnnnname      just a name   [wrfly]
 -slice         test string slice
 -sliceInt      test int slice   [1 2 3]
 -time          test time duration
 -type          A|B|C


# add some flags
./gua -age 18 -extra.debug -nnnnname gua \
    -slice "hello world" -sliceInt "1 3 5 7" \
    -time 1m -type C
{
  "Name": "gua",
  "Age": 18,
  "Slice": [
    "hello",
    "world"
  ],
  "SliceInt": [
    1,
    3,
    5,
    7
  ],
  "Time": 60000000000,
  "Extra": {
    "Loc": "home",
    "Valid": false,
    "Debug": true
  },
  "Type": "C"
}

gua's People

Contributors

wrfly avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

gua's Issues

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.