Add a completion script generator for the fish shell
This commit is contained in:
		
							parent
							
								
									eb3bd7ba8d
								
							
						
					
					
						commit
						56d1912f1d
					
				
					 6 changed files with 70 additions and 6 deletions
				
			
		
							
								
								
									
										5
									
								
								devscripts/fish-completion.in
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								devscripts/fish-completion.in
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,5 @@
 | 
			
		|||
 | 
			
		||||
{{commands}}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
complete --command youtube-dl --arguments ":ytfavorites :ytrecommended :ytsubscriptions :ytwatchlater :ythistory"
 | 
			
		||||
							
								
								
									
										48
									
								
								devscripts/fish-completion.py
									
										
									
									
									
										Executable file
									
								
							
							
						
						
									
										48
									
								
								devscripts/fish-completion.py
									
										
									
									
									
										Executable file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,48 @@
 | 
			
		|||
#!/usr/bin/env python
 | 
			
		||||
from __future__ import unicode_literals
 | 
			
		||||
 | 
			
		||||
import optparse
 | 
			
		||||
import os
 | 
			
		||||
from os.path import dirname as dirn
 | 
			
		||||
import sys
 | 
			
		||||
 | 
			
		||||
sys.path.append(dirn(dirn((os.path.abspath(__file__)))))
 | 
			
		||||
import youtube_dl
 | 
			
		||||
from youtube_dl.utils import shell_quote
 | 
			
		||||
 | 
			
		||||
FISH_COMPLETION_FILE = 'youtube-dl.fish'
 | 
			
		||||
FISH_COMPLETION_TEMPLATE = 'devscripts/fish-completion.in'
 | 
			
		||||
 | 
			
		||||
EXTRA_ARGS = {
 | 
			
		||||
    'recode-video': ['--arguments', 'mp4 flv ogg webm mkv', '--exclusive'],
 | 
			
		||||
 | 
			
		||||
    # Options that need a file parameter
 | 
			
		||||
    'download-archive': ['--require-parameter'],
 | 
			
		||||
    'cookies': ['--require-parameter'],
 | 
			
		||||
    'load-info': ['--require-parameter'],
 | 
			
		||||
    'batch-file': ['--require-parameter'],
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
def build_completion(opt_parser):
 | 
			
		||||
    commands = []
 | 
			
		||||
 | 
			
		||||
    for group in opt_parser.option_groups:
 | 
			
		||||
        for option in group.option_list:
 | 
			
		||||
            long_option = option.get_opt_string().strip('-')
 | 
			
		||||
            help_msg = shell_quote([option.help])
 | 
			
		||||
            complete_cmd = ['complete', '--command', 'youtube-dl', '--long-option', long_option]
 | 
			
		||||
            if option._short_opts:
 | 
			
		||||
                complete_cmd += ['--short-option', option._short_opts[0].strip('-')]
 | 
			
		||||
            if option.help != optparse.SUPPRESS_HELP:
 | 
			
		||||
                complete_cmd += ['--description', option.help]
 | 
			
		||||
            complete_cmd.extend(EXTRA_ARGS.get(long_option, []))
 | 
			
		||||
            commands.append(shell_quote(complete_cmd))
 | 
			
		||||
 | 
			
		||||
    with open(FISH_COMPLETION_TEMPLATE) as f:
 | 
			
		||||
        template = f.read()
 | 
			
		||||
    filled_template = template.replace('{{commands}}', '\n'.join(commands))
 | 
			
		||||
    with open(FISH_COMPLETION_FILE, 'w') as f:
 | 
			
		||||
        f.write(filled_template)
 | 
			
		||||
 | 
			
		||||
parser = youtube_dl.parseOpts()[0]
 | 
			
		||||
build_completion(parser)
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue