Skip to content

Commit

Permalink
Add command to list commands with prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
juanibiapina committed Apr 30, 2015
1 parent 84a8051 commit b615f68
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
32 changes: 32 additions & 0 deletions libexec/lasher-_commands
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash
#
# Summary: List all available commands with a prefix
# Usage: lasher _commands <prefix>
#
# Looks for executable files prefixed with prefix- and
# displays each suffix as an available command.
#
# This is supposed to be used from other scripts.

set -e

if [ "$#" -ne 1 ]; then
lasher-help _commands
exit 1
fi

prefix="$1"

IFS=: paths=($PATH)

shopt -s nullglob

{ for path in "${paths[@]}"; do
for command in "${path}/$prefix-"*; do
command="${command##*$prefix-}"
if [[ ! "$command" == _* ]]; then
echo ${command}
fi
done
done
} | sort | uniq
22 changes: 22 additions & 0 deletions tests/lasher-_commands.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bats

load test_helper

@test "without arguments prints usage" {
run lasher-_commands
assert_failure
assert_line "Usage: lasher _commands <prefix>"
}

@test "lists commands" {
run lasher-_commands lasher
assert_success
assert_line help
assert_line new-command
}

@test "does not list hidden commands" {
run lasher-_commands lasher
assert_success
refute_line _commands
}

0 comments on commit b615f68

Please sign in to comment.