Class: Kramdown::Man::CLI Private
- Inherits:
-
Object
- Object
- Kramdown::Man::CLI
- Defined in:
- lib/kramdown/man/cli.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Represents the kramdown-man
command's logic.
Constant Summary collapse
- PROGRAM_NAME =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The program name.
"kramdown-man"
- BUG_REPORT_URL =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The URL to report bugs to.
"https://github.com/postmodern/kramdown-man/issues/new"
- MAN_PAGE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The path to the man/ directory.
File.join(__dir__,'..','..','..','man','kramdown-man.1')
Instance Attribute Summary collapse
-
#option_parser ⇒ OptionParser
readonly
private
The option parser.
-
#output ⇒ String?
readonly
private
The optional output file to write to.
Class Method Summary collapse
-
.run(argv) ⇒ Integer
private
Initializes and runs the command.
Instance Method Summary collapse
-
#initialize ⇒ CLI
constructor
private
Initializes the command.
-
#print_backtrace(exception) ⇒ Object
private
Prints a backtrace to stderr.
-
#print_error(error) ⇒ Object
private
Prints an error message to stderr.
-
#run(argv = ARGV) ⇒ Integer
private
Runs the command.
-
#view_man_page(man_page) ⇒ Object
private
Displays the man page using the
man
command.
Constructor Details
#initialize ⇒ CLI
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initializes the command.
39 40 41 42 43 |
# File 'lib/kramdown/man/cli.rb', line 39 def initialize @option_parser = option_parser @output = nil end |
Instance Attribute Details
#option_parser ⇒ OptionParser (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The option parser.
29 30 31 |
# File 'lib/kramdown/man/cli.rb', line 29 def option_parser @option_parser end |
#output ⇒ String? (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The optional output file to write to.
34 35 36 |
# File 'lib/kramdown/man/cli.rb', line 34 def output @output end |
Class Method Details
.run(argv) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initializes and runs the command.
54 55 56 57 58 59 60 61 62 |
# File 'lib/kramdown/man/cli.rb', line 54 def self.run(argv) new().run(argv) rescue Interrupt # https://tldp.org/LDP/abs/html/exitcodes.html return 130 rescue Errno::EPIPE # STDOUT pipe broken return 0 end |
Instance Method Details
#print_backtrace(exception) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Prints a backtrace to stderr.
191 192 193 194 195 196 197 198 |
# File 'lib/kramdown/man/cli.rb', line 191 def print_backtrace(exception) $stderr.puts "Oops! Looks like you've found a bug!" $stderr.puts "Please report the following text to: #{BUG_REPORT_URL}" $stderr.puts $stderr.puts "```" $stderr.puts "#{exception.}" $stderr.puts "```" end |
#print_error(error) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Prints an error message to stderr.
181 182 183 |
# File 'lib/kramdown/man/cli.rb', line 181 def print_error(error) $stderr.puts "#{PROGRAM_NAME}: #{error}" end |
#run(argv = ARGV) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Runs the command.
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 |
# File 'lib/kramdown/man/cli.rb', line 73 def run(argv=ARGV) argv = begin @option_parser.parse(argv) rescue OptionParser::ParseError => error print_error(error.) return -1 end markdown = case argv.length when 1 path = argv[0] unless File.file?(path) print_error "no such file or directory: #{path}" return -1 end File.read(path) when 0 print_error "a MARKDOWN_FILE argument is required" return -1 else print_error "too many arguments given" return -1 end doc = Kramdown::Document.new(markdown) man_page = doc.to_man if @output File.write(@output,man_page) elsif $stdout.tty? view_man_page(man_page) else puts man_page end return 0 rescue => error print_backtrace(error) return -1 end |
#view_man_page(man_page) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Displays the man page using the man
command.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/kramdown/man/cli.rb', line 122 def view_man_page(man_page) io = IO.popen(%w[man -l -],'w') pid = io.pid begin io.puts man_page rescue Errno::EPIPE ensure io.close begin Process.waitpid(pid) rescue Errno::EPIPE, Errno::ECHILD end end end |