Ok,

I gave a quick go and decided that you were right :-)  Man pages are
very varied and can't be relied upon.  However, then I decided that
perhaps the job of making completions could be semi automated.  I wrote
a bit of perl that will work out what switches and options are available
from the man page and will create a fish script will the relavent
commands for each option.  It also puts comments in the fish script to
show some context concerning the option so that you can quickly fill in
the description text.  It's only a prototype but see what you think.  

You run it by doing:

./opt_sum.pl <command>

(I've also included a sample output for ls)

Limitations (it's only a prototype!)

1. It currently only does options that look like "-s" or "--long", it
can't do "-onedash" options yet.  (So don't use it on the find command).
You can run it by doing:

2. It can't reliably match up -s and --long options into one "complete"
command.

3. It currently can't cope with the idea of multiple man commands for
the same term (it just hokily searches /usr/share/man for the first one
it finds, if that's not the right one then it just produces rubbish).

4. I'm sure there's more.

I'll fix these things next week :-)

On Friday 21 April 2006 11:16, Axel Liljencrantz wrote:
> On 4/21/06, Ben Staniford <[EMAIL PROTECTED]> wrote:
> > Hi there,
> >
> > I notice that fish uses the man pages to complete man help style
> > completions.  But is there any reason that man pages to build command
> > completions, rather than building completions manually?  Man pages seem
> > to contain lots of things like OptDef that describe options that are
> > possible for commands.
> 
> Man-pages are rather losely structured, while there are many
> conventions, few of them are strictly followed by the individual
> pages, making it a very difficult task to correctly parse the
> structure of a man-page. That said, there is a project called
> doclifter, that tries to do just that. It could probably be used as
> the base for an automatic  completion-parser. You are more than
> welcome to give it a try.
> 
> >
> > Kind Regards,
> >
> > --
> > Ben Staniford
> > http://www.staniford.net
> 
> 
> --
> Axel
> 

-- 
Ben Staniford
(023) 9236 9315
http://www.staniford.net



# Context for -m (from man page)
 # -m fill width with a comma separated list of entries -n, --numeric-uid-gid 
like -l, but list numeric UIDs and
# GIDs -N, --literal print raw entry names (don't treat e.g. control
complete -c ls -s -m -a ""

# Context for -d (from man page)
 # --dereference-command-line -symlink-to-dirfollow each command line symbolic 
link that points to a directory
# --hide=PATTERN do not list implied entries matching shell PATTERN (overridden 
by -a or -A)
complete -c ls -s -d -a ""

# Context for --show-control-chars (from man page)
 # --show-control-chars show non graphic characters as-is (default unless 
program is `ls' and output is a terminal)
# -Q, --quote-name enclose entry names in double quotes --quoting-style=WORD
complete -c ls -l --show-control-chars -a ""

# Context for --dereference-command-line (from man page)
 # --dereference-command-line -symlink-to-dirfollow each command line symbolic 
link that points to a directory
# --hide=PATTERN do not list implied entries matching shell PATTERN (overridden 
by -a or -A)
complete -c ls -l --dereference-command-line -a ""

# Context for --dereference (from man page)
 # -L, --dereference when showing file information for a symbolic link, show 
information for the file the link refer
# ences rather than for the link itself -m fill width with a comma separated 
list of entries
complete -c ls -l --dereference -a ""

# Context for --full-time (from man page)
 # --full-time like -l --time-style=full-iso -g like -l, but do not list owner 
-G, --no-group like -l, but do not list
# group
complete -c ls -l --full-time -a ""

# Context for --inode (from man page)
 # -i, --inode with -l, print the index number of each file -I, 
--ignore=PATTERN do not list implied entries matching
# shell PATTERN -k like --block-size=1K
complete -c ls -l --inode -a ""

# Context for -w (from man page)
 # -w, --width =COLSassume screen width instead of current value -x list 
entries by lines instead of by columns -X
# sort alphabetically by entry extension
complete -c ls -s -w -a ""

# Context for -k (from man page)
 # -k like --block-size=1K -l use a long listing format -L, --dereference when 
showing file information for a sym
# bolic
complete -c ls -s -k -a ""

# Context for --help (from man page)
 # --help display this help and exit --version output version information and 
exit SIZE may be (or may be an integer
# optionally followed by) one of following: kB 1000, K 1024, MB 1000*1000, M 
1024*1024, and so on for G, T, P, E,
# Z, Y.
complete -c ls -l --help -a ""

# Context for -N (from man page)
 # -N, --literal print raw entry names (don't treat e.g. control characters 
specially) -o like -l, but do not list group in
# formation -p, --file-type
complete -c ls -s -N -a ""

# Context for -B (from man page)
 # -B, --ignore-backups do not list implied entries ending with ~ -c with -lt: 
sort by, and show, ctime (time of last
# modification of file status information) with -l: show ctime and sort by name 
otherwise: sort by ctime
complete -c ls -s -B -a ""

# Context for --directory (from man page)
 # -d, --directory list directory entries instead of contents, and do not 
dereference symbolic links -D, --dired gener
# ate output designed for Emacs' dired mode -f
complete -c ls -l --directory -a ""

# Context for -X (from man page)
 # -X sort alphabetically by entry extension -1 list one file per line SELINUX 
options:
complete -c ls -s -X -a ""

# Context for -r (from man page)
 # non-r ecent files and FORMAT2 to recent files;if STYLE is prefixed with 
`posix-', STYLE takes effect only outside
# the POSIX locale -t sort by modification time -T, --tabsize=COLS
complete -c ls -s -r -a ""

# Context for -C (from man page)
 # -C list entries by columns --color[=WHEN] control whether color is used to 
distinguish file types. WHEN may be
# `never', `always', or `auto' -d, --directory
complete -c ls -s -C -a ""

# Context for --dired (from man page)
 # -D, --dired generate output designed for Emacs' dired mode -f do not sort, 
enable -aU, disable -lst -F, --classify
# append indicator (one of */=@|) to entries
complete -c ls -l --dired -a ""

# Context for -u (from man page)
 # -u with -lt: sort by, and show, access time with -l: show access time and 
sort by name otherwise: sort by access time
# -U do not sort; list entries in directory order
complete -c ls -s -u -a ""

# Context for --lcontext (from man page)
 # --lcontext Display security context. Enable -l. Lines will probably be too 
wide for most displays. --context Dis
# play security context so it fits on most displays. Displays only mode, user, 
group, security context and file name.
complete -c ls -l --lcontext -a ""

# Context for --recursive (from man page)
 # -R, --recursive list subdirectories recursively -s, --size with -l, print 
size of each file, in blocks -S sort by file
# size
complete -c ls -l --recursive -a ""

# Context for -S (from man page)
 # -S sort by file size --sort=WORD extension -X, none -U, size -S, time -t, 
version -v, status -c, time -t, atime -u,
# access -u, use -u
complete -c ls -s -S -a ""

# Context for -s (from man page)
 # --scontext Display only security context and file name. --help display this 
help and exit --version output version
# information and exit
complete -c ls -s -s -a ""

# Context for -A (from man page)
 # -A, --almost-all do not list implied . and .. --author with -l, print the 
author of each file -b, --escape print octal
# escapes for nongraphic characters
complete -c ls -s -A -a ""

# Context for --width (from man page)
 # -w, --width =COLSassume screen width instead of current value -x list 
entries by lines instead of by columns -X
# sort alphabetically by entry extension
complete -c ls -l --width -a ""

# Context for -g (from man page)
 # -g like -l, but do not list owner -G, --no-group like -l, but do not list 
group -h, --human-readable with -l,
# print sizes in human readable format
complete -c ls -s -g -a ""

# Context for -G (from man page)
 # -G, --no-group like -l, but do not list group -h, --human-readable with -l, 
print sizes in human readable for
# mat (e.g., 1K 234M 2G) --si
complete -c ls -s -G -a ""

# Context for --version (from man page)
 # --version output version information and exit SIZE may be (or may be an 
integer optionally followed by) one of
# following: kB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, 
E, Z, Y. By default, color is not
# used to distinguish types of files. That is equivalent to using --color=none. 
Using the --color option without the
complete -c ls -l --version -a ""

