github.com/wasilibs/wazerox@v0.0.0-20240124024944-4923be63ab5f/examples/cli/testdata/cli.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"os"
     6  	"strconv"
     7  )
     8  
     9  func main() {
    10  	var sub bool
    11  	flag.BoolVar(&sub, "sub", false, "whether to subtract arguments instead of add")
    12  
    13  	flag.Parse()
    14  
    15  	if flag.NArg() < 2 {
    16  		os.Stdout.WriteString("bad arguments\n")
    17  		os.Exit(1)
    18  	}
    19  
    20  	a, err := strconv.Atoi(flag.Arg(0))
    21  	if err != nil {
    22  		os.Stdout.WriteString("bad arguments\n")
    23  		os.Exit(1)
    24  	}
    25  
    26  	b, err := strconv.Atoi(flag.Arg(1))
    27  	if err != nil {
    28  		os.Stdout.WriteString("bad arguments\n")
    29  		os.Exit(1)
    30  	}
    31  
    32  	var res int
    33  	if sub {
    34  		res = a - b
    35  	} else {
    36  		res = a + b
    37  	}
    38  
    39  	os.Stdout.WriteString("result: " + strconv.Itoa(res) + "\n")
    40  }