aboutsummaryrefslogtreecommitdiff
path: root/lib/leap_cli/markdown_document_listener.rb
blob: c25a24316ee8cdcf0cf6b9a1f24333327d0c0a14 (plain)
1
2
3
4
5
6
7
8
9
10
11
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#
# A class to generate a markdown file with all the information available with the
# help subcommand.
#
# This is adapted from GLI::Commands::RdocDocumentListener
#

require 'stringio'
require 'gli/commands/help_modules/arg_name_formatter'

module LeapCli
  class MarkdownDocumentListener

    def initialize(global_options,options,arguments,app)
      @io = File.new(File.basename($0) + ".md",'w')
      @nest = ''
      @commands = [File.basename($0)]
      @arg_name_formatter = GLI::Commands::HelpModules::ArgNameFormatter.new
    end

    def beginning
    end

    # Called when processing has completed
    def ending
      @io.close
    end

    # Gives you the program description
    def program_desc(desc)
      @io.puts "@title = 'Command Line Reference'"
      #@io.puts "# #{File.basename($0)} - #{desc}"
      @io.puts
    end

    def program_long_desc(desc)
      @io.puts desc
      @io.puts
    end

    # Gives you the program version
    def version(version)
      #@io.puts "v#{version}"
      #@io.puts
    end

    def options
      #@io.puts "<div class='options'>"
      @io.puts
      if @nest.size == 0
        @io.puts "# Global Options"
      else
        #@io.puts "#{@nest}# Options"
        @io.puts "**Options**"
      end
      @io.puts
    end

    # Gives you a flag in the current context
    def flag(name,aliases,desc,long_desc,default_value,arg_name,must_match,type)
      invocations = ([name] + Array(aliases)).map { |_| add_dashes(_) }.join('|')
      usage = "#{invocations} #{arg_name || 'arg'}"
      #@io.puts "#{@nest}## #{usage}"
      @io.puts "* `#{usage}`  "
      @io.puts String(desc).strip + "  "
      @io.puts String(long_desc).strip + "  " if long_desc
      @io.puts "Default Value: #{default_value || 'None'}  "
      @io.puts "Must Match: #{must_match.to_s}  " unless must_match.nil?
      @io.puts
    end

    # Gives you a switch in the current context
    def switch(name,aliases,desc,long_desc,negetable)
      if negetable
        name = "[no-]#{name}" if name.to_s.length > 1
        aliases = aliases.map { |_|  _.to_s.length > 1 ? "[no-]#{_}" : _ }
      end
      invocations = ([name] + aliases).map { |_| add_dashes(_) }.join('|')
      #@io.puts "#{@nest}## #{invocations}"
      @io.puts "* `#{invocations}`  "
      @io.puts String(desc).strip + "  "
      #@io.puts
      #@io.puts String(long_desc).strip
      @io.puts
    end

    def end_options
      #@io.puts "</div>"
    end

    def commands
      #@io.puts "#{@nest}## Commands"
      #@nest = "#{@nest}#"
    end

    # Gives you a command in the current context and creates a new context of this command
    def command(name,aliases,desc,long_desc,arg_name,arg_options)
      @commands.push(name)
      #@io.puts "#{@nest}## Command: <tt>#{([name] + aliases).join('|')} #{@arg_name_formatter.format(arg_name,arg_options)}</tt>"
      @io.puts
      @io.puts "#{@nest}# #{@commands.join ' '} #{@arg_name_formatter.format(arg_name,arg_options)}"
      @io.puts
      @io.puts String(desc).strip
      @io.puts
      @io.puts String(long_desc).strip
      @nest = "#{@nest}#"
    end

    # Ends a command, and "pops" you back up one context
    def end_command(name)
      @nest.gsub!(/\#$/,'')
      @commands.pop
    end

    # Gives you the name of the current command in the current context
    def default_command(name)
      @io.puts "Default Command: #{name}" unless name.nil?
    end

    def end_commands
      @nest.gsub!(/\#$/,'')
    end

  private

    def add_dashes(name)
      name = "-#{name}"
      name = "-#{name}" if name.length > 2
      name
    end


  end
end