From 977c4eea6effca651f920e45aa10829ae6d8cc03 Mon Sep 17 00:00:00 2001 From: mitchel Date: Wed, 28 Oct 2020 18:33:37 -0400 Subject: [PATCH] Add importable_sw.fish and add example to swim.fish --- importable_sw.fish | 87 ++++++++++++++++++++++++++++++++++++++++++++++ swim.fish | 3 ++ 2 files changed, 90 insertions(+) create mode 100644 importable_sw.fish diff --git a/importable_sw.fish b/importable_sw.fish new file mode 100644 index 0000000..d926061 --- /dev/null +++ b/importable_sw.fish @@ -0,0 +1,87 @@ +#!/usr/bin/env fish +set -g cmd_func_prefix 'swim' + +function main + run_swim_command $cmd_func_prefix $argv +end + +function run_swim_command -a prefix + argparse --ignore-unknown 'h/help' -- $argv + set -l command $argv[2] + set -l func $prefix\_$command + set -l args $argv[3..-1] + + define_included_functions $prefix + + if functions -q $func + if test -n "$_flag_h" + if test ($func is_group) = 'is-group' >/dev/null 2>&1 + $func $args --help + else + $prefix\_help $command + end + else + $func $args + end + else if test -n "$_flag_h" + $prefix\_help + else if functions -q $prefix\_default + $prefix\_default $args + else + echo "$prefix command '$command' does not exist" + return 127 + end +end + +function define_included_functions -a prefix + set -l prefix $prefix + + function $prefix\_do -V prefix -d 'Perform a list of commands separated by commas' + for command in (string split ',' "$argv") + test -z "$command"; and continue + set -l command (string trim "$command") + + echo "Running '$command' ..." + run_swim_command $prefix (string split ' ' $command) + end + end + + function _$prefix\_commands -V prefix -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 -V prefix -d 'Print help menu or 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 do `run help {command}`" + echo "To see a subcommand group's help menu do `run {command} help`" + end + end + + if not functions -q $prefix\_default + function $prefix\_default -a arg -V prefix -d 'Alias for help command' + $prefix\_help $arg + end + end + + function $prefix\_is_group -d 'Tells executor whether this is a subcommand group' + echo 'is-group' + end +end + +main $argv diff --git a/swim.fish b/swim.fish index 4bccea6..f8d1dba 100644 --- a/swim.fish +++ b/swim.fish @@ -70,3 +70,6 @@ end function swim_fails -d 'This command fails every time' false end + +# Uncomment the following to import the execution code for swim.fish, and run it like `./swim.fish`. +# curl -fsS https://raw.githubusercontent.com/mitchell/swim.fish/master/importable_sw.fish | source