From 182164b6ae1e754adc095266936f6fdd683d9a3d Mon Sep 17 00:00:00 2001 From: The Magician Date: Thu, 7 Dec 2023 18:03:24 +0000 Subject: [PATCH] Only match command input at the start of the command string --- tests/test_get_commands.sh | 12 +++++++++++- zk | 14 +++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/tests/test_get_commands.sh b/tests/test_get_commands.sh index 57dc45c..de3b914 100644 --- a/tests/test_get_commands.sh +++ b/tests/test_get_commands.sh @@ -5,10 +5,20 @@ setup() { } test_get_command_returns_list_for_substring() { + commands="list new" expected="list" for arg in l li lis list; do - result="$(get_command "$arg")" + result="$(get_command "$commands" "$arg")" assert_equals "$expected" "$result" done } + +test_get_command_does_not_return_match_unless_at_start_of_command_string() { + commands="delete" + expected="" + + result="$(get_command "$commands" "l")" + + assert_equals "$expected" "$result" +} diff --git a/zk b/zk index 25409bc..d426d9d 100755 --- a/zk +++ b/zk @@ -1,20 +1,24 @@ #!/bin/sh -COMMANDS='list' +COMMANDS='list new delete' get_command() { - for c in $COMMANDS; do - if echo "$c" | grep "$1" >/dev/null; then + commands="$1" + input="$2" + + for c in $commands; do + if echo "$c" | grep "^$input" >/dev/null; then echo "$c" fi done } main() { - echo "Hello, world" + subcommand="$(get_commands "$COMMANDS" "$1")" + echo "$subcommand" } name="$(basename "$0")" if test "$name" = "zk"; then - main + main "$@" fi