Class: GitlogMD::ArgumentParser

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlog-md/argument_parser.rb

Constant Summary collapse

DEFAULTS =
{ :output_file => 'HISTORY.md',
             :branch      => 'master',
             :repo_dir    => '.',
}

Instance Method Summary collapse

Constructor Details

#initializeArgumentParser

Returns a new instance of ArgumentParser.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gitlog-md/argument_parser.rb', line 12

def initialize
  @cmd_options = {}

  @optparse = OptionParser.new do | opts |
    # Set a banner
    opts.banner = "Usage: #{File.basename($0)} [options...]"

    opts.on "-o", "--output-file FILE",
            "File to write MarkDown output to",
            "(default #{DEFAULTS[:output_file]})"  do |file|
      @cmd_options[:output_file] = file
    end

    opts.on "-b", "--branch BRANCH",
            "Branch to read git log history from",
            "(default #{DEFAULTS[:branch]})"  do |file|
      @cmd_options[:branch] = file
    end

    opts.on "-r", "--repo-dir DIR",
            "Path to github repo",
            "(default #{DEFAULTS[:repo_dir]})"  do |file|
      @cmd_options[:repo_dir] = file
    end

    opts.on("--version", "Report currently running version of GitlogMD" ) do
      @cmd_options[:version] = true
    end

    opts.on("--help", "Display this screen" ) do
      @cmd_options[:help] = true
    end
  end

end

Instance Method Details

#parse(args = ARGV) ⇒ Hash

Parse an array of arguments into a Hash of options, use default values for unset arguments

Examples:

args = ['--option', 'value', '--option2', 'value2', '--switch']
parser = ArgumentParser.new
parser.parse(args) == {:option => 'value, :options2 => value, :switch => true}

Parameters:

  • args (Array) (defaults to: ARGV)

    The array of arguments to consume

Returns:

  • (Hash)

    Return the Hash of options



58
59
60
61
# File 'lib/gitlog-md/argument_parser.rb', line 58

def parse( args = ARGV )
  @optparse.parse(args)
  DEFAULTS.merge(@cmd_options)
end

#usageString

Generate a string representing the supported arguments

Examples:

parser = ArgumentParser.new
parser.usage = "Options:  ..."

Returns:

  • (String)

    Return a string representing the available arguments



70
71
72
# File 'lib/gitlog-md/argument_parser.rb', line 70

def usage
  @optparse.help
end