Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 157 additions & 0 deletions examples/custom-strings/download
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#!/usr/bin/env bash
# This script was generated by bashly (https://github.com/DannyBen/bashly)
# Modifying it manually is not recommended

# :command.root_command
root_command() {
# :src/root_command.sh
echo "# this file is located in 'src/root_command.sh'"
echo "# you can edit it freely and regenerate (it will not be overwritten)"
inspect_args
}

# :command.version_command
version_command() {
echo "$version"
}

# :command.usage
download_usage() {
echo -e "download - Sample minimal application with custom strings"
echo
echo -e "== Usage ==
"
echo -e " download SOURCE [options]"
echo


if [[ -n $long_usage ]]; then
echo -e "== Options ==
"
# :command.usage_fixed_flags
echo " --help, -h"
echo -e " Show this helpful help"
echo
echo " --version"
echo -e " Show version number"
echo
# :command.usage_flags
# :flag.usage
echo " --out, -o DIR (required)"
echo " Target directory"
echo
# :command.usage_args
echo -e "== Arguments ==
"

# :argument.usage
echo " SOURCE"
echo " URL to download from"
echo

fi
}


# :command.inspect_args
inspect_args() {
echo args:
for k in "${!args[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done
}

# :command.command_functions

# :command.parse_args
parse_args() {
# :command.fixed_flag_filter
case "$1" in
--version | -v )
version_command
exit 1
;;

--help | -h )
long_usage=yes
download_usage
exit 1
;;

esac
# :command.command_filter
action=root
# :command.required_args_filter
if [[ $1 && $1 != -* ]]; then
args[source]=$1
shift
else
echo -e "Boom! a required argument is missing: SOURCE\nusage: download SOURCE [options]"
exit 1
fi
# :command.required_flags_filter
argstring="$*"
if [[ "$argstring" != *--out* && "$argstring" != *-o* ]]; then
echo -e "Yo! you forgot a flag: --out, -o DIR"
exit 1
fi
# :command.parse_args_while
while [[ $# -gt 0 ]]; do
key="$1"
case "$key" in
# :flag.case
--out | -o )
if [[ $2 && $2 != -* ]]; then
args[--out]="$2"
shift
shift
else
echo -e "Hey! the flag --out requires an argument: --out, -o DIR"
exit 1
fi
;;


-* )
echo -e "invalid option: $key"
exit 1
;;

* )
# :command.parse_args_case
if [[ ! ${args[source]} ]]; then
args[source]=$1
shift
else
echo -e "invalid argument: $key"
exit 1
fi
;;

esac
done
}


# :command.initialize
initialize() {
version="0.1.0"
long_usage=''
set -e
}

# :command.run
run() {
declare -A args
parse_args "$@"

if [[ ${args[--version]} ]]; then
version_command
elif [[ ${args[--help]} ]]; then
long_usage=yes
download_usage
elif [[ $action == "root" ]]; then
root_command
fi
}

initialize
run "$@"
17 changes: 17 additions & 0 deletions examples/custom-strings/src/bashly-strings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Usage captions
usage: "== Usage ==\n"
options: "== Options ==\n"
arguments: "== Arguments ==\n"
commands: "== Commands ==\n"

# Fixed flags help text
show_this_help: Show this helpful help
show_version_number: Show version number

# Error messages
flag_requires_an_argument: "Hey! the flag %{long} requires an argument: %{usage}"
invalid_argument: "invalid argument: %{arg}"
invalid_flag: "invalid option: %{flag}"
missing_required_argument: "Boom! a required argument is missing: %{arg}\\nusage: %{usage}"
missing_required_flag: "Yo! you forgot a flag: %{usage}"

16 changes: 16 additions & 0 deletions examples/custom-strings/src/bashly.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: download
help: Sample minimal application with custom strings
version: 0.1.0

args:
- name: source
required: true
help: URL to download from

flags:
- long: --out
short: -o
arg: dir
required: true
help: Target directory

3 changes: 3 additions & 0 deletions examples/custom-strings/src/root_command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
echo "# this file is located in 'src/root_command.sh'"
echo "# you can edit it freely and regenerate (it will not be overwritten)"
inspect_args
12 changes: 12 additions & 0 deletions examples/custom-strings/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

set -x

rm -f ./src/*.sh

bashly generate

./download
./download -h
./download somesource
./download somesource -o
21 changes: 10 additions & 11 deletions examples/minimal/download
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,29 @@ version_command() {

# :command.usage
download_usage() {
echo "download - Sample minimal application without subcommands"
echo -e "download - Sample minimal application without subcommands"
echo
echo "Usage:"
echo " download SOURCE [TARGET] [options]"
echo -e "Usage:"
echo -e " download SOURCE [TARGET] [options]"
echo


if [[ -n $long_usage ]]; then
echo "Options:"
echo -e "Options:"
# :command.usage_fixed_flags
echo " --help, -h"
echo " Show this help"
echo -e " Show this help"
echo
echo " --version"
echo " Show version number"
echo -e " Show version number"
echo
# :command.usage_flags
# :flag.usage
echo " --force, -f"
echo " Overwrite existing files"
echo
# :command.usage_args
echo "Arguments:"
echo -e "Arguments:"

# :argument.usage
echo " SOURCE"
Expand Down Expand Up @@ -86,8 +86,7 @@ parse_args() {
args[source]=$1
shift
else
echo "missing required argument: SOURCE"
echo "Usage: download SOURCE [TARGET] [options]"
echo -e "missing required argument: SOURCE\nusage: download SOURCE [TARGET] [options]"
exit 1
fi
# :command.required_flags_filter
Expand All @@ -103,7 +102,7 @@ parse_args() {


-* )
echo "invalid option: $key"
echo -e "invalid option: $key"
exit 1
;;

Expand All @@ -116,7 +115,7 @@ parse_args() {
args[target]=$1
shift
else
echo "invalid argument: $key"
echo -e "invalid argument: $key"
exit 1
fi
;;
Expand Down
Loading