top | item 30540201

Argc – A handy way to handle sh/bash CLI parameters

36 points| TheLocehiliosan | 4 years ago |github.com | reply

6 comments

order
[+] gkfasdfasdf|4 years ago|reply
Very cool. A natural next step would be to auto-generate a completion script.
[+] moondev|4 years ago|reply
pretty cool. instead of putting the eval() inside the script a shebang also works.

#!/usr/bin/env -S bash -c 'argc -e ${0} "${@}"'

Would be cleaner if there was mode handling this.

#!/usr/bin/env -S argc -e

[+] ktpsns|4 years ago|reply
Okay so this is clever – it's a polyglot, both a valid bash script and a DSL which is interpreted by some binary written in Rust and called at the final "eval $(argc...)" line, where "argc" is the name of the rust binary.

Thinking loudly: I was just wondering about debugging (kind of "does this lack source maps?"), but shell never prints out lines at syntax errors, so it does not really matter. One can write "set -e" anyway at the start of the functions in order to catch errors early.

[+] voakbasda|4 years ago|reply
With bash, you can write a function that gets called when a script exits with an error that can print a full stack trace. It just isn’t a default behavior.
[+] nas|4 years ago|reply
It's an interesting idea but a lot of care is needed to avoid parsing bugs. I.e. the code you are feeding to the shell via "eval" doesn't not get parsed in some way you don't expect. Also, parsing the script source code, need to be careful and clear about the parse rules. In that case, it's not as dangerous since the script source code is presumably not provided by an attacker (otherwise they could just directly run the commands they want).