diff --git a/README.md b/README.md index 714193fd..9f9293d1 100644 --- a/README.md +++ b/README.md @@ -136,12 +136,14 @@ help: a sample script generated with bashly version: 0.1.0 # Specify an array of examples to show when using --help +# Each example can have multiple lines. examples: - myscript download - myscript download --force # Specify an array of environment variables needed by your script # This is used purely for displaying in the help text (when using --help) +# The help for each variable can have multiple lines. environment_variable: VARIABLE_NAME: Variable help text @@ -185,8 +187,7 @@ bash function. name: user # The message to display when using --help. -# This can have multiple lines, but it is recommended to keep lines shorter -# than ~70 characters, to avoid text wrapping in narrower terminals. +# This can have multiple lines. help: Username to use for logging in # Specify if this argument is required. @@ -216,8 +217,7 @@ long: --output short: -o # The text to display when using --help -# This can have multiple lines, but it is recommended to keep lines shorter -# than ~70 characters, to avoid text wrapping in narrower terminals. +# This can have multiple lines. help: Specify the output directory # If the flag requires an argument, specify its name here. diff --git a/Runfile b/Runfile index 365aa6ba..854da881 100644 --- a/Runfile +++ b/Runfile @@ -32,6 +32,7 @@ def examples "examples/custom-strings/download", "examples/minimal/download", "examples/subcommands/cli", + "examples/multiline/multi", "spec/fixtures/workspaces/short-flag/rush", ] end diff --git a/examples/colors/colorly b/examples/colors/colorly index 934fcb57..0b540ade 100644 --- a/examples/colors/colorly +++ b/examples/colors/colorly @@ -22,8 +22,13 @@ version_command() { # :command.usage colorly_usage() { - echo -e "colorly - Sample application that uses the color functions" - echo + if [[ -n $long_usage ]]; then + echo -e "colorly - Sample application that uses the color functions" + echo + else + echo -e "colorly - Sample application that uses the color functions" + echo + fi echo -e "Usage:" echo -e " colorly [MESSAGE] [options]" echo @@ -44,7 +49,7 @@ colorly_usage() { # :argument.usage echo " MESSAGE" - echo " Message to show [default: hello colors]" + echo -e " Message to show [default: hello colors]" echo diff --git a/examples/config-ini/configly b/examples/config-ini/configly index b5f73491..f6485469 100644 --- a/examples/config-ini/configly +++ b/examples/config-ini/configly @@ -10,8 +10,13 @@ version_command() { # :command.usage configly_usage() { - echo -e "configly - Sample application that uses the config functions" - echo + if [[ -n $long_usage ]]; then + echo -e "configly - Sample application that uses the config functions" + echo + else + echo -e "configly - Sample application that uses the config functions" + echo + fi echo -e "Usage:" echo -e " configly [command] [options]" echo @@ -41,8 +46,13 @@ configly_usage() { # :command.usage configly_set_usage() { - echo -e "configly set - Save a value in the config file" - echo + if [[ -n $long_usage ]]; then + echo -e "configly set - Save a value in the config file" + echo + else + echo -e "configly set - Save a value in the config file" + echo + fi echo -e "Usage:" echo -e " configly set KEY VALUE [options]" echo @@ -63,17 +73,18 @@ configly_set_usage() { # :argument.usage echo " KEY" - echo " Config key" + echo -e " Config key" echo # :argument.usage echo " VALUE" - echo " Config value" + echo -e " Config value" echo # :command.usage_examples echo -e "Examples:" - echo " configly set hello world" + + echo -e " configly set hello world" echo fi @@ -82,8 +93,13 @@ configly_set_usage() { # :command.usage configly_get_usage() { - echo -e "configly get - Read a value from the config file" - echo + if [[ -n $long_usage ]]; then + echo -e "configly get - Read a value from the config file" + echo + else + echo -e "configly get - Read a value from the config file" + echo + fi echo -e "Usage:" echo -e " configly get KEY [options]" echo @@ -104,12 +120,13 @@ configly_get_usage() { # :argument.usage echo " KEY" - echo " Config key" + echo -e " Config key" echo # :command.usage_examples echo -e "Examples:" - echo " configly set hello" + + echo -e " configly set hello" echo fi @@ -118,8 +135,13 @@ configly_get_usage() { # :command.usage configly_list_usage() { - echo -e "configly list - Show the entire config file" - echo + if [[ -n $long_usage ]]; then + echo -e "configly list - Show the entire config file" + echo + else + echo -e "configly list - Show the entire config file" + echo + fi echo -e "Usage:" echo -e " configly list [options]" echo diff --git a/examples/custom-includes/download b/examples/custom-includes/download index 935a2a03..1c5c7390 100644 --- a/examples/custom-includes/download +++ b/examples/custom-includes/download @@ -18,8 +18,13 @@ version_command() { # :command.usage download_usage() { - echo -e "download - Sample minimal application with custom strings" - echo + if [[ -n $long_usage ]]; then + echo -e "download - Sample minimal application with custom strings" + echo + else + echo -e "download - Sample minimal application with custom strings" + echo + fi echo -e "Usage:" echo -e " download [SOURCE] [options]" echo @@ -40,7 +45,7 @@ download_usage() { # :argument.usage echo " SOURCE" - echo " URL to download from" + echo -e " URL to download from" echo diff --git a/examples/custom-strings/download b/examples/custom-strings/download index 9ffa4a31..4e8e7ef0 100644 --- a/examples/custom-strings/download +++ b/examples/custom-strings/download @@ -17,8 +17,13 @@ version_command() { # :command.usage download_usage() { - echo -e "download - Sample minimal application with custom strings" - echo + if [[ -n $long_usage ]]; then + echo -e "download - Sample minimal application with custom strings" + echo + else + echo -e "download - Sample minimal application with custom strings" + echo + fi echo -e "== Usage == " echo -e " download SOURCE [options]" @@ -38,7 +43,7 @@ download_usage() { # :command.usage_flags # :flag.usage echo " --out, -o DIR (required)" - echo " Target directory" + echo -e " Target directory" echo # :command.usage_args echo -e "== Arguments == @@ -46,7 +51,7 @@ download_usage() { # :argument.usage echo " SOURCE" - echo " URL to download from" + echo -e " URL to download from" echo diff --git a/examples/minimal/download b/examples/minimal/download index 5a2d6cf2..e94517d4 100644 --- a/examples/minimal/download +++ b/examples/minimal/download @@ -17,8 +17,13 @@ version_command() { # :command.usage download_usage() { - echo -e "download - Sample minimal application without subcommands" - echo + if [[ -n $long_usage ]]; then + echo -e "download - Sample minimal application without subcommands" + echo + else + echo -e "download - Sample minimal application without subcommands" + echo + fi echo -e "Usage:" echo -e " download SOURCE [TARGET] [options]" echo @@ -36,25 +41,26 @@ download_usage() { # :command.usage_flags # :flag.usage echo " --force, -f" - echo " Overwrite existing files" + echo -e " Overwrite existing files" echo # :command.usage_args echo -e "Arguments:" # :argument.usage echo " SOURCE" - echo " URL to download from" + echo -e " URL to download from" echo # :argument.usage echo " TARGET" - echo " Target filename (default: same as source)" + echo -e " Target filename (default: same as source)" echo # :command.usage_examples echo -e "Examples:" - echo " download example.com" - echo " download example.com ./output -f" + + echo -e " download example.com" + echo -e " download example.com ./output -f" echo fi diff --git a/examples/multiline/README.md b/examples/multiline/README.md new file mode 100644 index 00000000..159f3d72 --- /dev/null +++ b/examples/multiline/README.md @@ -0,0 +1,6 @@ +Multiline Example +================================================== + +This example was generated manually to demonstrate multiline usage text. + + $ bashly generate diff --git a/examples/multiline/multi b/examples/multiline/multi new file mode 100644 index 00000000..b496adb3 --- /dev/null +++ b/examples/multiline/multi @@ -0,0 +1,373 @@ +#!/usr/bin/env bash +# This script was generated by bashly (https://github.com/DannyBen/bashly) +# Modifying it manually is not recommended + + +# :command.version_command +version_command() { + echo "$version" +} + +# :command.usage +multi_usage() { + if [[ -n $long_usage ]]; then + echo -e "multi" + echo + echo -e " Multiline test\n \n This is a multiline test example. Long lines are going to be properly wrapped\n at the 80 character mark." + echo + else + echo -e "multi - Multiline test" + echo + fi + echo -e "Usage:" + echo -e " multi [command] [options]" + echo + # :command.usage_commands + echo -e "Commands:" + echo " multiline Do some multiline work." + echo " regular Standard help line" + echo + + if [[ -n $long_usage ]]; then + echo -e "Options:" + # :command.usage_fixed_flags + echo " --help, -h" + echo -e " Show this help" + echo + echo " --version" + echo -e " Show version number" + echo + + + # :command.usage_environment_variables + echo -e "Environment Variables:" + echo " MULTI_VITAMIN" + echo -e " Environment variables can also use multiple lines. This can be useful if we\n need to explain - in excruciating detail - what the environemnt variable is\n used for." + echo + # :command.usage_examples + echo -e "Examples:" + + echo -e " Examples can also use multiple lines. This can be useful if we need to explain\n the example, for instance like we are doing here:\n \n multi multiline --flag" + echo + + fi +} + +# :command.usage +multi_multiline_usage() { + if [[ -n $long_usage ]]; then + echo -e "multi multiline" + echo + echo -e " Do some multiline work.\n \n This is a long process and takes time, therefore it takes a lot of lines to\n describe and this line is particularly long.\n \n But - not all hope is lost. Bashly should respect newlines and word-wrap long\n lines properly at the 80 character mark." + echo + else + echo -e "multi multiline - Do some multiline work." + echo + fi + echo -e "Usage:" + echo -e " multi multiline [MY_ARG] [options]" + echo + + + if [[ -n $long_usage ]]; then + echo -e "Options:" + # :command.usage_fixed_flags + echo " --help, -h" + echo -e " Show this help" + echo + echo " --version" + echo -e " Show version number" + echo + # :command.usage_flags + # :flag.usage + echo " --long" + echo -e " Long flag with long text.\n \n Finally, this will be the last multiline string we type (hopefully), but\n once we are done with this, we will be done with all." + echo + # :command.usage_args + echo -e "Arguments:" + + # :argument.usage + echo " MY_ARG" + echo -e " Argument with multiline.\n \n Once again we gather here to prepare some long lines that will most likely\n seem silly to anyone who reads them.\n \n But, it is just needed for testing - otherwise, it will not only be silly,\n but it will also be buggy!" + echo + + + + fi +} + + +# :command.usage +multi_regular_usage() { + if [[ -n $long_usage ]]; then + echo -e "multi regular - Standard help line" + echo + else + echo -e "multi regular - Standard help line" + echo + fi + echo -e "Usage:" + echo -e " multi regular [REPO] [options]" + echo + + + if [[ -n $long_usage ]]; then + echo -e "Options:" + # :command.usage_fixed_flags + echo " --help, -h" + echo -e " Show this help" + echo + echo " --version" + echo -e " Show version number" + echo + + # :command.usage_args + echo -e "Arguments:" + + # :argument.usage + echo " REPO" + echo -e " Repository name" + echo + + + + fi +} + + + +# :command.inspect_args +inspect_args() { + echo args: + for k in "${!args[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done +} + + +# :command.command_functions +# :command.function +multi_multiline_command() { + # :src/multi_multiline_command.sh + echo "# this file is located in 'src/multi_multiline_command.sh'" + echo "# code for 'multi multiline' goes here" + echo "# you can edit it freely and regenerate (it will not be overwritten)" + inspect_args +} + +# :command.function +multi_regular_command() { + # :src/multi_regular_command.sh + echo "# this file is located in 'src/multi_regular_command.sh'" + echo "# code for 'multi regular' goes here" + echo "# you can edit it freely and regenerate (it will not be overwritten)" + inspect_args +} + + +# :command.parse_args +parse_args() { + # :command.fixed_flag_filter + case "$1" in + --version | -v ) + version_command + exit 1 + ;; + + --help | -h ) + long_usage=yes + multi_usage + exit 1 + ;; + + esac + # :command.command_filter + action=$1 + + case $action in + -* ) + ;; + + multiline ) + action="multiline" + shift + multiline_parse_args "$@" + shift $# + ;; + + regular ) + action="regular" + shift + regular_parse_args "$@" + shift $# + ;; + + * ) + multi_usage + exit 1 + ;; + + esac + # :command.required_args_filter + # :command.required_flags_filter + # :command.parse_args_while + while [[ $# -gt 0 ]]; do + key="$1" + case "$key" in + + -* ) + echo -e "invalid option: $key" + exit 1 + ;; + + * ) + # :command.parse_args_case + echo -e "invalid argument: $key" + exit 1 + ;; + + esac + done +} + + +# :command.parse_args_secondary +multiline_parse_args() { + # :command.fixed_flag_filter + case "$1" in + --version | -v ) + version_command + exit 1 + ;; + + --help | -h ) + long_usage=yes + multi_multiline_usage + exit 1 + ;; + + esac + # :command.required_args_filter + # :command.required_flags_filter + # :command.parse_args_while + while [[ $# -gt 0 ]]; do + key="$1" + case "$key" in + # :flag.case + --long ) + args[--long]=1 + shift + ;; + + + -* ) + echo -e "invalid option: $key" + exit 1 + ;; + + * ) + # :command.parse_args_case + if [[ ! ${args[my_arg]} ]]; then + args[my_arg]=$1 + shift + else + echo -e "invalid argument: $key" + exit 1 + fi + ;; + + esac + done +} + + +# :command.parse_args_secondary +regular_parse_args() { + # :command.fixed_flag_filter + case "$1" in + --version | -v ) + version_command + exit 1 + ;; + + --help | -h ) + long_usage=yes + multi_regular_usage + exit 1 + ;; + + esac + # :command.required_args_filter + # :command.required_flags_filter + # :command.parse_args_while + while [[ $# -gt 0 ]]; do + key="$1" + case "$key" in + + -* ) + echo -e "invalid option: $key" + exit 1 + ;; + + * ) + # :command.parse_args_case + if [[ ! ${args[repo]} ]]; then + args[repo]=$1 + shift + else + echo -e "invalid argument: $key" + exit 1 + fi + ;; + + esac + done +} + + +# :command.initialize +initialize() { + version="0.1.0" + long_usage='' + set -e + + # :src/initialize.sh + # Code here runs inside the initialize() function + # Use it for anything that you need to run before any other function, like + # setting environment vairables: + # CONFIG_FILE=settings.ini + # + # Feel free to empty (but not delete) this file. +} + +# :command.run +run() { + declare -A args + parse_args "$@" + + if [[ $action == "multiline" ]]; then + if [[ ${args[--help]} ]]; then + long_usage=yes + multi_multiline_usage + else + multi_multiline_command + fi + + elif [[ $action == "regular" ]]; then + if [[ ${args[--help]} ]]; then + long_usage=yes + multi_regular_usage + else + multi_regular_command + fi + + elif [[ ${args[--version]} ]]; then + version_command + elif [[ ${args[--help]} ]]; then + long_usage=yes + multi_usage + elif [[ $action == "root" ]]; then + root_command + fi +} + +initialize +run "$@" diff --git a/examples/multiline/src/bashly.yml b/examples/multiline/src/bashly.yml new file mode 100644 index 00000000..3d97e7e8 --- /dev/null +++ b/examples/multiline/src/bashly.yml @@ -0,0 +1,49 @@ +name: multi +help: |- + Multiline test + + This is a multiline test example. Long lines are going to be properly wrapped at the 80 character mark. + +version: 0.1.0 + +commands: +- name: multiline + help: |- + Do some multiline work. + + This is a long process and takes time, therefore it takes a lot of lines to describe and this line is particularly long. + + But - not all hope is lost. Bashly should respect newlines and word-wrap long lines properly at the 80 character mark. + + args: + - name: my_arg + help: |- + Argument with multiline. + + Once again we gather here to prepare some long lines that will most likely seem silly to anyone who reads them. + + But, it is just needed for testing - otherwise, it will not only be silly, but it will also be buggy! + + flags: + - long: --long + help: |- + Long flag with long text. + + Finally, this will be the last multiline string we type (hopefully), but once we are done with this, we will be done with all. + +- name: regular + help: Standard help line + + args: + - name: repo + help: Repository name + +examples: + - |- + Examples can also use multiple lines. This can be useful if we need to explain the example, for instance like we are doing here: + + multi multiline --flag + +environment_variables: + MULTI_VITAMIN: |- + Environment variables can also use multiple lines. This can be useful if we need to explain - in excruciating detail - what the environemnt variable is used for. diff --git a/examples/multiline/src/initialize.sh b/examples/multiline/src/initialize.sh new file mode 100644 index 00000000..f2dbc52c --- /dev/null +++ b/examples/multiline/src/initialize.sh @@ -0,0 +1,6 @@ +# Code here runs inside the initialize() function +# Use it for anything that you need to run before any other function, like +# setting environment vairables: +# CONFIG_FILE=settings.ini +# +# Feel free to empty (but not delete) this file. diff --git a/examples/multiline/src/multi_multiline_command.sh b/examples/multiline/src/multi_multiline_command.sh new file mode 100644 index 00000000..162d3a6d --- /dev/null +++ b/examples/multiline/src/multi_multiline_command.sh @@ -0,0 +1,4 @@ +echo "# this file is located in 'src/multi_multiline_command.sh'" +echo "# code for 'multi multiline' goes here" +echo "# you can edit it freely and regenerate (it will not be overwritten)" +inspect_args diff --git a/examples/multiline/src/multi_regular_command.sh b/examples/multiline/src/multi_regular_command.sh new file mode 100644 index 00000000..dd6c3632 --- /dev/null +++ b/examples/multiline/src/multi_regular_command.sh @@ -0,0 +1,4 @@ +echo "# this file is located in 'src/multi_regular_command.sh'" +echo "# code for 'multi regular' goes here" +echo "# you can edit it freely and regenerate (it will not be overwritten)" +inspect_args diff --git a/examples/multiline/test.sh b/examples/multiline/test.sh new file mode 100644 index 00000000..048d5870 --- /dev/null +++ b/examples/multiline/test.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +rm -f ./src/*.sh + +set -x + +bashly generate + +./multi +./multi -h +./multi multiline +./multi multiline -h diff --git a/examples/subcommands/cli b/examples/subcommands/cli index 708d1d59..9d91c346 100644 --- a/examples/subcommands/cli +++ b/examples/subcommands/cli @@ -10,8 +10,13 @@ version_command() { # :command.usage cli_usage() { - echo -e "cli - Sample application" - echo + if [[ -n $long_usage ]]; then + echo -e "cli - Sample application" + echo + else + echo -e "cli - Sample application" + echo + fi echo -e "Usage:" echo -e " cli [command] [options]" echo @@ -35,7 +40,7 @@ cli_usage() { # :command.usage_environment_variables echo -e "Environment Variables:" echo " API_KEY" - echo " Set your API key" + echo -e " Set your API key" echo @@ -44,8 +49,13 @@ cli_usage() { # :command.usage cli_download_usage() { - echo -e "cli download - Download a file" - echo + if [[ -n $long_usage ]]; then + echo -e "cli download - Download a file" + echo + else + echo -e "cli download - Download a file" + echo + fi echo -e "Usage:" echo -e " cli download SOURCE [TARGET] [options]" echo @@ -63,29 +73,30 @@ cli_download_usage() { # :command.usage_flags # :flag.usage echo " --force, -f" - echo " Overwrite existing files" + echo -e " Overwrite existing files" echo # :command.usage_args echo -e "Arguments:" # :argument.usage echo " SOURCE" - echo " URL to download from" + echo -e " URL to download from" echo # :argument.usage echo " TARGET" - echo " Target filename (default: same as source)" + echo -e " Target filename (default: same as source)" echo # :command.usage_environment_variables echo -e "Environment Variables:" echo " DEFAULT_TARGET_LOCATION" - echo " Set the default location to download to" + echo -e " Set the default location to download to" echo # :command.usage_examples echo -e "Examples:" - echo " cli download example.com" - echo " cli download example.com ./output -f" + + echo -e " cli download example.com" + echo -e " cli download example.com ./output -f" echo fi @@ -94,8 +105,13 @@ cli_download_usage() { # :command.usage cli_upload_usage() { - echo -e "cli upload - Upload a file" - echo + if [[ -n $long_usage ]]; then + echo -e "cli upload - Upload a file" + echo + else + echo -e "cli upload - Upload a file" + echo + fi echo -e "Usage:" echo -e " cli upload SOURCE [options]" echo @@ -113,19 +129,19 @@ cli_upload_usage() { # :command.usage_flags # :flag.usage echo " --user, -u USER (required)" - echo " Username to use for logging in" + echo -e " Username to use for logging in" echo # :flag.usage echo " --password, -p PASSWORD" - echo " Password to use for logging in" + echo -e " Password to use for logging in" echo # :command.usage_args echo -e "Arguments:" # :argument.usage echo " SOURCE" - echo " File to upload" + echo -e " File to upload" echo diff --git a/lib/bashly/concerns/renderable.rb b/lib/bashly/concerns/renderable.rb index d0be7243..9a56cb78 100644 --- a/lib/bashly/concerns/renderable.rb +++ b/lib/bashly/concerns/renderable.rb @@ -1,3 +1,5 @@ +require 'erb' + module Bashly module Renderable def render(view) diff --git a/lib/bashly/extensions/string.rb b/lib/bashly/extensions/string.rb index b7449438..384eec63 100644 --- a/lib/bashly/extensions/string.rb +++ b/lib/bashly/extensions/string.rb @@ -1,20 +1,27 @@ -require 'erb' -require 'ostruct' - class String + def escape_newlines + gsub "\n", "\\n" + end + def indent(offset) return self unless offset > 0 split("\n").indent(offset).join("\n") end - def to_underscore! - gsub!(/(.)([A-Z])/,'\1_\2') - gsub!(' ', '_') - downcase! + def to_underscore + gsub(/(.)([A-Z])/,'\1_\2').gsub(' ', '_').downcase end - def to_underscore - dup.tap { |s| s.to_underscore! } + def wrap(length = 80) + strip! + split("\n").collect! do |line| + if line.length > length + line.gsub!(/([^\s]{#{length}})([^\s$])/, "\\1 \\2") + line.gsub(/(.{1,#{length}})(\s+|$)/, "\\1\n").rstrip + else + line + end + end * "\n" end end diff --git a/lib/bashly/views/argument/usage.erb b/lib/bashly/views/argument/usage.erb index bab4588a..6f8b8c02 100644 --- a/lib/bashly/views/argument/usage.erb +++ b/lib/bashly/views/argument/usage.erb @@ -1,4 +1,4 @@ # :argument.usage echo " <%= name.upcase %>" -echo " <%= summary %>" +echo -e "<%= help.wrap(76).indent(4).escape_newlines %>" echo diff --git a/lib/bashly/views/command/usage.erb b/lib/bashly/views/command/usage.erb index b376c14c..be8380b8 100644 --- a/lib/bashly/views/command/usage.erb +++ b/lib/bashly/views/command/usage.erb @@ -1,7 +1,19 @@ # :command.usage <%= full_name.to_underscore %>_usage() { - echo -e "<%= caption_string %>" - echo + if [[ -n $long_usage ]]; then + <%- if summary == help -%> + echo -e "<%= caption_string %>" + echo + <%- else -%> + echo -e "<%= full_name %>" + echo + echo -e "<%= help.wrap(78).indent(2).escape_newlines %>" + echo + <%- end -%> + else + echo -e "<%= caption_string %>" + echo + fi echo -e "<%= strings[:usage] %>" echo -e " <%= usage_string %>" echo diff --git a/lib/bashly/views/command/usage_environment_variables.erb b/lib/bashly/views/command/usage_environment_variables.erb index 150ef85a..0596b1cc 100644 --- a/lib/bashly/views/command/usage_environment_variables.erb +++ b/lib/bashly/views/command/usage_environment_variables.erb @@ -2,6 +2,6 @@ echo -e "<%= strings[:environment_variables] %>" <%- environment_variables.each do |name, description| -%> echo " <%= name %>" -echo " <%= description %>" +echo -e "<%= description.wrap(76).indent(4).escape_newlines %>" <%- end -%> echo diff --git a/lib/bashly/views/command/usage_examples.erb b/lib/bashly/views/command/usage_examples.erb index 283f3f02..6f16984e 100644 --- a/lib/bashly/views/command/usage_examples.erb +++ b/lib/bashly/views/command/usage_examples.erb @@ -1,6 +1,7 @@ # :command.usage_examples echo -e "<%= strings[:examples] %>" + <%- examples.each do |example| -%> -echo " <%= example %>" +echo -e "<%= example.wrap(78).indent(2).escape_newlines %>" <%- end -%> echo diff --git a/lib/bashly/views/flag/usage.erb b/lib/bashly/views/flag/usage.erb index eabba7c2..5bd1e5e8 100644 --- a/lib/bashly/views/flag/usage.erb +++ b/lib/bashly/views/flag/usage.erb @@ -1,4 +1,4 @@ # :flag.usage echo " <%= usage_string extended: true %>" -echo " <%= summary %>" +echo -e "<%= help.wrap(76).indent(4).escape_newlines %>" echo diff --git a/spec/approvals/examples/multiline b/spec/approvals/examples/multiline new file mode 100644 index 00000000..ca80e466 --- /dev/null +++ b/spec/approvals/examples/multiline @@ -0,0 +1,93 @@ ++ bashly generate +creating user files in src +created src/initialize.sh +created src/multi_multiline_command.sh +created src/multi_regular_command.sh +created ./multi +run ./multi --help to test your bash script ++ ./multi +multi - Multiline test + +Usage: + multi [command] [options] + +Commands: + multiline Do some multiline work. + regular Standard help line + ++ ./multi -h +multi + + Multiline test + + This is a multiline test example. Long lines are going to be properly wrapped + at the 80 character mark. + +Usage: + multi [command] [options] + +Commands: + multiline Do some multiline work. + regular Standard help line + +Options: + --help, -h + Show this help + + --version + Show version number + +Environment Variables: + MULTI_VITAMIN + Environment variables can also use multiple lines. This can be useful if we + need to explain - in excruciating detail - what the environemnt variable is + used for. + +Examples: + Examples can also use multiple lines. This can be useful if we need to explain + the example, for instance like we are doing here: + + multi multiline --flag + ++ ./multi multiline +# this file is located in 'src/multi_multiline_command.sh' +# code for 'multi multiline' goes here +# you can edit it freely and regenerate (it will not be overwritten) +args: ++ ./multi multiline -h +multi multiline + + Do some multiline work. + + This is a long process and takes time, therefore it takes a lot of lines to + describe and this line is particularly long. + + But - not all hope is lost. Bashly should respect newlines and word-wrap long + lines properly at the 80 character mark. + +Usage: + multi multiline [MY_ARG] [options] + +Options: + --help, -h + Show this help + + --version + Show version number + + --long + Long flag with long text. + + Finally, this will be the last multiline string we type (hopefully), but + once we are done with this, we will be done with all. + +Arguments: + MY_ARG + Argument with multiline. + + Once again we gather here to prepare some long lines that will most likely + seem silly to anyone who reads them. + + But, it is just needed for testing - otherwise, it will not only be silly, + but it will also be buggy! + diff --git a/spec/bashly/extensions/string_spec.rb b/spec/bashly/extensions/string_spec.rb index 1066245d..3e0221eb 100644 --- a/spec/bashly/extensions/string_spec.rb +++ b/spec/bashly/extensions/string_spec.rb @@ -1,6 +1,13 @@ require 'spec_helper' describe Array do + describe '#escape_newlines' do + subject { "hello\nworld" } + it "escapes newlines" do + expect(subject.escape_newlines).to eq "hello\\nworld" + end + end + describe '#indent' do subject { "hello" } it "prepends the string with spaces" do @@ -8,26 +15,14 @@ end end - describe '#to_underscore!' do - context "with CamelCased argument" do - subject { "NotCamelCase" } - - it "mutates it to snake_case" do - subject.to_underscore! - expect(subject).to eq "not_camel_case" - end - end - - context "with any string with spaces" do - subject { "in any case" } - - it "mutates it to snake_case" do - subject.to_underscore! - expect(subject).to eq "in_any_case" - end + describe '#wrap' do + subject { "a long line of text that is going to be wrapped, fingers crossed!" } + it "wraps the string to the specified length" do + expect(subject.wrap 30).to eq "a long line of text that is\ngoing to be wrapped, fingers\ncrossed!" end end + describe '#to_underscore' do subject { "Some String" }