Amber icon indicating copy to clipboard operation
Amber copied to clipboard

✨ Reading values of environment variables and parameters?

Open JackPott opened this issue 1 year ago • 4 comments

I couldn't see an example in the docs of how to read in external environment variables. Is there a more idiomatic way than $printenv FOO$ ?

Nor could I see a way to parse arguments ($1, $2 etc), for example compiling to a bash script intended to be ran by a human. I got it working with $echo \$1$? but that isn't very pretty.

Do you have any simple examples, or am I missing something fundamental in my approach?

JackPott avatar May 22 '24 00:05 JackPott

You're right @JackPott. This is something on the to do list. The end game syntax would look like this:

main(args) {
    // Args are of type [Text] here
}

This is something that we'll try to deliver asap

Also If you want to just refer to external variables - I haven't though about it yet. Perhaps this syntax would be nice:

env FOO               // Exports FOO as Text
env FOO as foo        // Exports foo as Text
env FOO as foo: Num   // Exports foo as Number

Ph0enixKM avatar May 22 '24 10:05 Ph0enixKM

First of all, congrats on your project, @Ph0enixKM! Great work so far! Love to see tools and languages like this!

And for the topic at hand:

Also If you want to just refer to external variables - I haven't though about it yet. Perhaps this syntax would be nice:

env FOO               // Exports FOO as Text
env FOO as foo        // Exports foo as Text
env FOO as foo: Num   // Exports foo as Number

I feel the syntax should be able to handle both reading/importing and writing/exporting environment variables. To expand on your suggestion, what about something like this:

// Reading / importing environment variables
env FOO                 // Imports env var FOO as Text, into variable `FOO`
env FOO as foo          // Imports env var FOO as Text, into variable `foo`
env FOO as foo: Num     // Imports env var FOO Number, into variable `foo`

// Writing / exporting variables to global environment
env FOO = "some value"  // Exports env var FOO, with value `some value`
env FOO = foo           // Exports env var FOO, with value from variable `foo`

If the env keyword should be able to cast to other types, as with env FOO as foo: Num, how would error handling be done? Could this be combined with failed in some way?

env FOO as foo: Num failed {
    echo "Could not read variable `FOO` from environment. Setting default."
    return 42  // Could also be implicit return
}

default instead of failed might event be cleaner.

env FOO as foo: Num default 42

This is just my two cents on the subject.

sirhaug avatar May 22 '24 18:05 sirhaug

I'd actually create a new syntax default that could be use to fallback easily to some value when given command or failable function fail. This way it'd be the standard way to handle default values across the language

let os = $uname$ default "unknown"

We'd have to discuss it on our discord server before implementing the default behaviour

Ph0enixKM avatar May 24 '24 11:05 Ph0enixKM

Hello, I think that arguments-parsing would be liked to realize with the notation of docopt.

kkdd avatar May 25 '24 18:05 kkdd

I am closing as we have the env stuff functions now.

Mte90 avatar Jul 19 '24 09:07 Mte90