Skip to content

Commit 213ca87

Browse files
committed
Adds better documentation to CLI, Command, and git plugin
1 parent bf11542 commit 213ca87

File tree

3 files changed

+55
-15
lines changed

3 files changed

+55
-15
lines changed

lib/git_commander/cli.rb

+18-6
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
require "optparse"
66

77
module GitCommander
8-
# @abstract Manages command line execution within the context of GitCommander
8+
# Manages mapping commands and their arguments that are run from the command-line (via `git-cmd`)
9+
# to their corresponding git-commander registered commands.
10+
#
11+
# @example Run a registered "start" command with a single argument
12+
# GitCommander::CLI.new.run ["start", "new-feature""]
13+
#
914
class CLI
1015
attr_reader :output, :registry
1116

1217
# @param registry [GitCommander::Registry] (GitCommander::Registry.new) the
13-
# command registry to use for matching available commands
18+
# command registry to use for matching available commands
1419
# @param output [IO] (STDOUT) the IO object you want to use to send output to when running commands
1520
def initialize(registry: GitCommander::Registry.new, output: STDOUT)
1621
@registry = registry
@@ -26,9 +31,7 @@ def run(args = ARGV)
2631
options = parse_command_options!(command, arguments)
2732
command.run options
2833
rescue Registry::CommandNotFound
29-
GitCommander.logger.error <<~ERROR_LOG
30-
#{command} not found in registry. Available commands: #{registry.commands.keys.inspect}
31-
ERROR_LOG
34+
log_command_not_found(command)
3235

3336
help
3437
rescue StandardError => e
@@ -40,7 +43,10 @@ def say(message)
4043
output.puts message
4144
end
4245

43-
# Parses ARGV for the provided git-cmd command name
46+
# Parses an array of values (as ARGV would provide) for the provided git-cmd command name.
47+
# The +arguments+ are run through Ruby's [OptionParser] for validation and
48+
# then filtered through the +command+ to extract it's options with any
49+
# default values.
4450
#
4551
# @param command [Command] the git-cmd command to parse the arguments for
4652
# @param arguments [Array] the command line arguments
@@ -74,6 +80,12 @@ def help
7480
say registry.commands.keys.join(", ")
7581
end
7682

83+
def log_command_not_found(command)
84+
GitCommander.logger.error <<~ERROR_LOG
85+
#{command} not found in registry. Available commands: #{registry.commands.keys.inspect}
86+
ERROR_LOG
87+
end
88+
7789
def configure_option_parser_for_command(command)
7890
valid_arguments_for_command = command.arguments.map { |arg| "[#{arg.name}]" }.join(" ")
7991

lib/git_commander/command.rb

+36-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
require_relative "command_loader_options"
88

99
module GitCommander
10-
# @abstract Wraps domain logic for executing git-cmd commands
10+
# Wraps domain logic for executing git-cmd Commands
1111
class Command
1212
include GitCommander::CommandLoaderOptions
1313

@@ -16,20 +16,25 @@ class Command
1616

1717
# @param name [String, Symbol] the name of the command
1818
# @param registry [GitCommander::Registry] (GitCommander::Registry.new) the
19-
# command registry to use lookups
19+
# command registry to use lookups
2020
# @param options [Hash] the options to create the command with
21+
#
2122
# @option options [String] :description (nil) a short description to use in the
22-
# single line version of the command's help output
23+
# single line version of the command's help output
2324
# @option options [String] :summary (nil) the long-form description of the command
24-
# to use in the command's help output
25+
# to use in the command's help output
2526
# @option options [IO] :output (STDOUT) the IO object you want to use to
26-
# send outut from the command to
27+
# send outut from the command to
2728
# @option options [Array] :arguments an array of hashes describing the
28-
# argument names and default values that can be supplied to the command
29+
# argument names and default values that can be supplied to the command
2930
# @option options [Array] :flags an array of hashes describing the
30-
# flags and default values that can be supplied to the command
31+
# flags and default values that can be supplied to the command
3132
# @option options [Array] :switches an array of hashes describing the
32-
# switches and default values that can be supplied to the command
33+
# switches and default values that can be supplied to the command
34+
#
35+
# @yieldparam [Array<Option>] run_options an Array of
36+
# Option instances defined from the above +options+
37+
#
3338
def initialize(name, registry: nil, **options, &block)
3439
@name = name
3540
@description = options[:description]
@@ -43,16 +48,23 @@ def initialize(name, registry: nil, **options, &block)
4348

4449
# Executes the block for the command with the provided run_options.
4550
#
46-
# @param run_options
51+
# @param run_options [Array<Option>] an array of Option(s) to pass to the {#block} of this Command
52+
#
4753
def run(run_options = [])
4854
assign_option_values(run_options)
4955
Runner.new(self).run options.map(&:to_h).reduce(:merge)
5056
end
5157

58+
# Appends the +message+ to the Command's {#output}
59+
#
60+
# @param message [String] the string to append to the {#output}
61+
#
5262
def say(message)
5363
output.puts message
5464
end
5565

66+
# Adds command-line help text to the {#output} of this Command
67+
#
5668
def help
5769
say "NAME"
5870
say " git-cmd #{name}#{summary}"
@@ -63,10 +75,25 @@ def help
6375
options_help
6476
end
6577

78+
# Access to a unique Set of this Command's {#arguments}, {#flags}, and {#switches}
79+
#
80+
# @return [Set] a unique list of all options this command can accept
81+
#
6682
def options
6783
Set.new(@arguments + @flags + @switches)
6884
end
6985

86+
# Add to this Command's {#arguments}, {#flags}, or {#switches}
87+
#
88+
# @param option_type [String, Symbol] the type of option to add
89+
# @param options [Hash] the options to create the [Option] with
90+
#
91+
# @option options [String, Symbol] :name the name of the option to add
92+
# @option options [Object] :default (nil) the default value of the Option
93+
# @option options [String] :description (nil) a description of the Option to use in
94+
# help text output
95+
# @option options [Object] :value (nil) the value on of the Option
96+
#
7097
def add_option(option_type, options = {})
7198
case option_type.to_sym
7299
when :argument

lib/git_commander/plugins/git.rb

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
CONFIG_FILE_PATH = "#{ENV["HOME"]}/.gitconfig.commander"
99

10+
# @private
1011
# Overrides Rugged::Repository#global_config so that we can use a custom config
1112
# file for all git-commander related configurations
1213
class RuggedRepositoryWithCustomConfig < SimpleDelegator

0 commit comments

Comments
 (0)