Ph0enixKM / Amber

💎 Amber the programming language compiled to bash
https://amber-lang.com
GNU General Public License v3.0
3.51k stars 67 forks source link

Using args with amber #207

Closed coderaiser closed 1 week ago

coderaiser commented 1 week ago

Could you please tell me how to write this in Amber:

coderaiser@localcmd:~/loop$ bash 1.sh x
Username: x
coderaiser@localcmd:~/loop$ cat 1.sh
echo "Username: $1";

I need to take command line arguments for script I'm working on. Can't find this in documentation. Is it possible?

Ph0enixKM commented 1 week ago

Hi @coderaiser. This is possible. A week ago there was a new feature introduced in main block that will give you all arguments passed to Amber. This is not documented yet although it has to be asap.

Here is the code example that demonstrates how to do it:

main (args) {
    let arg = args[1]
    echo "Username: {arg}"
}
coderaiser commented 1 week ago

Thank you, I fugured out another work around:

main {
    let file = "$1"
    let size = "$2"
}

This is also good, but it counts from zero, as I see:

main(args) {
    let file = args[0];
    let size = args[1];
}

Just rewritten one of my bash utilities to Amber, could you please point me up if I could use more ergonomic constructions?

https://github.com/coderaiser/loop/blob/master/src/loop.ab

Also I think would be great to support export instead of pub, since we have import. For other parts of language - it looks really good, the things that is missing is: linter (with autofixes) and formatter.

Thanks for a great work!

Ph0enixKM commented 1 week ago

That's a good point! import and export is more idiomatic. We can change that since we're still in an unstable alpha. Sorry for that mistake - it should be args[1] indeed. Linting and formatting is on the list. Probably when we integrate LSP.

Ph0enixKM commented 1 week ago

@coderaiser your project is looking good. I hope once Amber evolves it will be even easier to write shell scripts. The RDC (realtime dependency checking) is on the way so you'll probably don't need to check for each command to exist. You could remove that code in the future as Amber will do it by itself