skapt

package module
v0.0.0-...-fdaedd9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2018 License: MIT Imports: 9 Imported by: 2

README

skapt

Build Status Go Report Card GoDoc Coverage Status

Lightweight package for building command line apps in Go

I was inspired from other command line libraries to do my own package in Go.

experimental

API example

Example of command line with two arguments. One flag is required to be explicitly passed.

All the code in this example is written in one main.go file.

package main

import (
	"fmt"
	"os"
	"time"

	"github.com/hoenirvili/skapt"
	"github.com/hoenirvili/skapt/argument"
	"github.com/hoenirvili/skapt/flag"
)

func main() {
	app := skapt.Application{
		Name:        "Example",
		Description: "Example is an example of command line app",
		Version:     "1.0.0",
		Handler: func(ctx *skapt.Context) error {
			w := ctx.Int("wait")
			wait := time.Duration(w)
			exp := ctx.String("e")
			time.Sleep(wait * time.Second)
			_, err := fmt.Fprintf(ctx.Stdout, exp)
			return err
		},
		Flags: flag.Flags{{
			Short: "e", Long: "exp",
			Description: "Print something funny",
			Type:        argument.String,
			Required:	 true,
		}, {
			Short: "w", Long: "wait",
			Description: "How many seconds to wait until you print",
			Type:        argument.Int,
		}},
	}
	app.Exec(os.Args)
}

If we try to pass a long argument --help than we get this auto-generated output.

By default we append help and version flags if the Flags slice does not contain any of them.

Help output

$ : main --help
Usage: Example [OPTIONS] [ARG...]
       Example [ --help | -h | -v | --version ]

Example is an example of command line app

Options:

-e --exp      Print something funny
-w --wait     How many seconds to wait until you print
-h --help     Print out the help menu
-v --version  Print out the version of the program

For the version flag.

Version output

$ : main -v
Version 1.0.0
$ : main --version
Version 1.0.0

In our Application struct we declared 2 flags, -e/--exp and -w/--wait, if we pass valid values, the program sleeps 3 seconds and outputs the message passed in -e.

Run output

$ : main -e "Example text" --wait=3
Example text

Error output

We treat all basic errors by default.

$ : main
Option -e --exp is required

This also checks if the value passed is valid.

$ : main -w fjsuiajfsd
Cannot parse value "fjsiadufj" as int

FAQ

  1. Why should I use this package instead of a more popular one?

    Really, nothing is stopping you to use one that is more battle tested, a more matured package.
    The reason for developing another command line parsing package was that I often want to write
    a simple lightweight command line app and I don't need more features than some basic flag value checking
    and a simpler flag value retrieval api.

  2. Why not standard lib one?

    I didn't like the interface they provided and their flag value retrieval.

Documentation

Overview

Package skapt provides a tiny interface to create and manage your command line applications

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Application

type Application struct {
	// Name of the command line application
	Name string
	// Usage is the usage of the command line
	Usage string
	// Description holds the description of the command line
	Description string
	// Version is the version of the application
	Version string
	// Flags holds list of the root command
	Flags flag.Flags
	// NArgs minim required value args
	NArgs int
	// Handler is the root main handler
	Handler func(ctx *Context) error
	// Stdout place to write information that the user
	// needs to know
	Stdout io.Writer
	// Stderr place where all error messages should be written
	Stderr io.Writer
}

Application will hold all the information for creating and parsing the command line

func (Application) Exec

func (a Application) Exec(args []string) (err error)

Exec executes the command line based on the args provided

type Context

type Context struct {
	// Flags contains the parsed flags
	flag.Flags
	// Args additional command line arguments
	Args []string
	// Stdout writer to stdout
	Stdout io.Writer
	// Stdout writer to stderr
	Stderr io.Writer
}

Context holds context specific information for the current running handler

Directories

Path Synopsis
Package argument provides small set of types to parse and interpret command line arguments Package argument provides small set of types to parse and interpret command line arguments
Package argument provides small set of types to parse and interpret command line arguments Package argument provides small set of types to parse and interpret command line arguments
Package flag provides types and functions to create and manage your command line applications Package flag provides types and functions to create and manage your command line applications
Package flag provides types and functions to create and manage your command line applications Package flag provides types and functions to create and manage your command line applications

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL