Simplified parse.

This commit is contained in:
Lars Mikal Rogne 2024-04-24 19:53:00 +02:00
parent 9ed8f02013
commit f07769fe9f
Signed by: Logiar
SSH Key Fingerprint: SHA256:tq77C31em1ZG4oELIHC3k62wq5UzPSXmhqH8g62whIY
2 changed files with 5 additions and 6 deletions

View File

@ -25,11 +25,10 @@ func NewCalculator(parser *io.Cliargs) Calculator {
} }
func (o Calculator) Collatz() { func (o Calculator) Collatz() {
err := o.parser.Parse() number, err := o.parser.Parse()
if err != nil { if err != nil {
log.Panicf("Couldn't parse input: %s\n", err) log.Panicf("Couldn't parse input: %s\n", err)
} }
number := o.parser.GetNumber()
fmt.Print(number.Text(10)) fmt.Print(number.Text(10))
o.nextNumber(number) o.nextNumber(number)
fmt.Println() fmt.Println()

View File

@ -10,17 +10,17 @@ type Cliargs struct {
value *big.Int value *big.Int
} }
func (arg *Cliargs) Parse() error { func (arg *Cliargs) Parse() (*big.Int, error) {
args := os.Args[1:] args := os.Args[1:]
if len(args) != 1 { if len(args) != 1 {
return errors.New("only expected 1 arg") return nil, errors.New("only expected 1 arg")
} }
num, success := new(big.Int).SetString(args[0], 10) num, success := new(big.Int).SetString(args[0], 10)
if !success { if !success {
return errors.New("argument is not an integer") return nil, errors.New("argument is not an integer")
} }
arg.value = num arg.value = num
return nil return num, nil
} }
func (arg *Cliargs) GetNumber() *big.Int { func (arg *Cliargs) GetNumber() *big.Int {