pstray / rename

Rename renames the filenames supplied according to the rule specified as the first argument. The argument is a Perl expression which is expected to modify the $_ string for at least some of the filenames specified.
Other
6 stars 1 forks source link

[feature] shell completion #6

Closed Freed-Wu closed 1 year ago

Freed-Wu commented 1 year ago

Can perl-rename have a shell completion like parallel? TIA!

Freed-Wu commented 1 year ago

parallel also have a zsh completion: https://git.savannah.gnu.org/cgit/parallel.git/tree/src/parallel#n1646. How about it?

pstray commented 1 year ago

I don't use zsh personally, so I have no experience with completion there... Seems it would need a bit larger rewrite to handle that since it seems to need option descriptions too.

Freed-Wu commented 1 year ago

I write a zsh completion script. Can rename generate it?

#compdef perl-rename

local options=(
  {-b,--backup}"[make backup before removal]"
  {-B,--prefix}"[set backup filename prefix]:suffix"
  {-f,--force}"[remove existing destinations, never prompt]"
  {-i,--interactive}"[prompt before overwrite]"
  {-l,--link-only}"[link file instead of rename]"
  {-n,--just-print,--dry-run}"[don't rename, implies --verbose]"
  {-v,--verbose}"[explain what is being done]"
  {-V,--version-control}"[override the usual version control]:method:(numbered t existing nil simple never)"
  {-Y,--basename-prefix}"[set backup filename basename prefix]:prefix"
  {-z,-S,--suffix}"[set backup filename suffix]:suffix"
  '(- : *)'--help"[display this help and exit]"
  '(- : *)'--version"[output version information and exit]"
)

_arguments -S -s $options '1:perl expr' '*:: :_files'
pstray commented 1 year ago

I'll have a look at how to best generate those lines... should be doable :)

But what are the '(- : *)'-parts before --help and --version?

Freed-Wu commented 1 year ago

But what are the '(- : *)'-parts before --help and --version?

This is group. If two options are exclusive, such as

local options=(
  --bar
  '(--interactive)--nointeractive'
  '(--nointeractive)--interactive'
)
_argument $options ''

so foo --interactive <TAB> will only complete --bar, not --nointeractive.

- is a special group which stop all -*. * is a special group stop all *. : doesn't exist here which can be removed.

Another example:

local options=(
  '(- : *)--help'
)
_argument $options ':operations:(add sub)' '*::file:_files'

foo <TAB> will complete add or sub. foo add <TAB> will complete files. foo add a.txt b.txt ... <TAB> will complete files, too. However, foo add --help <TAB> will not complete files due to *. foo --help <TAB> will not complete add and sub due to :.

Freed-Wu commented 1 year ago

Here is document https://zsh.sourceforge.io/Doc/Release/Completion-System.html#Completion-Functions.

pstray commented 1 year ago

Ah, I see, that is quite useful.