# Context for --file-type (from man page)
 # -p, --file-type append indicator (one of /=@|) to entries -q, 
--hide-control-chars print ? instead of non graphic
# characters --show-control-chars show non graphic characters as-is (default
complete -c ls -l --file-type -a ""

# Context for -n (from man page)
 # -n, --numeric-uid-gid like -l, but list numeric UIDs and GIDs -N, --literal 
print raw entry names (don't treat
# e.g. control characters specially) -o
complete -c ls -s -n -a ""

# Context for --time (from man page)
 # --time =WORDwith -l, show time as WORD instead of modification time: atime, 
access, use, ctime or status; use
# specified time as sort key if --sort=time --time-style=STYLE with -l, show 
times using style STYLE: full-iso,
# long-iso, iso, locale, +FORMAT.
complete -c ls -l --time -a ""

# Context for -D (from man page)
 # -D, --dired generate output designed for Emacs' dired mode -f do not sort, 
enable -aU, disable -lst -F, --classify
# append indicator (one of */=@|) to entries
complete -c ls -s -D -a ""

# Context for --color (from man page)
 # --color =auto, color codes are output only if standard output is connectedto 
a terminal (tty). Exit status is 0 if OK, 1
# if minor problems, 2 if serious trouble. Written by Richard Stallman and 
David MacKenzie. Report bugs to
# .
complete -c ls -l --color -a ""

# Context for --size (from man page)
 # -s, --size with -l, print size of each file, in blocks -S sort by file size 
--sort=WORD extension -X, none -U, size
# -S, time -t,
complete -c ls -l --size -a ""

# Context for -F (from man page)
 # none (default), classify (-F ), file-type (-p).TP -i, --inode with -l, print 
the index number of each file -I, --ig
# nore=PATTERN do not list implied entries matching shell PATTERN
complete -c ls -s -F -a ""

# Context for --no-group (from man page)
 # -G, --no-group like -l, but do not list group -h, --human-readable with -l, 
print sizes in human readable for
# mat (e.g., 1K 234M 2G) --si
complete -c ls -l --no-group -a ""

# Context for --author (from man page)
 # --author with -l, print the author of each file -b, --escape print octal 
escapes for nongraphic characters
# --block-size=SIZE use SIZE-byte blocks
complete -c ls -l --author -a ""

# Context for --escape (from man page)
 # -b, --escape print octal escapes for nongraphic characters --block-size=SIZE 
use SIZE-byte blocks -B, --ig
# nore-backups do not list implied entries ending with ~
complete -c ls -l --escape -a ""

# Context for -x (from man page)
 # -x list entries by lines instead of by columns -X sort alphabetically by 
entry extension -1 list one file per line
complete -c ls -s -x -a ""

# Context for --scontext (from man page)
 # --scontext Display only security context and file name. --help display this 
help and exit --version output version
# information and exit
complete -c ls -l --scontext -a ""

# Context for -H (from man page)
 # -H, --dereference-command-line follow symbolic links listed on the command 
line --dereference-com
# mand-line-symlink-to-dir follow each command line symbolic link that points 
to a directory --hide=PATTERN
complete -c ls -s -H -a ""

# Context for -o (from man page)
 # -o like -l, but do not list group information -p, --file-type append 
indicator (one of /=@|) to entries -q,
# --hide-control-chars print ? instead of non graphic characters
complete -c ls -s -o -a ""

# Context for --time-style (from man page)
 # --time-style =STYLEwith -l, show times using style STYLE: full-iso, 
long-iso, iso, locale, +FORMAT. FORMAT
# is interpreted like `date'; if FORMAT is FORMAT1FORMAT2, FORMAT1 applies to 
non-recent files
# and FORMAT2 to recent files; if STYLE is prefixed with `posix-', STYLE takes 
effect only outside the POSIX lo
# cale
complete -c ls -l --time-style -a ""

# Context for -f (from man page)
 # --full-time like -l --time-style=full-iso -g like -l, but do not list owner 
-G, --no-group like -l, but do not list
# group
complete -c ls -s -f -a ""

# Context for --quote-name (from man page)
 # -Q, --quote-name enclose entry names in double quotes --quoting-style=WORD 
use quoting style WORD for
# entry names: literal, locale, shell, shell-always, c, escape -r, --reverse
complete -c ls -l --quote-name -a ""

# Context for --classify (from man page)
 # -F, --classify append indicator (one of */=@|) to entries --format=WORD 
across -x, commas -m, horizontal -x,
# long -l, single-column -1, verbose -l, vertical -C --full-time
complete -c ls -l --classify -a ""

# Context for -T (from man page)
 # -T, --tabsize =COLSassume tab stops at each COLS instead of 8 -u with -lt: 
sort by, and show, access time with
# -l: show access time and sort by name otherwise: sort by access time
complete -c ls -s -T -a ""

# Context for --tabsize (from man page)
 # -T, --tabsize =COLSassume tab stops at each COLS instead of 8 -u with -lt: 
sort by, and show, access time with
# -l: show access time and sort by name otherwise: sort by access time
complete -c ls -l --tabsize -a ""

# Context for --hide-control-chars (from man page)
 # -q, --hide-control-chars print ? instead of non graphic characters 
--show-control-chars show non graphic
# characters as-is (default unless program is `ls' and output is a terminal) 
-Q, --quote-name
complete -c ls -l --hide-control-chars -a ""

# Context for -L (from man page)
 # -L, --dereference when showing file information for a symbolic link, show 
information for the file the link refer
# ences rather than for the link itself -m fill width with a comma separated 
list of entries
complete -c ls -s -L -a ""

# Context for -R (from man page)
 # -R, --recursive list subdirectories recursively -s, --size with -l, print 
size of each file, in blocks -S sort by file
# size
complete -c ls -s -R -a ""

# Context for --si (from man page)
 # --si likewise, but use powers of 1000 not 1024 -H, 
--dereference-command-line follow symbolic links listed on
# the command line --dereference-command-line-symlink-to-dir follow each 
command line symbolic link
complete -c ls -l --si -a ""

# Context for --context (from man page)
 # --context Display security context so it fits on most displays. Displays 
only mode, user, group, security context and
# file name. --scontext Display only security context and file name.
complete -c ls -l --context -a ""

# Context for -h (from man page)
 # --help display this help and exit --version output version information and 
exit SIZE may be (or may be an integer
# optionally followed by) one of following: kB 1000, K 1024, MB 1000*1000, M 
1024*1024, and so on for G, T, P, E,
# Z, Y.
complete -c ls -s -h -a ""

# Context for --ignore (from man page)
 # -I, --ignore =PATTERNdo not list implied entries matching shell PATTERN -k 
like --block-size=1K -l use a
# long listing format
complete -c ls -l --ignore -a ""

# Context for --ignore-backups (from man page)
 # -B, --ignore-backups do not list implied entries ending with ~ -c with -lt: 
sort by, and show, ctime (time of last
# modification of file status information) with -l: show ctime and sort by name 
otherwise: sort by ctime
complete -c ls -l --ignore-backups -a ""

# Context for -q (from man page)
 # --quoting-style =WORDuse quoting style WORD for entry names: literal, 
locale, shell, shell-always, c, escape -r,
# --reverse reverse order while sorting -R, --recursive
complete -c ls -s -q -a ""

# Context for --block-size (from man page)
 # like --block-size =1K.TP -l use a long listing format -L, --dereference when 
showing file information for a sym
# bolic link, show information for the file the link
complete -c ls -l --block-size -a ""

# Context for -v (from man page)
 # --version output version information and exit SIZE may be (or may be an 
integer optionally followed by) one of
# following: kB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, 
E, Z, Y. By default, color is not
# used to distinguish types of files. That is equivalent to using --color=none. 
Using the --color option without the
complete -c ls -s -v -a ""

# Context for -b (from man page)
 # like --block-size =1K.TP -l use a long listing format -L, --dereference when 
showing file information for a sym
# bolic link, show information for the file the link
complete -c ls -s -b -a ""

# Context for --literal (from man page)
 # -N, --literal print raw entry names (don't treat e.g. control characters 
specially) -o like -l, but do not list group in
# formation -p, --file-type
complete -c ls -l --literal -a ""

# Context for --human-readable (from man page)
 # -h, --human-readable with -l, print sizes in human readable format (e.g., 1K 
234M 2G) --si likewise, but use
# powers of 1000 not 1024 -H, --dereference-command-line
complete -c ls -l --human-readable -a ""

