Skip to content

Instantly share code, notes, and snippets.

@DimitriGilbert
Last active July 1, 2024 10:17
Show Gist options
  • Select an option

  • Save DimitriGilbert/15ba85746846ff77378ac3aea7ef3c81 to your computer and use it in GitHub Desktop.

Select an option

Save DimitriGilbert/15ba85746846ff77378ac3aea7ef3c81 to your computer and use it in GitHub Desktop.
groupier ! groups a bunch of files in a single output to feed to LLM
#!/bin/bash
# Check if parseArger is installed
if ! command -v parseArger &> /dev/null; then
read -p "parseArger is not installed. Would you like to install it? (yes/y to confirm): " install_parseArger
if [[ "$install_parseArger" =~ ^[Yy](es)?$ ]]; then
# Install parseArger
echo "Installing parseArger..."
# Download the install script
curl -s https://raw.githubusercontent.com/DimitriGilbert/parseArger/main/utils/get_parseArger -O
# Make it executable
chmod +x get_parseArger
# Install
./get_parseArger --install
# Source bashrc
source "$HOME/.bashrc"
else
echo "parseArger is required to run this script. Exiting."
exit 1
fi
fi
parseArger generate --help-message "Display file contents with separators" \
--leftovers \
--opt "separator 'Separator to use between file contents' --default-value '========'" \
--opt "save 'File to save the output'" \
--output groupier
cat <<'EOF' >> groupier
separator="$_arg_separator"
output_file="$_arg_save"
process_file() {
local filepath="$1"
if [[ -f "$filepath" ]]; then
echo "$filepath"
echo "$separator"
cat "$filepath"
echo "$separator"
echo
else
echo "File not found: $filepath" >&2
fi
}
collect_files() {
file_list=()
for filepath in "${_arg_leftovers[@]}"; do
if [[ -d "$filepath" ]]; then
for file in "$filepath"/*; do
file_list+=("$file")
done
else
file_list+=("$filepath")
fi
done
}
# Collect files to process
collect_files
# Process and optionally save output
if [[ -n "$output_file" ]]; then
{
for file in "${file_list[@]}"; do
process_file "$file"
done
} > "$output_file"
else
for file in "${file_list[@]}"; do
process_file "$file"
done
fi
EOF
@DimitriGilbert
Copy link
Author

This script installs parseArger if you do not have it, generate the parsing code using parseArger and add the script content to the file

Display file contents with separators:
	--separator <separator>: Separator to use between file contents [default: ' ======== ']
	--save <save>: File to save the output
Usage :
	groupier <file|directory> [file|directory ...] [--separator <value>] [--save <value>]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment