all repos — scriptura @ 5eb7c15d14f3eb6b95b4427e818459d027d2f0d1

a corner in $HOME for your scripts

allow users to write their own subcommands

Alin
commit

5eb7c15d14f3eb6b95b4427e818459d027d2f0d1

parent

e9bf47b59387168b9370ec60e84c4c3d0aeebc49

M README.mdREADME.md
@@ -12,6 +12,7 @@ - Uses subcommands instead of flags (`scriptura new` vs `sd -new`)
- `--` can be used to separate arguments intended for `scriptura` itself from those intended for the executed command - [Choose your own short alias](#shorter-command-name) - A new `cmd` subcommand for running external commands on scripts (e.g. `scriptura cmd foobar -- rm`) +- [Add your own subcommands](#custom-subcommands) by placing them under `$SCRIPTURA_ROOT/scriptura` ## Motivation
@@ -110,6 +111,30 @@ For directories, the content of a file called `help` will be printed, if it exists. `scriptura help nix` would print the contents of `~/sd/nix/help`.
Given a script at `~/sd/foo/bar`, `scriptura help foo bar` will print the contents of a corresponding file with the `.help` extension, in this case `~/sd/foo/bar.help`. If such a file does not exist, any comments in the script (excluding shebang) are printed instead. + +#### Custom subcommands + +You can add your own subcommands by placing them under `$SCRIPTURA_ROOT/scriptura`. Adding a tree subcommand would look like this: + +```fish +scriptura new scriptura tree -- tree +``` + +Then you can run the new command like so (note: no `run` subcommand) + +```fish +scriptura tree +``` + +``` +. +└── scriptura + └── tree + +2 directories, 1 file +``` + +Flags and arguments can be passed to the subcommand by adding them after the subcommand name: `scriptura tree -L1 foo` ## Configuration
M completions/scriptura.fishcompletions/scriptura.fish
@@ -49,18 +49,23 @@ --description (__scriptura_describe_function __scriptura_cmd) \
--arguments cmd complete --command scriptura \ - --condition "__fish_seen_subcommand_from $subcommands new (__scriptura_subcommands)" \ + --condition __fish_use_subcommand \ + --exclusive \ + --arguments '(__scriptura_complete_script scriptura)' + +complete --command scriptura \ + --condition "__fish_seen_subcommand_from $subcommands new (__scriptura_subcommands) (__scriptura_subcommands scriptura)" \ --condition 'not __scriptura_seen_script' \ --arguments '(__scriptura_complete_script)' complete --command scriptura \ - --condition "__fish_seen_subcommand_from $subcommands new (__scriptura_subcommands)" \ + --condition "__fish_seen_subcommand_from $subcommands new (__scriptura_subcommands) (__scriptura_subcommands scriptura)" \ --condition __scriptura_seen_script \ --condition 'not contains -- "--" (commandline -xpc)' \ --arguments -- complete --command scriptura \ - --condition "__fish_seen_subcommand_from $subcommands (__scriptura_subcommands)" \ + --condition "__fish_seen_subcommand_from $subcommands (__scriptura_subcommands) (__scriptura_subcommands scriptura)" \ --condition __scriptura_seen_script \ --condition 'contains -- "--" (commandline -xpc)' \ --force-files
M functions/__scriptura_subcommands.fishfunctions/__scriptura_subcommands.fish
@@ -1,3 +1,10 @@
function __scriptura_subcommands --description 'List available subcommands' - path basename $(path filter --type file,dir --perm exec $SCRIPTURA_ROOT/*) + set --local from $SCRIPTURA_ROOT + for arg in $argv + if path is --type dir $from/$arg + set --function from $from/$arg + end + end + + path basename $(path filter --type file,dir --perm exec $from/*) end
M functions/scriptura.fishfunctions/scriptura.fish
@@ -33,6 +33,15 @@ echo ""
for subcommand in $valid_subcommands printf ' %-12s %s\n' $subcommand (__scriptura_describe_function __scriptura_$subcommand) end + set --local user_subcommands (__scriptura_subcommands scriptura) + if count $user_subcommands >/dev/null + echo "" + echo "User Subcommands:" + echo "" + for subcommand in $user_subcommands + printf ' %-12s %s\n' $subcommand (__scriptura_desc $SCRIPTURA_ROOT/scriptura/$subcommand) + end + end echo "" echo "Scripts:" echo ""
@@ -47,6 +56,9 @@
set --function subcommand $argv[1] if contains $subcommand $valid_subcommands + set --erase argv[1] + else if contains $subcommand (__scriptura_subcommands scriptura) + set --function cmd $subcommand set --erase argv[1] else echo Invalid subcommand: $subcommand >&2
@@ -105,6 +117,9 @@ __scriptura_cat $target $remaining
return $status case cmd __scriptura_cmd $target $remaining + return $status + case (__scriptura_subcommands scriptura) + __scriptura_cmd $target -- $cmd $remaining return $status end