Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Pax completion?



On 9 Apr, The Matt wrote:
> 
> Folks, I was wondering if anyone out there has made completion bits for
> pax, the portable archiver?  I'm not a hacker, so once I looked at the
> _cpio and _tar functions, I was kinda scared to build my own.  (The bash
> completion project also doesn't have the pax so I can't try to copy
> theirs.)

Don't let the complexity of _tar or _cpio worry you. tar's options are
particulary complicated (-C is especially messy to handle) and the
worst part is handling completion of files inside an archive. It
shouldn't be hard to write basic pax completion that does most of what
you want.

> Barring anyone actually having one, how would I go about associating pax
> with at least the file types I use it for (*.pax, .tar, .tar.gz, .cpio,

  _pax() { _files -g '*.(tar|pax|cpio|tar.gz)' }
  compdef _pax pax

would do the job. Or in zsh 4.1, you can do:

  compdef "_files -g '*.(tar|pax|cpio|tar.gz)'" pax

> &c.).  Would something like:
> 
> compctl -g '*.(cpio|tar|pax) pax -f
> 

The final `-f' wouldn't do what you think but with slight changes that
would roughly work. I'd advise using compdef instead though.

To write a decent pax completion, I'd suggest starting by getting the
options together using _arguments. As a start, you might have:

_arguments -s \
  '(-a -w)-r[extract from archive]' \
  '(-a -r)-w[write to archive]' \
  '(-r -w)-a[append files to end of archive]' \
  '-b+[specify block size]:block size' \
  '-x+[specify output archive format]:archive format:(cpio bcpio sv4cpio sv4crc tar ustar)'

For the -f option, it would be something like:
  '-f+[specifiy archive file]:archive file:_files -g "*.(pax|tar|cpio)"'

> work?  And is there a similar way for pax -zf to expand for .pax and

Getting it to detect a -z option for gzip is not trivial however. You
could check for -z manually in $words (which is what _tar does) but it
is more reliable to use _argument's opt_args association which is set
to the options which have been specified on the line.

The following works by appending (|.gz) to the glob if it finds the -z
option.

  '-f+[specifiy archive file]:archive file:_files -g "*.(pax|tar|cpio)${opt_args[(I)-z]\:+(|.gz)}"'

> .tar.gz files?  Or should I buckle down and try to bring together the
> _cpio and _tar* into one _pax?

You'd find it easier starting from scratch. _zip is probably a simpler
example of this sort of stuff - _tar is one of the oldest functions.
And if you run into problems, you can always ask here.

Oliver



Messages sorted by: Reverse Date, Date, Thread, Author