Skip to content
Simple, easy to use input handler for the CLI
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
.travis.yml
Gopkg.lock
Gopkg.toml
LICENSE
README.md
input.go
input_test.go
validators.go
validators_test.go

README.md

Input

Build Status Coverage Status Go Report Card GoDoc

Simple, easy to use input handler for the CLI

Installation

go get github.com/atrox/input
# or with dep
dep ensure -add github.com/atrox/input

Usage

package main

import (
	"fmt"

	"github.com/atrox/input"
)

func main() {
	// Input (can be empty)
	someInput := input.Prompt("How was your day").(string)

	// Required Input
	someInput = input.Prompt("What is your favourite tv-show", input.RequiredValidator).(string)

	// Boolean Input (y/n)
	boolInput := input.Prompt("Are you sure (y/n)", input.RequiredValidator, input.BooleanValidator).(bool)

	// Boolean Input with default (Y/n)
	var inputResult bool
	switch boolInput2 := input.Prompt("Are you sure (Y/n)", input.BooleanValidator).(type) {
	case bool:
		// user set it with yes or no
		inputResult = boolInput2
	default:
		// user just pressed enter - set default
		inputResult = true
	}

	// File Input
	file := input.Prompt("Location of the config file?", input.RequiredValidator, input.FileValidator).(string)
	fmt.Println("File location:", file)
}

Validators

List of built-in validators:

  • RequiredValidator: ensures the input is not empty
  • PathValidator: ensures the input is valid looking path
  • DirectoryValidator: ensures the input is a valid and existing directory
  • FileValidator: ensures the input is a valid and existing file
  • IntegerValidator: ensures and converts the input to a integer with strconv.Atoi
  • BooleanValidator: ensures and converts the input to a boolean
    • true: 1, t, true, y, yes
    • false: 0, f, false, n, no

Create your own - you just need to create a input.ValidatorFunction:

func ContainsSomethingValidator(input string) (interface{}, error) {
    if !strings.Contains("something") {
        return nil, fmt.Errorf("Input %s does not contain 'something'", input)
    }

    return input, nil
}

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

You can’t perform that action at this time.