input

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2017 License: MIT Imports: 8 Imported by: 2

README

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:

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BooleanValidator

func BooleanValidator(input string) (interface{}, error)

BooleanValidator converts the input to a boolean If input is empty we return nil instead of an integer so you can set a default on your side

func DirectoryValidator

func DirectoryValidator(input string) (interface{}, error)

DirectoryValidator ensures the input is a valid and **existing** directory Returns modified extended path

func FileValidator

func FileValidator(input string) (interface{}, error)

FileValidator ensures the input is a valid and **existing** file Returns modified extended path

func IntegerValidator

func IntegerValidator(input string) (interface{}, error)

IntegerValidator converts the input to a integer with strconv.Atoi If input is empty we return nil instead of an integer so you can set a default on your side

func PathValidator

func PathValidator(input string) (interface{}, error)

PathValidator ensures the input is valid looking path Returns modified extended path

func Prompt

func Prompt(question string, validators ...ValidatorFunction) (out interface{})

Prompt prints the question to Stdout and checks the user input according to the provided validators

func RequiredValidator

func RequiredValidator(input string) (interface{}, error)

RequiredValidator ensures the input is not empty

Types

type ValidatorFunction

type ValidatorFunction func(string) (interface{}, error)

ValidatorFunction describes the simple interface every validator needs to meet

Jump to

Keyboard shortcuts

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