mirror of
				https://github.com/mitchell/swim.fish.git
				synced 2025-11-03 23:35:25 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			66 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Fish
		
	
	
		
			Executable file
		
	
	
	
	
			
		
		
	
	
			66 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Fish
		
	
	
		
			Executable file
		
	
	
	
	
#!/usr/bin/env fish
 | 
						|
 | 
						|
source ./run.fish
 | 
						|
 | 
						|
function main
 | 
						|
    define_aliases
 | 
						|
 | 
						|
    for command in $argv
 | 
						|
        set -l last_status $status
 | 
						|
        test $last_status -gt 0; and exit $last_status
 | 
						|
 | 
						|
        execute_command 'run' $command
 | 
						|
    end
 | 
						|
 | 
						|
    if test -z "$argv"
 | 
						|
        execute_command 'run' default
 | 
						|
    end
 | 
						|
end
 | 
						|
 | 
						|
function execute_command -a prefix command
 | 
						|
    set -l args (string split --max 1 $run_arg_delimeter $command)
 | 
						|
    set -l func $prefix'_'$args[1]
 | 
						|
 | 
						|
    define_default_functions $prefix
 | 
						|
 | 
						|
    if functions -q $func
 | 
						|
        $func $args[2]
 | 
						|
    else
 | 
						|
        echo "$prefix command '$command' does not exist"
 | 
						|
        exit 1
 | 
						|
    end
 | 
						|
end
 | 
						|
 | 
						|
function define_default_functions
 | 
						|
    set -g prefix $argv[1]
 | 
						|
 | 
						|
    function "$prefix"_commands -d 'List all available commands'
 | 
						|
        set -l names (functions --names | grep $prefix'_')
 | 
						|
 | 
						|
        for name in $names
 | 
						|
            set -l details (functions -D -v $name)
 | 
						|
            set -l description $details[5]
 | 
						|
            set -l short_name (string replace "$prefix"_ '' $name)
 | 
						|
 | 
						|
            if test (string length $short_name) -ge 8
 | 
						|
                echo $short_name\t$description
 | 
						|
            else
 | 
						|
                echo $short_name\t\t$description
 | 
						|
            end
 | 
						|
        end
 | 
						|
    end
 | 
						|
 | 
						|
    function "$prefix"_help -a command -d 'Print command definition'
 | 
						|
        if test -n "$command"
 | 
						|
            functions $prefix'_'$command
 | 
						|
        else
 | 
						|
            echo 'Here are the available commands:'\n
 | 
						|
            "$prefix"_commands
 | 
						|
            echo \n"To see a command's definition and description do `run help$run_arg_delimeter{command}`"
 | 
						|
            echo "To see a subcommand group's help menu do `run {command}$run_arg_delimeter""help`"
 | 
						|
        end
 | 
						|
    end
 | 
						|
end
 | 
						|
 | 
						|
main $argv
 |