matejak / argbash

Bash argument parsing code generator
Other
1.4k stars 62 forks source link

[Feature] Argbash Interpreter #49

Open CodingNinja opened 6 years ago

CodingNinja commented 6 years ago

Hey all,, thanks so much for your work on Argbash Matejak, all out tooling is powered by argbash anhd we absolutely love it!

We wrote this small wrapper for our argbash scripts to enable devs to easily work on bash files without the manual compilation step between, hopefully it will be of use to others :)

https://gist.github.com/CodingNinja/c736e6cff18a4b37322558b6bb9dd93e

matejak commented 6 years ago

Hello, I am happy that you find Argbash useful. Your idea is really interesting, do I understand correctly that it is a transparent and efficient translator from Argbash templates to Bash scripts? What is the exact purpose of it? How do you use it? What kind of templates do you write?

I have tried to use it and the gist contains a typo (on line 9), so I assume that your production version is different. I can also say that you could have used Argbash to provide some decent interface to the interpreter - you can, for example, let users override the CACHE_PATH variable.

CodingNinja commented 6 years ago

Hey @matejak ,

That's correct, rather than using the traditional "/usr/bin/env bash" shebang, we use /usr/bin/env argbash-interpreter which allows us to just modify Argbash templates and have them silently translated to normal bash scripts.

The exact purpose is to ensure that as devs are runnnincg commands, they don't need to first ensure all argbash templates are compiled (We are nearing 50 argbash scripts now) with the latest version.

Usage is simple, replace the shebang in any bash script with #!/usr/bin/env argbash-interpreter

Templates are the same as normal Argbash templates (exmple), all we do is transperantly run argbash if the hash of a template changes before running it.

If you see benefit in having this in the core argbash, I'm happy to convert it to an argbash template and do up a proper PR.

matejak commented 6 years ago

That's really great to hear! You can go ahead and submit a PR, I don't see why not to have argbash-interpreter part of the suite. I just see some room for coding style improvement, but aside from that, it is a useful and lightweight tool.

jonasandero commented 5 years ago

I have a similar workflow, but I never cache the generated code. I know it is not portable, but it works fine for me. Here is an example:

given my helper script ~/bin/spd_argbash_parse:

#!/bin/bash

echo 'tmp_file=$(mktemp); argbash --strip all ${BASH_SOURCE[0]} > $tmp_file; [ $? -eq 0 ] || exit $?; source $tmp_file'

I call it from another script (example)

#!/bin/bash

# ARG_POSITIONAL_SINGLE([env], [The environement to target])
# ARG_HELP([Bootstrap backend])
# ARG_TYPE_GROUP_SET([content], [Environment], [env], [dev,prod])
# ARGBASH_GO
eval $(spd_argbash_parse)

printf "'%s' \\n"  "$_arg_env"

This means that I run argbash every time I call my script, but the overhead on my computer is neglectable. Moreover, the scripts are cleaner and no extra files.

jonasandero commented 5 years ago

I really would like to replace

eval $(spd_argbash_parse)

with

argbash --strip all ${BASH_SOURCE[0]} | eval

but that just gives

> ~ ./bin/minimal.sh
/Users/jonas/.local/bin/argbash: line 471: printf: write error: Broken pipe

BTW I am on MacOs if that rings any bells.