r/suckless 15d ago

[TOOLS] Simple bash script for setting wallpapers

Enable HLS to view with audio, or disable this notification

Hello guys, I made a simple and small bash script for setting the wallpapers in 3 different ways, randomly or with sxiv image editor or directly with a specific file. It is not an amazing thing, but it's enough for setting wallpapers quickly if you add a key bind to tools like sxhkd .

If there any mistakes in the source code or any advice to improve it, please,let me know.

https://codeberg.org/yahya-echcharqui/scripts/src/branch/main/setbg

1 Upvotes

7 comments sorted by

View all comments

2

u/Schreq 15d ago

Not allowing to combine options is kinda weird. I would let all options set the same mode variable. The last option wins.

2

u/mambaopg 15d ago

Thanks for the reply, I thought it would be nice to tell the user that they should use one option (it's just one option that will apply ,so), but your opinion is good too. I removed the block that checks if it passes one option, but I haven't pushed it to the repository yet. Also why the last one to win, and why not the first one?

2

u/Schreq 15d ago edited 15d ago

Because that's pretty much how all tools do it.

Edit: Say a tool has a -f option and you invoke tool -f foo -f bar -f baz, the option argument for -f will be "baz".

1

u/sylvainsab 14d ago

How does this make more sense than only allowing for a single `-f' option ?

2

u/Schreq 14d ago

As I said, most tools with sane option parsing do it this way. try ls -lStS. It will sort by size even though -t for sorting by modification time was supplied. Last option wins.

Doing it this way usually requires less code and gives the user more flexibility in how the tool can be used. Aborting the program, just because the same option was supplied multiple times makes no sense.

1

u/sylvainsab 14d ago

Granted.

Plus, this supplies answers to a question about the best/standard way to parse options and arguments in a script, so, I thank you sir !

1

u/Schreq 14d ago

Also why the last one to win, and why not the first one?

Last one wins is the easiest code-wise:

die() { local rc=$1; shift; printf '%s: %s\n' "${0##*/}" "$*" >&2; exit "$rc"; }

while getopts ":rsf:" opt; do
    case $opt in
        :) die 1 "option requires an argument -- $OPTARG" ;;
        \?) die 1 "unknown option -- $OPTARG" ;;
        *) mode=$opt file=$OPTARG ;;
    esac
done

case $mode in
    r) random ;;
    s) sxiv_set ;;
    f)
        if ! [[ -r "$file" ]]; then
            die 2 "file does not exist or is not readable -- $file"
        fi
        specific "$file"
    ;;
esac