Class: PrettyDoc::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/pretty_doc/cli.rb

Constant Summary collapse

DEFAULT_OUTPUT_DIR =
'./out'

Class Method Summary collapse

Class Method Details

.run!(args) ⇒ Object



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
# File 'lib/pretty_doc/cli.rb', line 9

def run!(args)
  options = OpenStruct.new
  options.template = 'default'
  options.output = output_dir(DEFAULT_OUTPUT_DIR)
  options.files = []
  options.enable_line_numbers = false

  opts_parser = OptionParser.new do |opts|
    opts.banner = 'Pretty document empowered by markdown language.'
    opts.separator ''
    opts.separator 'Usage: pretty_doc [options] files'
    opts.separator ''
    opts.separator 'Options:'
    opts.separator ''

    opts.on_tail('-v', '--version', 'output the version number') do
      puts PrettyDoc::VERSION
      exit
    end

    opts.on_tail('-h', '--help', 'output usage information') do
      puts opts
      exit
    end

    opts.on('-o', '--output [path]', 'output to a given folder, default is `./out`') do |o|
      options.output = output_dir(o || DEFAULT_OUTPUT_DIR)

    end

    opts.on('-t', '--template [folder]', 'choose a template') do |tmpl|
      options.template = tmpl
    end

    opts.on('-l', '--line-numbers', 'enable line numbers for code highlight') do
      options.enable_line_numbers = true
    end
  end

  opts_parser.parse!(args)

  if args.length == 0
    options.files = Dir.glob('./*.md')
  else
    args.each do |arg|
      if File.directory? arg
        arg = File.join(File.expand_path(arg), '*.md')
      end
      options.files.concat(Dir.glob(arg))
    end
  end

  if options.files.length == 0
    puts 'No file found'
    puts opts_parser
    exit
  end

  options.template = PrettyDoc.template(options.template)
  PrettyDoc::Resource::Source.build(options)
end