# README
Input
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:
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features
# Functions
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.
DirectoryValidator ensures the input is a valid and **existing** directory Returns modified extended path.
FileValidator ensures the input is a valid and **existing** file Returns modified extended path.
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.
PathValidator ensures the input is valid looking path Returns modified extended path.
Prompt prints the question to Stdout and checks the user input according to the provided validators.
RequiredValidator ensures the input is not empty.
# Type aliases
ValidatorFunction describes the simple interface every validator needs to meet.