Files
super-man/bash-helper.sh
T
Trilltechnician effbd8ebe5 feat: Add CLI parameter support for direct command lookup
- Add --help flag with comprehensive usage documentation
- Add --list flag to show all available commands
- Add direct mode: bash-helper COMMAND [FILTER]
- Keep interactive fzf mode as default (when no args)
- Improve error handling and user feedback
- Add better formatting with colored output
- Support both interactive and non-interactive usage

Examples:
  bash-helper              # Interactive mode (original behavior)
  bash-helper ls           # Direct: show all ls flags
  bash-helper ls size      # Direct: show ls flags with 'size'
  bash-helper --help       # Show usage
  bash-helper --list       # List all commands

This makes the tool more versatile for both interactive exploration
and quick CLI lookups without requiring fzf for direct usage.
2025-10-27 20:48:33 -07:00

220 lines
6.3 KiB
Bash
Executable File

#!/bin/bash
show_help() {
cat << EOF
Usage: bash-helper [OPTIONS] [COMMAND] [FILTER]
Interactive bash command helper with fzf or direct CLI usage.
OPTIONS:
-h, --help Show this help message
-i, --interactive Force interactive mode (default if no args)
-l, --list List all available commands
MODES:
1. Interactive (default):
bash-helper
2. Direct lookup:
bash-helper COMMAND [FILTER]
Examples:
bash-helper ls # Show all flags for ls
bash-helper ls size # Show ls flags containing "size"
bash-helper grep -i # Show grep flags containing "-i"
COMMANDS:
Available commands: ls, cd, pwd, cp, mv, rm, mkdir, rmdir, grep, find, echo
DEPENDENCIES:
fzf - Required for interactive mode only
EXAMPLES:
bash-helper # Interactive mode with fzf
bash-helper ls # Show all ls flags
bash-helper grep recursive # Show grep flags about recursion
bash-helper --list # List all available commands
EOF
}
show_flags() {
CMD_NAME=$1
FILTER=$2
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${YELLOW}Flags for: ${GREEN}$CMD_NAME${NC}"
if [ -n "$FILTER" ]; then
echo -e "${YELLOW}Filter: ${GREEN}$FILTER${NC}"
fi
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""
if type "$CMD_NAME" | grep -q "shell builtin"; then
if [ -n "$FILTER" ]; then
help "$CMD_NAME" | grep -E ' -' | grep -i "$FILTER"
else
help "$CMD_NAME" | grep -E ' -'
fi
elif command -v "$CMD_NAME" &> /dev/null; then
if [ -n "$FILTER" ]; then
man "$CMD_NAME" 2>/dev/null | col -b | grep -E '^\s*-' | grep -i "$FILTER" || \
man "$CMD_NAME" 2>/dev/null | col -b | grep -E ' -' | grep -i "$FILTER"
else
man "$CMD_NAME" 2>/dev/null | col -b | grep -E '^\s*-' || \
man "$CMD_NAME" 2>/dev/null | col -b | grep -E ' -'
fi
else
echo -e "${RED}Could not find help for: $CMD_NAME${NC}"
echo ""
echo "Make sure the command is installed or check the spelling."
return 1
fi
echo ""
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
}
list_commands() {
echo "Available commands:"
echo ""
for cmd in "${COMMANDS[@]}"; do
CMD_NAME=${cmd%%:*}
DESC=${cmd#*:}
printf " %-10s - %s\n" "$CMD_NAME" "$DESC"
done
}
get_command_description() {
local search_cmd=$1
for cmd in "${COMMANDS[@]}"; do
CMD_NAME=${cmd%%:*}
DESC=${cmd#*:}
if [ "$CMD_NAME" = "$search_cmd" ]; then
echo "$DESC"
return 0
fi
done
echo "Command help"
}
# Command definitions
COMMANDS=(
"ls:List directory contents"
"cd:Change the current directory"
"pwd:Print the name of the current working directory"
"cp:Copy files and directories"
"mv:Move or rename files and directories"
"rm:Remove files or directories"
"mkdir:Create a new directory"
"rmdir:Remove an empty directory"
"grep:Print lines matching a pattern"
"find:Search for files in a directory hierarchy"
"echo:Display a line of text"
)
# Parse arguments
INTERACTIVE=false
COMMAND=""
FILTER=""
if [ $# -eq 0 ]; then
INTERACTIVE=true
else
case "$1" in
-h|--help)
show_help
exit 0
;;
-l|--list)
list_commands
exit 0
;;
-i|--interactive)
INTERACTIVE=true
;;
-*)
echo "Unknown option: $1"
echo "Use --help for usage information"
exit 1
;;
*)
COMMAND=$1
FILTER=$2
;;
esac
fi
# Interactive mode with fzf
if [ "$INTERACTIVE" = true ]; then
# Check if fzf is available
if ! command -v fzf &> /dev/null; then
echo "Error: fzf is not installed"
echo "Install it with: sudo apt install fzf"
echo ""
echo "Or use direct mode: bash-helper COMMAND [FILTER]"
exit 1
fi
FZF_COMMAND=$(printf "%s\n" "${COMMANDS[@]}" | fzf --height 40% --border --preview 'echo {}' --preview-window=up:1:wrap)
if [ -n "$FZF_COMMAND" ]; then
CMD=${FZF_COMMAND%%:*}
DESC=${FZF_COMMAND#*:}
echo ""
echo -e "\033[0;34m═══════════════════════════════════════════════════════════\033[0m"
echo -e "\033[0;32mCommand:\033[0m $CMD"
echo -e "\033[0;32mDescription:\033[0m $DESC"
echo -e "\033[0;34m═══════════════════════════════════════════════════════════\033[0m"
echo ""
read -p "Filter flags (optional, press Enter to skip): " FILTER
echo ""
show_flags "$CMD" "$FILTER"
else
echo "No command selected"
exit 0
fi
# Direct mode
elif [ -n "$COMMAND" ]; then
# Validate command exists in our list
FOUND=false
for cmd in "${COMMANDS[@]}"; do
CMD_NAME=${cmd%%:*}
if [ "$CMD_NAME" = "$COMMAND" ]; then
FOUND=true
DESC=${cmd#*:}
break
fi
done
if [ "$FOUND" = false ]; then
echo "Warning: '$COMMAND' is not in the predefined list"
echo "Available commands:"
list_commands
echo ""
echo "Attempting to show flags anyway..."
echo ""
fi
# Show command info
DESC=$(get_command_description "$COMMAND")
echo ""
echo -e "\033[0;34m═══════════════════════════════════════════════════════════\033[0m"
echo -e "\033[0;32mCommand:\033[0m $COMMAND"
echo -e "\033[0;32mDescription:\033[0m $DESC"
echo -e "\033[0;34m═══════════════════════════════════════════════════════════\033[0m"
echo ""
show_flags "$COMMAND" "$FILTER"
else
show_help
exit 1
fi