-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
260 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
#!/bin/usr/env bash | ||
|
||
import log | ||
|
||
declare -Ag cli__options=() | ||
declare -Ag cli__descriptions=() | ||
cli__program_name="$0" | ||
cli__order=() | ||
|
||
cli::reset() { | ||
cli__order=() | ||
declare -Ag cli__options=() | ||
declare -Ag cli__descriptions=() | ||
} | ||
|
||
cli::set_program_name() { | ||
local program_name="$1" | ||
|
||
cli__program_name="${program_name}" | ||
} | ||
|
||
cli::_option_encode() { | ||
local option="$1" | ||
|
||
log::debug "Encoding ${option}" | ||
|
||
echo "${option}" | ||
} | ||
|
||
cli::_option_decode() { | ||
local option="$1" | ||
|
||
log::debug "Decoding ${option}" | ||
|
||
echo "${option}" | ||
} | ||
|
||
# if letter require argument, then subfix letter with ':' | ||
cli::add_option() { | ||
local letter="$1" | ||
local callback="$2" | ||
local description="$3" | ||
|
||
cli__order+=("$letter") | ||
cli__options["${letter:0:1}"]="$callback" | ||
cli__descriptions[$letter]="$description" | ||
} | ||
|
||
cli::print_help() { | ||
local usage="Usage: ${cli__program_name}" | ||
local description="" | ||
local description_formatted="" | ||
log::debug "${!cli__descriptions[@]}" | ||
log::debug "values: ${cli__descriptions[*]}" | ||
for letter in "${cli__order[@]}";do | ||
description_formatted="${cli__descriptions["$letter"]//\\n/\\n }" | ||
if [ "${letter:1:1}" = ":" ];then | ||
usage+=" [-${letter:0:1} ...]" | ||
description+=" -${letter:0:1} <...> ${description_formatted}\n" | ||
else | ||
usage+=" [-${letter:0:1}]" | ||
description+=" -${letter:0:1} ${description_formatted}\n" | ||
fi | ||
if [ "$(echo -e "$usage" | tail -n1 | wc -m )" -gt 70 ];then | ||
usage+="\n " | ||
fi | ||
done | ||
echo -e "${usage}\n\nOptions:\n${description}" | ||
} | ||
|
||
cli::handle() { | ||
local all_options="" | ||
for letter in "${cli__order[@]}";do | ||
all_options+="${letter}" | ||
done | ||
|
||
while getopts ":${all_options}" opt; do | ||
if [ "$opt" = ":" ];then | ||
log::fatal "Option -${OPTARG} require an argument" | ||
elif [ "$opt" = "?" ];then | ||
log::fatal "Invalid option -${OPTARG}" | ||
fi | ||
${cli__options[$opt]} "${OPTARG}" | ||
done | ||
} | ||
|
||
cli_test__a=0 | ||
cli_test__b=0 | ||
cli_test__c=0 | ||
|
||
cli_test::a_cb() { | ||
cli_test__a=1 | ||
} | ||
|
||
cli_test::b_cb() { | ||
cli_test__b=$1 | ||
} | ||
|
||
cli_test::c_cb() { | ||
cli_test__c=1 | ||
} | ||
|
||
cli_test::test_handle() { | ||
cli::reset | ||
cli::set_program_name "example" | ||
cli::add_option "a" cli_test::a_cb "Example option a" | ||
cli::add_option "b:" cli_test::b_cb "Example option b" | ||
cli::add_option "c" cli_test::c_cb "Example option c" | ||
|
||
cli::handle "-a" "-b" "sth" | ||
unit::assert_eq "${cli_test__a}" "1" | ||
unit::assert_eq "${cli_test__b}" "sth" | ||
unit::assert_eq "${cli_test__c}" "0" | ||
} | ||
|
||
cli_test::test_help_gen() { | ||
cli::reset | ||
cli::set_program_name "example" | ||
cli::add_option "a" cli_test::a_cb "Example option a" | ||
cli::add_option "b:" cli_test::b_cb "Example option b" | ||
cli::add_option "c" cli_test::c_cb "Example option c" | ||
|
||
local expected_output | ||
expected_output="Usage: example [-a] [-b ...] [-c] | ||
Options: | ||
-a Example option a | ||
-b <...> Example option b | ||
-c Example option c" | ||
local output | ||
output=$(cli::print_help) | ||
|
||
unit::assert_eq "${output}" "${expected_output}" | ||
} | ||
|
||
cli_test::all() { | ||
unit::test cli_test::test_help_gen | ||
unit::test cli_test::test_handle | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.