-
Notifications
You must be signed in to change notification settings - Fork 147
Sub sub commands #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Sub sub commands #37
Changes from all commits
dbf42ec
24c5845
d64d43a
560c636
b7b7a6a
fd5792d
fbc39c1
019c1ac
4278c6f
72b8d87
dd458b1
79a12ab
dc4ee37
e134574
254a7f7
34a4bb9
1da72da
8764cfc
2bf26a9
95a123e
13797c3
138b124
d1d4108
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,13 +6,6 @@ | |
|
|
||
| set -e | ||
|
|
||
| # Provide sub completions | ||
| if [ "$1" = "--complete" ]; then | ||
| echo --sh | ||
| echo --no-sh | ||
| exit | ||
| fi | ||
|
|
||
| if [ "$1" = "--sh" ]; then | ||
| sh=1 | ||
| shift | ||
|
|
@@ -23,19 +16,42 @@ fi | |
|
|
||
| shopt -s nullglob | ||
|
|
||
| if grep -i "^# provide sub completions" "$_SUB_COMMAND_ROOT/sub-$_SUB_COMMAND_FILE" >/dev/null; then | ||
| completions=`exec "$_SUB_COMMAND_ROOT/sub-$_SUB_COMMAND_FILE" --complete "$@"` | ||
| if [ "$completions" ]; then | ||
| echo $completions | ||
| fi | ||
| exit | ||
| fi | ||
|
|
||
| { for path in ${PATH//:/$'\n'}; do | ||
| for command in "${path}/sub-"*; do | ||
| command="${command##*sub-}" | ||
| if [ -n "$sh" ]; then | ||
| if [ ${command:0:3} = "sh-" ]; then | ||
| echo ${command##sh-} | ||
| if [ ! "$command" == "$_SUB_COMMAND_ROOT/sub-$_SUB_COMMAND_FILE" ] || [ "$_SUB_COMMAND_FILE" == "commands" ]; then | ||
| if [ -d "$_SUB_COMMAND_ROOT" ] && [ "$_SUB_COMMAND_ROOT" == "$path" ]; then | ||
| do_commands="true" | ||
| elif [ ! -d "$_SUB_COMMAND_ROOT" ] || [ "$_SUB_COMMAND_ROOT" == "$_SUB_ROOT/libexec" ]; then | ||
| do_commands="true" | ||
| else | ||
| do_commands="false" | ||
| fi | ||
| elif [ -n "$nosh" ]; then | ||
| if [ ${command:0:3} != "sh-" ]; then | ||
| elif [ "$command" == "commands" ] && [ "$command" == "$_SUB_COMMAND_FILE" ]; then | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ "$command" == "commands" ]That condition can now never be true, since $command is always going to be a path with "/sub-" in it.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very good call =] |
||
| do_commands="true" | ||
| else | ||
| do_commands="false" | ||
| fi | ||
| if [ "$do_commands" == "true" ]; then | ||
| command="${command##*sub-}" | ||
| if [ -n "$sh" ]; then | ||
| if [ ${command:0:3} = "sh-" ]; then | ||
| echo ${command##sh-} | ||
| fi | ||
| elif [ -n "$nosh" ]; then | ||
| if [ ${command:0:3} != "sh-" ]; then | ||
| echo ${command##sh-} | ||
| fi | ||
| else | ||
| echo ${command##sh-} | ||
| fi | ||
| else | ||
| echo ${command##sh-} | ||
| fi | ||
| done | ||
| done | ||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| #!/usr/bin/env bash | ||
| # Usage: sub example advanced | ||
| # Summary: List all Advanced sub Example Scripts | ||
| # Help: This command lists all of the Advanced sub Example Scripts | ||
|
|
||
| set -e | ||
|
|
||
| sub help example bash advanced |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| #!/usr/bin/env bash | ||
| # Usage: sub example advanced expert case-select | ||
| # Summary: BASH Case Select Example | ||
| # Help: This is an example of a BASH Case Select | ||
|
|
||
| set -e | ||
|
|
||
| # Provide sub completions | ||
| if [ "$1" = "--complete" ]; then | ||
| echo --name | ||
| echo --break | ||
| exit | ||
| fi | ||
|
|
||
| what="counter" | ||
| break_on=6 | ||
| while [ ! "$1" == "" ]; do | ||
| case "$1" in | ||
| --name) | ||
| if [[ ! "$2" == -* ]]; then | ||
| name=$2 | ||
| shift | ||
| else | ||
| name="default" | ||
| fi | ||
| ;; | ||
| --break) | ||
| if [[ ! "$2" == -* ]]; then | ||
| break_on=$2 | ||
| shift | ||
| else | ||
| break_on=3 | ||
| fi | ||
| ;; | ||
| esac | ||
|
|
||
| shift | ||
| done | ||
|
|
||
| echo "-=> Executing Case Select Example" | ||
| echo "----> Breaking on $break_on" | ||
| echo "----> Counter is called $name" | ||
|
|
||
| COUNTER=0 | ||
| while [ $COUNTER -lt 5 ]; do | ||
| echo "------> The $name is $COUNTER" | ||
| COUNTER=`expr $COUNTER + 1` | ||
|
|
||
| if [ "$break_on" == "$COUNTER" ]; then | ||
| break | ||
| fi | ||
| done | ||
|
|
||
| echo "- Finished..." |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| #!/usr/bin/env bash | ||
| # Usage: sub example advanced expert | ||
| # Summary: Collection of expert scripts | ||
| # Help: Collection of advanced expert sub scripts | ||
|
|
||
| sub help example advanced expert |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #!/usr/bin/env bash | ||
| # Usage: sub example advanced fix-7 | ||
| # Summary: Fixes Issue #7 | ||
| # Help: Fixes Issue #7: https://github.com/37signals/sub/issues/7 | ||
|
|
||
| set -e | ||
|
|
||
| # Provide sub completions | ||
| if [ "$1" = "--complete" ]; then | ||
| while [ ! "$1" == "" ]; do | ||
| if [ "$1" = "foo" ] || [ "$1" = "bar" ]; then | ||
| { echo baz; echo bla; } | sort | uniq | ||
| exit | ||
| fi | ||
|
|
||
| shift | ||
| done | ||
|
|
||
| { echo foo; echo bar; } | sort | uniq | ||
| exit; | ||
| fi |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| #!/usr/bin/env bash | ||
| # Usage: sub example bash basic | ||
| # Summary: List all basic sub Example Scripts | ||
| # Help: This command lists all of the basic sub Example Scripts | ||
|
|
||
| set -e | ||
|
|
||
| sub help example bash basic |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| #!/usr/bin/env bash | ||
| # Usage: sub example basic filecomplete | ||
| # Summary: Example of auto completion for files | ||
| # Help: This is an example of auto completion for files |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| #!/usr/bin/env bash | ||
| # Usage: sub example basic foreach [--break] | ||
| # Summary: BASH For Each Example w/ break loop option | ||
| # Help: This command is an example of a BASH For Each Loop with an example of how to break the loop based on an argument parameter | ||
|
|
||
| set -e | ||
|
|
||
| # Provide sub completions | ||
| if [ "$1" = "--complete" ]; then | ||
| echo --break | ||
| exit | ||
| fi | ||
|
|
||
| if [ "$1" = "--break" ] && [ ! "$2" == "" ] && [[ ! "$2" == -* ]]; then | ||
| break_on=$2 | ||
| shift | ||
| shift | ||
| elif [ "$1" = "--break" ] && [ "$2" == "" ]; then | ||
| echo "Defaulting Break On Three" | ||
| break_on=3 | ||
| shift | ||
| else | ||
| break_on=6 | ||
| fi | ||
|
|
||
| foreach_array=("one" "two" "three" "four" "five") | ||
| count=0 | ||
|
|
||
| for array_value in ${foreach_array[@]} | ||
| do | ||
| if [ "$break_on" == "$count" ]; then | ||
| break | ||
| else | ||
| echo $array_value | ||
| fi | ||
| count=`expr $count + 1` | ||
| done | ||
|
|
||
| echo "done" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| #!/usr/bin/env bash | ||
| # Usage: sub example [command] | ||
| # Summary: Collection of BASH Sub Example Scripts | ||
| # Help: These commands are mostly used as examples and for testing sub modifications | ||
|
|
||
| set -e | ||
|
|
||
| sub help example |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't fully understand what is being achieved here, perhaps add some comments explaining what's going on? Something like what are the cases where we want to show the commands and where we don't.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will def add some comments, I am going to work on this for a couple of hours now