Skip to content

Commit

Permalink
add --really
Browse files Browse the repository at this point in the history
  • Loading branch information
ianthehenry committed Dec 1, 2021
1 parent 161c908 commit f50a371
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,34 @@ But there are some special flags that are significant to `sd`. If you supply any
$ sd foo bar --edit
$ sd foo bar --cat
$ sd foo bar --which
$ sd foo bar --really

## `--really`

Suppress special handling of any of the other special flags. Allows you to pass `--help` as an argument to your actual script, instead of being interpreted by `sd`.

$ sd foo bar --help --really

Will invoke:

~/sd/foo/bar --help

The first occurrence of the `--really` argument will be removed from the arguments passed to the script, so if you need to pass a literal `--really`, you must pass it twice to `sd`. For example:

$ sd foo bar --really --help --really

Will invoke:

$ ~/sd/foo/bar --help --really

And:


$ sd foo bar --really --really --help

Will invoke:

$ ~/sd/foo/bar --really --help

## `--help`

Expand Down Expand Up @@ -186,6 +214,9 @@ There are no *releases* of `sd`, per se, but I have occasionally made changes.

## 2021-11-30

- added `--really`
- `dir.help` files are now `dir/help` files

You used to be able to provide a description for a directory called `foo/` by writing a file called `foo.help` as a sibling of that directory.

Now directory help summaries are expected in `foo/help` instead.
Expand Down
24 changes: 19 additions & 5 deletions sd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ __sd_join_path() {
}

__sd_which() {
__sd_join_path "$@"
echo "$1"
}

__sd_cat() {
Expand Down Expand Up @@ -67,16 +67,17 @@ __sd_directory_help() {
helps+=("$help")
done

if [[ "${#commands[@]}" -eq 0 ]]; then
if [[ ${#commands[@]} -eq 0 ]]; then
echo "(no subcommands found)"
else
local max_length=0
local length
for command in "${commands[@]}"; do
length="${#command}"
max_length=$(( length > max_length ? length : max_length))
length=${#command}
max_length=$((length > max_length ? length : max_length))
done

# zsh doesn't support ${!commands[@]} expansion
for ((i = 0; i < ${#commands[@]}; i++)); do
printf "%-${max_length}s -- %s\n" "${commands[i]}" "${helps[i]}"
done
Expand Down Expand Up @@ -182,6 +183,7 @@ sd() (
local found_edit="false"
local found_cat="false"
local found_which="false"
local found_really="false"

for arg in "$@"; do
case "$arg" in
Expand All @@ -190,10 +192,22 @@ sd() (
--edit) found_edit=true ;;
--cat) found_cat=true ;;
--which) found_which=true ;;
--really) found_really=true ;;
esac
done

if [[ "$found_help" = "true" ]]; then
if [[ "$found_really" = "true" ]]; then
local -a preserved=()
for arg in "$@"; do
case "$arg" in
'--really') shift; break ;;
*) preserved+=("$arg"); shift ;;
esac
done
if [[ ${#preserved[@]} -gt 0 ]]; then
set -- "${preserved[@]}" "$@"
fi
elif [[ "$found_help" = "true" ]]; then
__sd_help "$target"
exit 0
elif [[ "$found_new" = "true" ]]; then
Expand Down

0 comments on commit f50a371

Please sign in to comment.