Thorsh: Thor, but for bash
source link: https://willschenk.com/howto/2023/thorsh_thor_for_bash/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Thor, but for bash
Like Thor, but for bash!
Check it out on github
Example
#!/bin/bash
DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
. "$DIR/thorsh"
add_function hello "Says hello"
function hello() {
echo Hello, World
}
add_value type=x11 "Server type"
add_value image=debian-12 "Image name"
add_function create name "Create a new server"
function create() {
echo Called create
echo "name = " ${name}
echo "type = " ${type}
echo "image = " ${image}
}
thorsh "$@"
Which gives you
$ ./example hello
Hello, World
$ ./example create server_name --image ubuntu
Called create
name = server_name
type = x11
image = ubuntu
$ ./example help
Usage: example [OPTIONS] COMMAND [args]
Commands
help Show the help command
hello Says hello
create name Create a new server
Options
--type=x11 Server type
--image=debian-12 Image name
Quick Start
curl -L https://github.com/wschenk/thorsh/raw/main/thorsh > thorsh
chmod +x thorsh
thorsh template my_script
vi my_script
thorsh
needs to be in the same directory as your script
Usage
bash thorsh help
Usage: thorsh [OPTIONS] COMMAND [args]
Commands
help Show the help command
template name Create a new script
inline name Make a throsh script self-contained
template
To generate a new template file
inline
Merges the thorsh
script with the user script, which then is standalone
bash thorsh inline example > example.inlined
I’ve been doing a bunch of server scripting lately, and I want to reach for Thor to help sort out some command options. But its a bit of a pain to get a ruby environment setup, especially in a Docker container where it’s not the primary purpose.
But I missed the simple ways of defining a method with help and then implementing it.
How does it work?
Calling add_flag
, add_option
and add_function
puts stuff into a
bunch of bash 3 compatible arrays. Then the thorsh
at the end of
your script does this:
function thorsh() {
eval $(generate_options_parser)
}
Which iterates over those arrays creating bash code to generate a
while/case loop over all of the input arguments. It sets what needs
to be set in the environment and then executes the function in cmd
.
Usage
thorsh template new_script
add_flag key Usage
For adding single, true/false things. (Either the string “true” or “”)
add_flag verbose "Verbose flag"
verbose
default to “”.
add_flag logging=true "Verbose flag"
add_value key Usage
Adding a 2 parameter value, something like --log_level debug
add_value log_level=warn "Logging level (default: warn)
example
log_level
will be warn
example --log_level debug
log_level
will be debug
add_function name arg1 arg2 argN “Usage”
Define a function with a varible number of (fixed) arguments.
The basic is
add_function hello "Prints hello world"
function hello() {
echo Yo
}
And the example above
add_function name arg1 arg2 argN "Usage"
function name() {
echo arg1 = #{arg1}
echo arg2 = #{arg2}
echo argN = #{argN}
}
Inspired by
Recommend
-
6
This article was published a while ago and may contain obsolete information!Some scripts work well because they are self contained and don’t have a lot of dependancies, like the
-
13
币牛BiBull平台即将上线THOR
-
8
Of Lasers And Lightning: Thwarting Thor With Technology Most of us don’t spend that much time thinking about lightning. Every now and then we hear some miraculous news story about the man who...
-
3
What If...?’s ending was spoiled weeks ago by Thor in a Hyundai commercial What if Marvel revealed the big finale in a months-old car commercial?...
-
2
Dear Reader, I have a project mailPipe that I use for a couple of different projects. At its heart mailPipe is called by my MTA when it receives an email for a specific address. It loo...
-
8
MATLAB Student Ambassador Achievement – Simon Thor » Student Lounge Today we are congratulating MATLAB Student Ambassador- Simon Thor from KTH Royal Institute of Technology. As a...
-
8
New 'Thor: Love and Thunder' trailer reveals a new goddess of thunderLauren ForristalTue, April 19, 2022, 1:04 AM·3 min readSlated to hit theaters on July 8, 2022, “
-
9
In gods we trust — Gorr the God Butcher is coming for Thor in new Love and Thunder trailer “Let me tell you the story of the space viking, Thor Odinson….”...
-
6
Zeus flicks too hard in latest Thor trailer Gorr the God Butcher has entered the chat By
-
6
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK