From 5377582c3e224187c43eb92901184fd9f4bd7826 Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Fri, 13 Dec 2019 15:01:00 +0000 Subject: [PATCH] show shortcut in command usage --- examples/colors/colorly | 1 + examples/config-ini/configly | 13 +++++++++++++ examples/custom-includes/download | 1 + examples/custom-strings/download | 1 + examples/default-command/ftp | 9 +++++++++ examples/dependencies/cli | 3 +++ examples/docker-like/docker | 15 +++++++++++++++ examples/environment-variables/cli | 5 +++++ examples/git-like/git | 9 +++++++++ examples/minimal/download | 1 + examples/multiline/multi | 3 +++ examples/subcommands/cli | 9 +++++++++ examples/yaml/yaml | 1 + lib/bashly/models/environment_variable.rb | 2 +- lib/bashly/models/flag.rb | 2 +- lib/bashly/templates/strings.yml | 10 ++++++++++ lib/bashly/views/command/usage.erb | 10 ++++++++-- lib/bashly/views/command/usage_commands.erb | 2 +- spec/approvals/examples/docker-like | 6 ++++++ spec/approvals/examples/environment-variables | 2 ++ spec/approvals/examples/git-like | 4 ++++ spec/approvals/examples/subcommands | 4 ++++ 22 files changed, 108 insertions(+), 5 deletions(-) diff --git a/examples/colors/colorly b/examples/colors/colorly index b388e97c..e9c721db 100644 --- a/examples/colors/colorly +++ b/examples/colors/colorly @@ -29,6 +29,7 @@ colorly_usage() { printf "colorly - Sample application that uses the color functions\n" echo fi + printf "Usage:\n" printf " colorly [MESSAGE] [options]\n" printf " colorly --help | -h\n" diff --git a/examples/config-ini/configly b/examples/config-ini/configly index 3f1e71a6..17e70988 100644 --- a/examples/config-ini/configly +++ b/examples/config-ini/configly @@ -16,6 +16,7 @@ configly_usage() { printf "configly - Sample application that uses the config functions\n" echo fi + printf "Usage:\n" printf " configly [command] [options]\n" printf " configly [command] --help | -h\n" @@ -50,6 +51,10 @@ configly_set_usage() { printf "configly set - Save a value in the config file\n" echo fi + + printf "Shortcut: s\n" + echo + printf "Usage:\n" printf " configly set KEY VALUE [options]\n" printf " configly set --help | -h\n" @@ -93,6 +98,10 @@ configly_get_usage() { printf "configly get - Read a value from the config file\n" echo fi + + printf "Shortcut: g\n" + echo + printf "Usage:\n" printf " configly get KEY [options]\n" printf " configly get --help | -h\n" @@ -131,6 +140,10 @@ configly_list_usage() { printf "configly list - Show the entire config file\n" echo fi + + printf "Shortcut: l\n" + echo + printf "Usage:\n" printf " configly list [options]\n" printf " configly list --help | -h\n" diff --git a/examples/custom-includes/download b/examples/custom-includes/download index 165ee398..53edde0c 100644 --- a/examples/custom-includes/download +++ b/examples/custom-includes/download @@ -25,6 +25,7 @@ download_usage() { printf "download - Sample minimal application with custom strings\n" echo fi + printf "Usage:\n" printf " download [SOURCE] [options]\n" printf " download --help | -h\n" diff --git a/examples/custom-strings/download b/examples/custom-strings/download index 75ef9790..fbc6b79c 100644 --- a/examples/custom-strings/download +++ b/examples/custom-strings/download @@ -24,6 +24,7 @@ download_usage() { printf "download - Sample minimal application with custom strings\n" echo fi + printf "== Usage ==\n\n" printf " download SOURCE [options]\n" printf " download --help | -h\n" diff --git a/examples/default-command/ftp b/examples/default-command/ftp index 88df4e07..b7773656 100644 --- a/examples/default-command/ftp +++ b/examples/default-command/ftp @@ -16,6 +16,7 @@ ftp_usage() { printf "ftp - Sample application that uses the default command option\n" echo fi + printf "Usage:\n" printf " ftp [command] [options]\n" printf " ftp [command] --help | -h\n" @@ -49,6 +50,10 @@ ftp_upload_usage() { printf "ftp upload - Upload a file\n" echo fi + + printf "Shortcut: u\n" + echo + printf "Usage:\n" printf " ftp upload SOURCE [options]\n" printf " ftp upload --help | -h\n" @@ -81,6 +86,10 @@ ftp_download_usage() { printf "ftp download - Download a file\n" echo fi + + printf "Shortcut: d\n" + echo + printf "Usage:\n" printf " ftp download SOURCE [options]\n" printf " ftp download --help | -h\n" diff --git a/examples/dependencies/cli b/examples/dependencies/cli index 54281743..9b4f3658 100644 --- a/examples/dependencies/cli +++ b/examples/dependencies/cli @@ -16,6 +16,7 @@ cli_usage() { printf "cli - Sample application that requires dependencies\n" echo fi + printf "Usage:\n" printf " cli [command] [options]\n" printf " cli [command] --help | -h\n" @@ -49,6 +50,7 @@ cli_download_usage() { printf "cli download - Download something\n" echo fi + printf "Usage:\n" printf " cli download [options]\n" printf " cli download --help | -h\n" @@ -73,6 +75,7 @@ cli_upload_usage() { printf "cli upload - Upload something\n" echo fi + printf "Usage:\n" printf " cli upload [options]\n" printf " cli upload --help | -h\n" diff --git a/examples/docker-like/docker b/examples/docker-like/docker index 0d349dc3..620342b8 100644 --- a/examples/docker-like/docker +++ b/examples/docker-like/docker @@ -16,6 +16,7 @@ docker_usage() { printf "docker - Docker example\n" echo fi + printf "Usage:\n" printf " docker [command] [options]\n" printf " docker [command] --help | -h\n" @@ -49,6 +50,10 @@ docker_container_usage() { printf "docker container - Container commands\n" echo fi + + printf "Shortcut: c*\n" + echo + printf "Usage:\n" printf " docker container [command] [options]\n" printf " docker container [command] --help | -h\n" @@ -78,6 +83,7 @@ docker_container_run_usage() { printf "docker container run - Run a container\n" echo fi + printf "Usage:\n" printf " docker container run IMAGE [options]\n" printf " docker container run --help | -h\n" @@ -110,6 +116,7 @@ docker_container_stop_usage() { printf "docker container stop - Stop a container\n" echo fi + printf "Usage:\n" printf " docker container stop CONTAINER [options]\n" printf " docker container stop --help | -h\n" @@ -142,6 +149,10 @@ docker_image_usage() { printf "docker image - Image commands\n" echo fi + + printf "Shortcut: i*\n" + echo + printf "Usage:\n" printf " docker image [command] [options]\n" printf " docker image [command] --help | -h\n" @@ -170,6 +181,10 @@ docker_image_ls_usage() { printf "docker image ls - Show all images\n" echo fi + + printf "Shortcut: l\n" + echo + printf "Usage:\n" printf " docker image ls [options]\n" printf " docker image ls --help | -h\n" diff --git a/examples/environment-variables/cli b/examples/environment-variables/cli index 93807422..4c352735 100644 --- a/examples/environment-variables/cli +++ b/examples/environment-variables/cli @@ -16,6 +16,7 @@ cli_usage() { printf "cli - Sample application that requires environment variables\n" echo fi + printf "Usage:\n" printf " cli [command] [options]\n" printf " cli [command] --help | -h\n" @@ -56,6 +57,10 @@ cli_verify_usage() { printf "cli verify - Verify your user\n" echo fi + + printf "Shortcut: v\n" + echo + printf "Usage:\n" printf " cli verify [options]\n" printf " cli verify --help | -h\n" diff --git a/examples/git-like/git b/examples/git-like/git index a796f49c..acfd50ab 100644 --- a/examples/git-like/git +++ b/examples/git-like/git @@ -16,6 +16,7 @@ git_usage() { printf "git - Git example\n" echo fi + printf "Usage:\n" printf " git [command] [options]\n" printf " git [command] --help | -h\n" @@ -49,6 +50,10 @@ git_status_usage() { printf "git status - Show repository status\n" echo fi + + printf "Shortcut: s*\n" + echo + printf "Usage:\n" printf " git status [options]\n" printf " git status --help | -h\n" @@ -73,6 +78,10 @@ git_commit_usage() { printf "git commit - Commit changes\n" echo fi + + printf "Shortcut: c*\n" + echo + printf "Usage:\n" printf " git commit [options]\n" printf " git commit --help | -h\n" diff --git a/examples/minimal/download b/examples/minimal/download index 09161116..f0696d2d 100644 --- a/examples/minimal/download +++ b/examples/minimal/download @@ -24,6 +24,7 @@ download_usage() { printf "download - Sample minimal application without subcommands\n" echo fi + printf "Usage:\n" printf " download SOURCE [TARGET] [options]\n" printf " download --help | -h\n" diff --git a/examples/multiline/multi b/examples/multiline/multi index 2324b37a..af7ba9ca 100644 --- a/examples/multiline/multi +++ b/examples/multiline/multi @@ -18,6 +18,7 @@ multi_usage() { printf "multi - Multiline test\n" echo fi + printf "Usage:\n" printf " multi [command] [options]\n" printf " multi [command] --help | -h\n" @@ -66,6 +67,7 @@ multi_multiline_usage() { printf "multi multiline - Do some multiline work.\n" echo fi + printf "Usage:\n" printf " multi multiline [MY_ARG] [options]\n" printf " multi multiline --help | -h\n" @@ -102,6 +104,7 @@ multi_regular_usage() { printf "multi regular - Standard help line\n" echo fi + printf "Usage:\n" printf " multi regular [REPO] [options]\n" printf " multi regular --help | -h\n" diff --git a/examples/subcommands/cli b/examples/subcommands/cli index 0557791c..0bbe719d 100644 --- a/examples/subcommands/cli +++ b/examples/subcommands/cli @@ -16,6 +16,7 @@ cli_usage() { printf "cli - Sample application\n" echo fi + printf "Usage:\n" printf " cli [command] [options]\n" printf " cli [command] --help | -h\n" @@ -57,6 +58,10 @@ cli_download_usage() { printf "cli download - Download a file\n" echo fi + + printf "Shortcut: d\n" + echo + printf "Usage:\n" printf " cli download SOURCE [TARGET] [options]\n" printf " cli download --help | -h\n" @@ -111,6 +116,10 @@ cli_upload_usage() { printf "cli upload - Upload a file\n" echo fi + + printf "Shortcut: u\n" + echo + printf "Usage:\n" printf " cli upload SOURCE [options]\n" printf " cli upload --help | -h\n" diff --git a/examples/yaml/yaml b/examples/yaml/yaml index 8f9c79e9..11422f88 100644 --- a/examples/yaml/yaml +++ b/examples/yaml/yaml @@ -39,6 +39,7 @@ yaml_usage() { printf "yaml - Sample application that uses the YAML functions\n" echo fi + printf "Usage:\n" printf " yaml FILENAME [VARIABLE] [options]\n" printf " yaml --help | -h\n" diff --git a/lib/bashly/models/environment_variable.rb b/lib/bashly/models/environment_variable.rb index 8d786d0e..2651bbb3 100644 --- a/lib/bashly/models/environment_variable.rb +++ b/lib/bashly/models/environment_variable.rb @@ -3,7 +3,7 @@ module Models class EnvironmentVariable < Base def usage_string(extended: false) result = [name.upcase] - result << "(required)" if required and extended + result << strings[:required] if required and extended result.join " " end end diff --git a/lib/bashly/models/flag.rb b/lib/bashly/models/flag.rb index 396ca3a2..24f815bd 100644 --- a/lib/bashly/models/flag.rb +++ b/lib/bashly/models/flag.rb @@ -14,7 +14,7 @@ def aliases def usage_string(extended: false) result = [aliases.join(", ")] result << arg.upcase if arg - result << "(required)" if required and extended + result << strings[:required] if required and extended result.join " " end end diff --git a/lib/bashly/templates/strings.yml b/lib/bashly/templates/strings.yml index 48f89fa3..4c3259fa 100644 --- a/lib/bashly/templates/strings.yml +++ b/lib/bashly/templates/strings.yml @@ -1,3 +1,6 @@ +# When overriding from the user folder, feel free to delete everything +# you do not plan on overriding to keep the default string. + # Usage captions usage: "Usage:" options: "Options:" @@ -6,6 +9,13 @@ commands: "Commands:" examples: "Examples:" environment_variables: "Environment Variables:" +# Usage helpers +command_shortcut: "Shortcut: %{short}" +default_command_summary: "%{summary} (default)" +required: "(required)" +help_flag: "--help | -h" +version_flag: "--version" + # Fixed flags help text help_flag_text: Show this help version_flag_text: Show version number diff --git a/lib/bashly/views/command/usage.erb b/lib/bashly/views/command/usage.erb index 7528fe44..7a873220 100644 --- a/lib/bashly/views/command/usage.erb +++ b/lib/bashly/views/command/usage.erb @@ -14,15 +14,21 @@ printf "<%= caption_string %>\n" echo fi + + <%- if short -%> + printf "<%= strings[:command_shortcut] % { short: short } %>\n" + echo + <%- end -%> + printf "<%= strings[:usage] %>\n" printf " <%= usage_string %>\n" <%- if commands.any? -%> printf " <%= full_name %> [command] --help | -h\n" <%- else -%> - printf " <%= full_name %> --help | -h\n" + printf " <%= full_name %> <%= strings[:help_flag] %>\n" <%- end -%> <%- if root_command? -%> - printf " <%= full_name %> --version\n" + printf " <%= full_name %> <%= strings[:version_flag] %>\n" <%- end -%> echo <%= render(:usage_commands).indent 2 if commands.any? %> diff --git a/lib/bashly/views/command/usage_commands.erb b/lib/bashly/views/command/usage_commands.erb index 30c8cb4d..5938473f 100644 --- a/lib/bashly/views/command/usage_commands.erb +++ b/lib/bashly/views/command/usage_commands.erb @@ -3,7 +3,7 @@ printf "<%= strings[:commands] %>\n" <%- maxlen = command_names.map(&:size).max -%> <%- commands.each do |command| -%> <%- summary = command.summary -%> -<%- summary = "#{summary} (default)" if command.default -%> +<%- summary = strings[:default_command_summary] % { summary: summary } if command.default -%> echo " <%= command.name.ljust maxlen %> <%= summary %>" <%- end -%> echo diff --git a/spec/approvals/examples/docker-like b/spec/approvals/examples/docker-like index d3baa66d..89f7cd87 100644 --- a/spec/approvals/examples/docker-like +++ b/spec/approvals/examples/docker-like @@ -40,6 +40,8 @@ Options: + ./docker container docker container - Container commands +Shortcut: c* + Usage: docker container [command] [options] docker container [command] --help | -h @@ -75,6 +77,8 @@ args: + ./docker con docker container - Container commands +Shortcut: c* + Usage: docker container [command] [options] docker container [command] --help | -h @@ -86,6 +90,8 @@ Commands: + ./docker image docker image - Image commands +Shortcut: i* + Usage: docker image [command] [options] docker image [command] --help | -h diff --git a/spec/approvals/examples/environment-variables b/spec/approvals/examples/environment-variables index 7f458e1e..e757ce0c 100644 --- a/spec/approvals/examples/environment-variables +++ b/spec/approvals/examples/environment-variables @@ -40,6 +40,8 @@ Environment Variables: + ./cli verify -h cli verify - Verify your user +Shortcut: v + Usage: cli verify [options] cli verify --help | -h diff --git a/spec/approvals/examples/git-like b/spec/approvals/examples/git-like index 71f09a2d..d33e40ae 100644 --- a/spec/approvals/examples/git-like +++ b/spec/approvals/examples/git-like @@ -44,6 +44,8 @@ args: + ./git status -h git status - Show repository status +Shortcut: s* + Usage: git status [options] git status --help | -h @@ -55,6 +57,8 @@ Options: + ./git commit -h git commit - Commit changes +Shortcut: c* + Usage: git commit [options] git commit --help | -h diff --git a/spec/approvals/examples/subcommands b/spec/approvals/examples/subcommands index 30f6b7ac..34af25a6 100644 --- a/spec/approvals/examples/subcommands +++ b/spec/approvals/examples/subcommands @@ -45,6 +45,8 @@ Environment Variables: + ./cli download -h cli download - Download a file +Shortcut: d + Usage: cli download SOURCE [TARGET] [options] cli download --help | -h @@ -85,6 +87,8 @@ args: + ./cli upload --help cli upload - Upload a file +Shortcut: u + Usage: cli upload SOURCE [options] cli upload --help | -h