summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-03-23 22:28:55 +0200
committerPaul Buetow <paul@buetow.org>2026-03-23 22:28:55 +0200
commitc1de7b44437997e4695b5554a96ad253cf44584a (patch)
treeccdba61d94f77cac2932bf464e60a9a060782de8 /cmd
parentfb3ace9c0548906eff67504368a7a40ef2f980a8 (diff)
Fix error handling in cmd/perc/main.go
Diffstat (limited to 'cmd')
-rw-r--r--cmd/perc/main.go24
1 files changed, 21 insertions, 3 deletions
diff --git a/cmd/perc/main.go b/cmd/perc/main.go
index 848428e..5dd1819 100644
--- a/cmd/perc/main.go
+++ b/cmd/perc/main.go
@@ -25,7 +25,9 @@ func runCommand(args []string) (string, error) {
if len(args) < 2 {
// No args provided - check if stdin is a TTY for REPL mode
if isatty.IsTerminal(os.Stdin.Fd()) {
- repl.RunREPL()
+ if err := runREPL(); err != nil {
+ return "", err
+ }
return "", nil
}
printUsage()
@@ -38,7 +40,14 @@ func runCommand(args []string) (string, error) {
// Check for --repl flag
if args[1] == "--repl" || args[1] == "repl" {
- repl.RunREPL()
+ // REPL command explicitly requested - run it (may fail if not a TTY)
+ if err := runREPL(); err != nil {
+ // If not a TTY, just return empty string (REPL can't run in non-interactive mode)
+ if !isatty.IsTerminal(os.Stdin.Fd()) {
+ return "", nil
+ }
+ return "", err
+ }
return "", nil
}
@@ -58,7 +67,8 @@ func runCommand(args []string) (string, error) {
input := strings.Join(args[1:], " ")
// Try RPN parsing first (for bare RPN expressions like "3 4 +")
- if rpnResult, rpnErr := runRPN(input); rpnErr == nil {
+ rpnResult, rpnErr := runRPN(input)
+ if rpnErr == nil {
return rpnResult, nil
}
@@ -71,6 +81,14 @@ func runCommand(args []string) (string, error) {
return result, nil
}
+// runREPL runs the REPL and handles errors
+func runREPL() error {
+ if err := repl.RunREPL(); err != nil {
+ return fmt.Errorf("REPL error: %w", err)
+ }
+ return nil
+}
+
// runRPN parses and evaluates an RPN expression
func runRPN(input string) (string, error) {
vars := rpn.NewVariables()