Code Monkey home page Code Monkey logo

lighter's Introduction

lighter

a lightweight cli and launcher

usage

The pattern is to create your flags, run Parse, then use your flags.

// create flag
name, err := lighter.NewStringFlag("name", "enter the user's name", true)

// parse flags
err = lighter.Parse()

// use flag
fmt.Println("Hi, my name is", name.Value())

The supported flag types are string, bool, and int64. To create a flag use NewStringFlag(), NewBoolFlag(), and NewInt64Flag() functions.

These functions require the name and description of the flag as well as declaring whether the flag is required or not. Flags that are set as required and are not entered as a command line argument will return an error when parsed.

example

// create a required string flag 
name, err := lighter.NewStringFlag("name", "enter the user's name", true)
if err != nil {
    // display help message
    lighter.HelpWithError(err)
    os.Exit(1)
}

// create a required int flag 
age, err := lighter.NewInt64Flag("age", "enter user's age", true)
if err != nil {
    // display help message
    lighter.HelpWithError(err)
    os.Exit(1)
}

// create an optional bool flag 
admin, err := lighter.NewBoolFlag("admin", "set admin privileges", false )
if err != nil {
    // display help message
    lighter.HelpWithError(err)
    os.Exit(1)
}

if err := lighter.Parse; err != nil {
    // display help message
    lighter.HelpWithError(err)
    os.Exit(1)
}

fmt.Println("name:", name.Value())
fmt.Println("age:", age.Value())
fmt.Println("is admin:", admin.Value())

The following:

./MyApp --name john --age 32

Will result in:

name: john
age: 32
is admin: false

If you want to check if an optional flag was entered, you can use the IsSet() method.

age, err := lighter.NewInt64Flag("age", "enter user's age", false)
checkErr(err)

err = lighter.Parse
checkErr(err)

if age.IsSet() {
    fmt.Println("The age was set to", age.Value())
}

lighter's People

Contributors

ianchildress avatar

Watchers

 avatar

lighter'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.