diff options
| author | Paul Buetow <paul@buetow.org> | 2025-11-25 22:32:54 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-11-25 22:32:54 +0200 |
| commit | b97e4b19ec9c415cd5f3d204e23e5fde5180db26 (patch) | |
| tree | af324415c9e0e71724d4a920a62cc4295877dbc0 /cmd | |
Initial commit: perc v0.0.0 - percentage calculatorv0.0.0
Amp-Thread-ID: https://ampcode.com/threads/T-e4f4a959-8cc6-4ac0-b6fb-2779867e8b2a
Co-authored-by: Amp <amp@ampcode.com>
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/perc/main.go | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/cmd/perc/main.go b/cmd/perc/main.go new file mode 100644 index 0000000..a5764b5 --- /dev/null +++ b/cmd/perc/main.go @@ -0,0 +1,48 @@ +package main + +import ( + "fmt" + "os" + "strings" + + "codeberg.org/snonux/perc/internal" + "codeberg.org/snonux/perc/internal/calculator" +) + +func main() { + output, err := runCommand(os.Args) + if err != nil { + fmt.Println("Error:", err) + os.Exit(1) + } + fmt.Println(output) +} + +func runCommand(args []string) (string, error) { + if len(args) < 2 { + printUsage() + return "", fmt.Errorf("no input provided") + } + + if args[1] == "version" { + return internal.Version, nil + } + + input := strings.Join(args[1:], " ") + result, err := calculator.Parse(input) + if err != nil { + return "", err + } + + return result, nil +} + +func printUsage() { + fmt.Println("Usage: perc <calculation>") + fmt.Println(" perc version") + fmt.Println("\nExamples:") + fmt.Println(" perc 20% of 150") + fmt.Println(" perc what is 20% of 150") + fmt.Println(" perc 30 is what % of 150") + fmt.Println(" perc 30 is 20% of what") +} |
