Skip to content

Instantly share code, notes, and snippets.

@kitsuyui
Last active September 18, 2017 05:34
Show Gist options
  • Save kitsuyui/d7f3addae3a430b26f3c3a36caab2e26 to your computer and use it in GitHub Desktop.
Save kitsuyui/d7f3addae3a430b26f3c3a36caab2e26 to your computer and use it in GitHub Desktop.
備忘録: 最小限の労力でサブコマンド付きのシェルスクリプトが書きたい ref: http://qiita.com/kitsuyui/items/4b204963e0ebec53fe3c
$ some-command
Usage:
some-command --help
some-command sub-command1
some-command sub-command2
$ some-command --help
Usage:
some-command --help
some-command sub-command1
some-command sub-command2
$ some-command sub-command1
1
$ some-command sub-command2
2
$ some-command wrong-command
Usage:
some-command --help
some-command sub-command1
some-command sub-command2
$ echo $?
1
unset -f -- "${FUNCNAME[0]}"
compgen -A function \
| xargs -I % echo ' ' "$(basename "$0")" %
! declare -F -- "$1" >/dev/null && --help && exit 1
sub-command1() {
echo "$@"
}
$ some-command sub-command1 A B C
A B C
#!/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 "$@"
# 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