Giter VIP home page Giter VIP logo

console's Introduction

console

A minimalist framework for beautiful CLI applications written in Go.

image

Setup

As a dependency

go get github.com/jordanbrauer/console

For development

  1. Clone (or fork) the repository
  2. Checkout a new branch from trunk
  3. ???
  4. PROFIT!!!

Happy hacking!

Usage

Creating new commands

Commands are defined as variables using a struct that's properties are act as a form of configuration. At a minimum, your command should have a Name and Run property defined.

var greet = &console.Command{
    Name: "greet",
    Description: "fubar snafu",
    Run: func(fubar *Command) ExitCode {
        println("hello, world!")

        return 0
    }}

var noop = &console.Command{
    Name: "noop",
    Description: "nada, zip, ziltch, nothing",
    Run: func() ExitCode {
        return 0
    }}

Adding sub commands

Commands are recursive. That is to say that they can have commands of their own. To do this, simply populate the Commands property with one or more Command pointers, instead of defining a Run function.

var parent = &console.Command{
    Name: "foo",
    Description: "i am the parent",
    Commands: []*Command{child},
}

var child = &console.Command{
    Name: "bar",
    Description: "i am the child",
    Run: func() ExitCode {
        return 0
    }}

which would be used like so

go run main.go foo bar

Configuring the CLI

Once you have some commands, you can create a new CLI app, give it a version, and register one or more commands to be executed by the user.

cli := console.New("your version")

cli.Register(noop, greet)

cli.Splash(func () string {
    // logo, author(s), version, licensing, whatever you want...
})

Running the CLI

The easiest part is running the app. It's a good idea to pass the execution result to os.Exit so that the user's operating system can see the exit code returned by the executed command.

os.Exit(cli.Run())

console's People

Contributors

jordanbrauer avatar

Stargazers

 avatar

Watchers

 avatar  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.