{"thread":{"id":"3886","subject":"[FYI] generated completions","startedAt":"2006-04-16T02:05:26Z","lastAt":"2006-04-16T02:05:26Z","messageCount":1,"participants":["Johannes Schindelin"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"18721","messageId":"Pine.LNX.4.63.0604160401330.31461@wbgn013.biozentrum.uni-wuerzburg.de","threadId":"3886","inReplyTo":null,"subject":"[FYI] generated completions","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2006-04-16T02:05:26Z","receivedAt":"2006-04-16T02:05:26Z","isPatch":false,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nI just played around with command line completion in bash, mainly to \ndistract me from the work I have to do.\n\nThis is a shell script which generates a completion script from the \ndocumentation. It is very dumb at the moment, but at least a start. I am \nnot likely to continue with this in the near future, so everyone who's \ninterested: go wild.\n\n--- generate-completions.sh ---\n#!/bin/sh\n\ncat << EOF\nfunction gitcomplete ()\n{\n\tcase \"\\$COMP_LINE\" in\nEOF\n\ncmds=\"\"\nfor file in Documentation/git-*.txt; do\n\tcmd=$(expr $file : \"Documentation/git-\\(.*\\).txt\")\n\tcmds=\"$cmds $cmd\"\n\techo \"\tgit\\ $cmd\\ *)\"\n\techo '\t\tcase \"$3\" in'\n\n\t# get all lines which begin with '-' or '<' and end with '::',\n\t# because they likely contain a command line option\n\tsed -n \"s/^\\([-<].*\\)::/\\1/p\" < $file | { \\\n\t\topts=\"\"\n\t\twhile read line; do\n\t\t\t# some lines have the form '-a|--all'\n\t\t\topt1=$(expr \"$line\" : '^\\([^ |<]*\\)')\n\t\t\topt2=$(expr \"$line\" : '|\\([^ |<]*\\)')\n\t\t\topts=\"$opts $opt1 $opt2\"\n\t\t\t# this is an example how to handle non-simple args:\n\t\t\t# '-m <msg>' means: if the second-last arg is '-m',\n\t\t\t# the current arg is the message.\n\t\t\ttest \"$(expr \"$line\" : \".*<msg>\")\" != 0 &&\n\t\t\t\techo \"\t\t'$opt1')\" &&\n\t\t\t\techo '\t\t\tCOMPREPLY=(\\\"a \\\"z);;'\n\t\tdone\n\t\techo \"\t\t*)\"\n\t\techo \"\t\t\tCOMPREPLY=(\\$(compgen -W \\\"$opts\\\" -- \\$2));;\"\n\t}\n\techo \"\t\tesac;;\"\ndone\n\ncat << EOF\n\tgit\\ *\\ *)\n\t\tCOMPREPLY=();;\n\tgit\\ *)\n\t\tCOMPREPLY=(\\$(compgen -W '$cmds' -- \\$2));;\n\tesac\n}\n\ncomplete -o default -F gitcomplete git\nEOF\n--- cut ---\n"}]}