Last active
September 18, 2017 05:34
-
-
Save kitsuyui/d7f3addae3a430b26f3c3a36caab2e26 to your computer and use it in GitHub Desktop.
備忘録: 最小限の労力でサブコマンド付きのシェルスクリプトが書きたい ref: http://qiita.com/kitsuyui/items/4b204963e0ebec53fe3c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ some-command | |
Usage: | |
some-command --help | |
some-command sub-command1 | |
some-command sub-command2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ some-command --help | |
Usage: | |
some-command --help | |
some-command sub-command1 | |
some-command sub-command2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ some-command sub-command1 | |
1 | |
$ some-command sub-command2 | |
2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ some-command wrong-command | |
Usage: | |
some-command --help | |
some-command sub-command1 | |
some-command sub-command2 | |
$ echo $? | |
1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unset -f -- "${FUNCNAME[0]}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
compgen -A function \ | |
| xargs -I % echo ' ' "$(basename "$0")" % |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
! declare -F -- "$1" >/dev/null && --help && exit 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub-command1() { | |
echo "$@" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ some-command sub-command1 A B C | |
A B C |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
sub-command1() { | |
echo 1 | |
} | |
sub-command2() { | |
echo 2 | |
} | |
--help() { | |
echo 'Usage:' | |
compgen -A function \ | |
| xargs -I % echo ' ' "$(basename "$0")" % | |
} | |
main() { | |
unset -f -- "${FUNCNAME[0]}" | |
! declare -F -- "$1" >/dev/null && --help && exit 1 | |
"$@" | |
} | |
main "$@" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://qiita.com/kitsuyui/items/4b204963e0ebec53fe3c | |
--help() { | |
echo 'Usage:' | |
compgen -A function \ | |
| xargs -I % echo ' ' "$(basename "$0")" % | |
} | |
main() { | |
unset -f -- "${FUNCNAME[0]}" | |
! declare -F -- "$1" >/dev/null && --help && exit 1 | |
"$@" | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment