abs-lang / abs

Home of the ABS programming language: the joy of shell scripting.
https://www.abs-lang.org
MIT License
510 stars 35 forks source link

A bug in the flag() builtin function #462

Open s5unty opened 2 years ago

s5unty commented 2 years ago
#!/usr/bin/env abs

cli = require('@cli')

@cli.cmd("foo", "opts", {"s": 1 })
f foo(argv, flags) {
    echo("flags: %s", flags.str())
}

@cli.cmd("bar", "opts", {"ss":1 })
f bar(argv, flags) {
    echo("flags: %s", flags.str())
}

cli.run()
 % ./bug.abs foo -s
 flags: {"s": "./bug.abs"}
 % ./bug.abs bar -ss
 flags: {"ss": true}

When the flagFN() function iterates through os.Args, the first v value is "abs", which causes the flag "s" to be misinterpreted.

diff --git evaluator/functions.go evaluator/functions.go
index 5d3d7c0..52999a7 100644
--- evaluator/functions.go
+++ evaluator/functions.go
@@ -615,7 +615,7 @@ func flagFn(tok token.Token, env *object.Environment, args ...object.Object) obj
        // passed to the script
        // This is O(n) but again, performance
        // is not a big deal in ABS
-       for _, v := range os.Args {
+       for _, v := range os.Args[1:] {
                // If the flag was found in the previous
                // argument...
                if found {