# Context for -c (from man page)
 # Report bugs to  2005 Free Software Foundation, Inc.
# This is free software; see the source for copying conditions. There is NO 
warranty; not even for MER
# CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The full documentation for
complete -c ls -s -c -a ""

# Context for --almost-all (from man page)
 # -A, --almost-all do not list implied . and .. --author with -l, print the 
author of each file -b, --escape print octal
# escapes for nongraphic characters
complete -c ls -l --almost-all -a ""

# Context for -I (from man page)
 # -I, --ignore =PATTERNdo not list implied entries matching shell PATTERN -k 
like --block-size=1K -l use a
# long listing format
complete -c ls -s -I -a ""

# Context for --hide (from man page)
 # --hide =PATTERNdo not list implied entries matching shell PATTERN 
(overridden by -a or -A) --indica
# tor-style=WORD append indicator with style WORD to entry names: none 
(default), classify (-F), file-type (-p)
# -i, --inode
complete -c ls -l --hide -a ""

# Context for -t (from man page)
 # -t sort by modification time -T, --tabsize=COLS assume tab stops at each 
COLS instead of 8 -u with -lt: sort by,
# and show, access time
complete -c ls -s -t -a ""

# Context for --numeric-uid-gid (from man page)
 # -n, --numeric-uid-gid like -l, but list numeric UIDs and GIDs -N, --literal 
