diff options
author | Ayush Agarwal <ayush@fastmail.in> | 2022-02-16 22:35:11 +0530 |
---|---|---|
committer | Ayush Agarwal <ayush@fastmail.in> | 2022-02-16 22:37:26 +0530 |
commit | 4f03711a317c2409fa420909cc6005aa1af8be1a (patch) | |
tree | e8f014190d58eb5dc381efd502b3770df5da2347 | |
parent | d4315c63b4defca0bbcee0668262cb596256badd (diff) |
refactor!: initialize sane defaults, parse args
BREAKING CHANGE: the `-b` option has been changed to `-d` to make more
sense. i know changes like these may annoy users but i didn't want to
use a generic term like `--backend` for configuring dmenu programs.
`--backend` is also confusing because tessen supports two kinds of
backends now - pass backend and dmenu backend
the `tmp_opts` variable was declare before in the parse_config option as
a way to resolve a conflict when a user provides config files for rofi
and wofi and ends up choosing rofi or wofi explicitly from the
arguments. in that case, validate_backend would reset the list of
dmenu_backend_opts variable to its defaults without the config file
locations which are mentioned in the parse_config function
since we've parsed the `-c` option before in parse_config, the `-c`
option must be ignored in the main() function. we can't omit `-c`
because then a user would get incorrect arg specified error.
-rwxr-xr-x | tessen | 78 |
1 files changed, 69 insertions, 9 deletions
@@ -723,32 +723,92 @@ parse_config() { } main() { - local _opt - - if ! [[ -d "$tsn_prefix" ]]; then - _die "password store directory not found" + # initialize basic options for users who expect sane defaults and don't use + # either the config file or args + if [[ -z "$pass_backend" ]]; then + find_pass_backend fi + if [[ -z "$dmenu_backend" ]]; then + find_dmenu_backend + fi + validate_dmenu_backend "$dmenu_backend" + validate_action default + + # parse the config file + parse_config "$@" + # parse command line arguments after the config file because args hold more + # precedence and priority than the config file + local _opt while [[ "$#" -gt 0 ]]; do _opt="$1" case "$_opt" in - -b | --backend) - tsn_backend="${2}" + -p | --pass) + if [[ "$#" -lt 2 ]]; then + _die "please specify a valid password store backend: pass | gopass" + fi + validate_pass_backend "$2" + readonly pass_backend + shift + ;; + --pass=*) + if [[ -z "${_opt##--pass=}" ]]; then + _die "please specify a valid password store backend: pass | gopass" + fi + validate_pass_backend "${_opt##--pass=}" + readonly pass_backend + ;; + -d | --dmenu) + if [[ "$#" -lt 2 ]]; then + _die "please specify a valid dmenu backend: rofi | fuzzel | bemenu | wofi" + fi + validate_dmenu_backend "$2" + # if rofi or wofi are selected, the additional values of + # dmenu_backend_opts which were mentioned in the config file should be + # appended + # we need tmp_opts because even if dmenu_backend_opts is set to the + # correct value, validating it would reset it back to the default + # values + readonly dmenu_backend + if [[ "$dmenu_backend" == "rofi" ]] || [[ "$dmenu_backend" == "wofi" ]]; then + if [[ "${#tmp_opts[@]}" -gt 0 ]]; then + dmenu_backend_opts+=("${tmp_opts[@]}") + readonly dmenu_backend_opts + fi + fi shift ;; - --backend=*) - tsn_backend="${_opt##--backend=}" + --dmenu=*) + if [[ -z "${_opt##--dmenu=}" ]]; then + _die "please specify a valid dmenu backend: rofi | fuzzel | bemenu | wofi" + fi + validate_dmenu_backend "${_opt##--dmenu=}" + readonly dmenu_backend + if [[ "$dmenu_backend" == "rofi" ]] || [[ "$dmenu_backend" == "wofi" ]]; then + if [[ "${#tmp_opts[@]}" -gt 0 ]]; then + dmenu_backend_opts+=("${tmp_opts[@]}") + readonly dmenu_backend_opts + fi + fi ;; -a | --action) if [[ "$#" -lt 2 ]]; then _die "please specify a valid action: autotype | copy | both" fi validate_action "$2" + readonly tsn_action shift ;; --action=*) + if [[ -z ${_opt##--action=} ]]; then + _die "please specify a valid action: autotype | copy | both" + fi validate_action "${_opt##--action=}" + readonly tsn_action ;; + # we've already parsed `-c` before so we'll need to skip it here + -c | --config) shift ;; + --config=*) : ;; -h | --help) print_help exit 0 @@ -765,7 +825,7 @@ main() { esac shift done - unset -v _opt + unset -v _opt tmp_opts if [[ -z "${tsn_backend}" ]]; then tsn_backend="$(find_backend)" |