Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: free form --help text #120

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
11 changes: 10 additions & 1 deletion lib/dry/cli/banner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def self.call(command, name)
command_subcommands(command),
command_arguments(command),
command_options(command),
command_examples(command, name)
command_examples(command, name),
command_help(command)
].compact.join("\n")
end

Expand Down Expand Up @@ -52,6 +53,14 @@ def self.command_examples(command, name)
"\nExamples:\n#{command.examples.map { |example| " #{name} #{example}" }.join("\n")}"
end

# @since 0.8.0
# @api private
def self.command_help(command)
return if command.help.nil?

"\n#{command.help.()}"
end

# @since 0.1.0
# @api private
def self.command_description(command)
Expand Down
42 changes: 42 additions & 0 deletions lib/dry/cli/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def self.inherited(base)
base.class_eval do
@_mutex = Mutex.new
@description = nil
@help = nil
@examples = []
@subcommands = []
@arguments = base.superclass_arguments || []
Expand Down Expand Up @@ -108,6 +109,46 @@ def self.example(*examples)
@examples += examples.flatten(1)
end

# Add extra free text when `--help` flag is used.
#
# @param blk [Proc] a block that must return a free form string to add
# extra information
#
# @since 0.8.0
#
# @example
# require "dry/cli"
#
# class Convert < Dry::CLI::Command
# help do
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you though to support just help "String" too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rngtng Yeah, that could work too.

# require "tzinfo"
#
# "Available timezones:\n\n" +
# TZInfo::Timezone.all_identifiers.map { |i| "\t#{i}\n" }.join
# end
#
# def call(*)
# # ...
# end
# end
#
# # $ time convert --help
# # # ...
# #
# # Available timezones:
# #
# # Africa/Adibdjan
# # Africa/Accra
# # ...
# # Zulu
def self.help(&blk)
if blk
@help = blk
else
@help
end
end

# Specify an argument
#
# @param name [Symbol] the argument name
Expand Down Expand Up @@ -375,6 +416,7 @@ def self.superclass_options
delegate %i[
description
examples
help
arguments
options
params
Expand Down