print raw entry names (don't treat
# e.g. control characters specially) -o
complete -c ls -l --numeric-uid-gid -a ""

# Context for --sort (from man page)
 # specified time as sort key if --sort =time.TP --time-style=STYLE with -l, 
show times using style STYLE: full-iso,
# long-iso, iso, locale, +FORMAT. FORMAT is interpreted like `date'; if FORMAT 
is FORMAT1
# MAT2, FORMAT1 applies to non-recent files and FORMAT2 to recent files;
complete -c ls -l --sort -a ""

# Context for -l (from man page)
 # Display security context. Enable -l . Lineswill probably be too wide for 
most displays. --context Display security
# context so it fits on most displays. Displays only mode, user, group, 
security context and file name.
complete -c ls -s -l -a ""

# Context for -Q (from man page)
 # -Q, --quote-name enclose entry names in double quotes --quoting-style=WORD 
use quoting style WORD for
# entry names: literal, locale, shell, shell-always, c, escape -r, --reverse
complete -c ls -s -Q -a ""

# Context for --indicator-style (from man page)
 # --indicator-style =WORD append indicator with style WORD to entry names:none 
(default), classify (-F),
# file-type (-p) -i, --inode with -l, print the index number of each file -I, 
--ignore=PATTERN do not list implied
# entries matching shell PATTERN
complete -c ls -l --indicator-style -a ""

# Context for -i (from man page)
 # full-i so, long-iso, iso, locale, +FORMAT.FORMAT is interpreted like `date'; 
if FORMAT is FORMAT1
# line>FORMAT2, FORMAT1 applies to non-recent files and FORMAT2 to recent 
files; if STYLE is prefixed with
# `posix-', STYLE takes effect only outside the POSIX locale -t
complete -c ls -s -i -a ""

# Context for -p (from man page)
 # -p, --file-type append indicator (one of /=@|) to entries -q, 
--hide-control-chars print ? instead of non graphic
# characters --show-control-chars show non graphic characters as-is (default
complete -c ls -s -p -a ""

# Context for --all (from man page)
 # -a, --all do not ignore entries starting with . -A, --almost-all do not list 
implied . and .. --author with -l, print
# the author of each file
complete -c ls -l --all -a ""

# Context for --quoting-style (from man page)
 # --quoting-style =WORDuse quoting style WORD for entry names: literal, 
locale, shell, shell-always, c, escape -r,
# --reverse reverse order while sorting -R, --recursive
complete -c ls -l --quoting-style -a ""

# Context for --reverse (from man page)
 # -r, --reverse reverse order while sorting -R, --recursive list 
subdirectories recursively -s, --size with -l, print
# size of each file, in blocks
complete -c ls -l --reverse -a ""

# Context for -a (from man page)
 # literal, locale, shell, shell-a lways, c, escape.TP -r, --reverse reverse 
order while sorting -R, --recursive list sub
# directories recursively
complete -c ls -s -a -a ""

# Context for --format (from man page)
 # --format =WORDacross -x, commas -m, horizontal -x, long -l, single-column 
-1, verbose -l, vertical -C
# --full-time like -l --time-style=full-iso -g
complete -c ls -l --format -a ""

# Context for -U (from man page)
 # -U do not sort; list entries in directory order -v sort by version -w, 
--width=COLS assume screen width instead of
# current value
complete -c ls -s -U -a ""

# Context for -1 (from man page)
 # -1 list one file per line SELINUX options: --lcontext Display security 
context. Enable -l. Lines will probably be
# too wide for most displays.
complete -c ls -s -1 -a ""

Attachment: opt_sum.pl
Description: Perl program

Reply via email to