github.com/kbehouse/nsc@v0.0.6/cmd/numberparams.go (about) 1 /* 2 * Copyright 2018 The NATS Authors 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package cmd 17 18 import ( 19 "fmt" 20 21 "github.com/dustin/go-humanize" 22 23 cli "github.com/nats-io/cliprompts/v2" 24 ) 25 26 type NumberParams int64 27 28 func (e *NumberParams) Valid() error { 29 // flag already insures this is a number 30 return nil 31 } 32 33 func (e *NumberParams) Edit(prompt string) error { 34 var err error 35 var nv int64 36 sv := fmt.Sprintf("%d", e) 37 _, err = cli.Prompt(prompt, sv, cli.Val(func(s string) error { 38 nv, err = ParseNumber(s) 39 return err 40 })) 41 if err != nil { 42 return err 43 } 44 *e = NumberParams(nv) 45 return nil 46 } 47 48 func (e *NumberParams) Set(s string) error { 49 nv, err := ParseNumber(s) 50 if err != nil { 51 return err 52 } 53 *e = NumberParams(nv) 54 return nil 55 } 56 57 func (e *NumberParams) Type() string { 58 return "number" 59 } 60 61 func (e *NumberParams) String() string { 62 return humanize.Comma(int64(*e)) 63 } 64 65 func (e *NumberParams) Int64() int64 { 66 return int64(*e) 67 }