Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Taming Command Line Chaos: Macros to the Rescue (candid.dev)
1 point by candiddevmike on June 7, 2024 | hide | past | favorite | 3 comments


Scripts? What happened to scripts?


Author here, the blog post addresses this:

> To wrangle these beasts, we’ve all resorted to wrapping commands in shell scripts or aliases—the infamous “bash spaghetti.” This approach may solve the repeatability problem, but it introduces new ones:

> Argument Wrestling: Ever tried to figure out if a bash variable is a string or an array? Or when to quote something with spaces? It’s a battle.

> Discoverability Disaster: Your beautifully crafted CLI tool is now hidden behind an unsightly bash script. Not the most welcoming first impression.


>Ever tried to figure out if a bash variable is a string or an array?

    if [[ "$(declare -p some_array)" =~ ^declare -([^\ ]*a[^\ ]*)\ .* ]]; then
    
        echo "Array".

    fi
This is the quick and dirty version and it doesn't recognize an array that was assigned to a nameref, but that's a different discussion.

>Or when to quote something with spaces? It’s a battle.

You __ALWAYS__ quote and if you want to be on the safe side you always use "{}" around the variables as well.

You also use "set -o nounset" to do yourself the favor of not working with variables that don't exist because of e.g. typos.

>Discoverability Disaster: Your beautifully crafted CLI tool is now hidden behind an unsightly bash script. Not the most welcoming first impression.

How's that different from the macro? The attempt is the same, to abstract the complex command line options of a tool behind something "easier".




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: