Git Product home page Git Product logo

decli's Introduction

De(clarative) CLI

Purpose of DeCLI is to simplify parsing complex CLI arguments and defining rich CLI tools with minimal amount of required code.

DeCLI builds on top of cli. It relies heavily on defining CLI parameters as field tags in Golang structs instead of programmatically defining the arguments using Go code.

Why should I use it?

DeCLI enables you to write more concise code and will add type safety to your arguments.

As an illustration, here's a hello world example of a DeCLI:

package main

import (
    "fmt"
    "os"

    "github.com/draganm/decli"
)

type App struct {
    FirstName string `name:"first-name" usage:"your first name" aliases:"fn"`
    LastName  string `name:"last-name" usage:"your last name" aliases:"ln"`
    Age       int    `name:"age" usage:"your age"`
}

func (a *App) Run([]string) error {
    fmt.Printf("Hello %s %s (%d)\n", a.FirstName, a.LastName, a.Age)
    return nil
}

func main() {
    decli.RunAndFinish(&App{FirstName: "John", LastName: "Doe", Age: -1}, os.Args)
}

as opposed to using raw cli:

package main

import (
    "fmt"
    "log"
    "os"

    "github.com/urfave/cli/v2"
)

func main() {
    app := cli.App{
        Flags: []cli.Flag{
            &cli.StringFlag{
                Name:    "first-name",
                Usage:   "your first name",
                Aliases: []string{"fn"},
                Value:   "John",
            },
            &cli.StringFlag{
                Name:    "last-name",
                Usage:   "your last name",
                Aliases: []string{"ln"},
                Value:   "Doe",
            },
            &cli.IntFlag{
                Name:  "age",
                Usage: "your age",
                Value: -1,
            },
        },
        Action: func(c *cli.Context) error {
            fmt.Printf("Hello %s %s (%d)\n", c.String("first-name"), c.String("last-name"), c.Int("age"))
            return nil
        },
    }

    err := app.Run(os.Args)
    if err != nil {
        log.Fatalf("error: %s\n", err.Error())
    }
}

Both sources do the same, DeCLI version has almost the half of lines of code.

decli's People

Contributors

draganm avatar zimbatm avatar

Stargazers

Abiria avatar Samuel Rounce avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

tav zimbatm

decli's Issues

Change the metadata format

One issue with the current metadata format is that it's not specific to decli. This causes the following issues:

  • typos don't get caught. Metadata tags that are not understood are ignored.
  • the tags might be used by other libraries.

Instead, I propose to change the format from:

type MyStruct struct {
  FirstName string `name:"first-name" usage:"your first name" aliases:"fn"`
}

To:

type MyStruct struct {
  FirstName string `decli:"name=first-name usage='your first name' aliases=[fn]"`
}

This would use the https://github.com/zimbatm/lines format to parse the tag.

Access to parent flags

Currently there is no way to access to flags defined on the parent. Add either access to the parent, or add special tag from:"parent" or similar

not supported type "int32"

Issue

When I run this code,

type App struct {
	X    int32 `name:"x"    usage:"x coordinate"`
	Z    int32 `name:"z"    usage:"z coordinate"`
}

The error occurs.

error: while configuring decli: not supported type "int32"

Suggestion

It would be great if decli could support other primitive types like int32 and int64, etc.

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.