re-work to use subcommands instead of flags
1 file changed, 40 insertions(+), 43 deletions(-)
changed files
M functions/scriptura.fish → functions/scriptura.fish
@@ -7,38 +7,49 @@ end end function scriptura - argparse --stop-nonopt h/help n/new e/edit w/which c/cat -- $argv - or return + set --function valid_subcommands run help new edit which cat if not path is $SCRIPTURA_ROOT - if set --query _flag_new + if test "$argv[1]" = new mkdir $SCRIPTURA_ROOT + return 0 else - echo \$SCRIPTURA_ROOT $SCRIPTURA_ROOT does not exist, use `$(status function) --new` to create it + echo \$SCRIPTURA_ROOT $SCRIPTURA_ROOT does not exist, use `$(status function) new` to create it return 1 end end if not count $argv >/dev/null - __scriptura_usage + __scriptura_usage $valid_subcommands return 0 end set --function target $SCRIPTURA_ROOT set --function valid 0 - for i in (seq 1 (count $argv)) + set --function subcommand $argv[1] + + if contains $subcommand $valid_subcommands + set --erase argv[1] + else + echo Invalid subcommand: $subcommand >&2 + echo Expected one of: $valid_subcommands >&2 + return 1 + end + + for i in (seq (count $argv)) if test "$argv[$i]" = -- set --function valid $i break else if path is --type dir $target/$argv[$i] set --function target $target/$argv[$i] + set --function seen_dir 1 set --function valid $i - else if path is --type file $target/$argv[$i]; or set --query _flag_new + else if path is --type file $target/$argv[$i]; or test "$subcommand" = new set --function target $target/$argv[$i] set --function seen_script 1 set --function valid $i - if not set --query _flag_new + if not test "$subcommand" = new break end else@@ -46,49 +57,35 @@ break end end - if test $valid -gt 0 - set --local remaining $argv[(math $valid + 1)..-1] + set --local remaining $argv[(math $valid + 1)..-1] + + switch $subcommand + case run + if set --query --function seen_script + __scriptura_run $target $remaining + return $status + else if path is --type dir $target; + and test (count $remaining) -eq 0; + or set --query --function seen_dir - if set --query _flag_help + __scriptura_list $target + return $status + end + case help __scriptura_help $target return $status - end - - if set --query _flag_new - __scriptura_new $_flag_edit $target $remaining + case new + __scriptura_new $target $remaining return $status - end - - if set --query _flag_edit - __scriptura_edit $target + case edit + __scriptura_edit $target $remaining return $status - end - - if set --query _flag_which + case which __scriptura_which $target $remaining return $status - end - - if set --query _flag_cat - __scriptura_cat $target + case cat + __scriptura_cat $target $remaining return $status - end - - if set --query --function seen_script - if path is --perm exec $target - set --export SCRIPTURA_ROOT $SCRIPTURA_ROOT - set --export SD (path dirname $target) - $target $remaining - return $status - else - echo "scriptura: $target is not executable" >&2 - __scriptura_cat $target - return $status - end - else if path is --type dir $target; and test (count $remaining) -eq 0 - __scriptura_list $target - return $status - end end echo "scriptura: don't know what to do with target \"$argv\"" >&2