Class: Khaleesi::CLI

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

Direct Known Subclasses

About, Construction, CreatePost, Generate, Help, Produce, Version

Defined Under Namespace

Classes: About, Build, Construction, CreatePost, FileReader, Generate, Help, Produce, Version

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CLI

Returns a new instance of CLI.



23
24
# File 'lib/khaleesi/cli.rb', line 23

def initialize(options={})
end

Class Method Details

.class_from_arg(arg) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/khaleesi/cli.rb', line 35

def self.class_from_arg(arg)
  case arg
    when 'construction', 'c'
      Construction
    when 'createpost', 'cp'
      CreatePost
    when 'generate', 'g'
      Generate
    when 'version', 'v'
      Version
    when 'produce', 'p'
      Produce
    when 'build', 'b'
      Build
    when 'help', 'h'
      Help
    else
      About
  end
end

.doc {|'usage: khaleesi [command] [args...]'| ... } ⇒ Object

Yields:

  • ('usage: khaleesi [command] [args...]')


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/khaleesi/cli.rb', line 5

def self.doc
  return enum_for(:doc) unless block_given?

  yield 'usage: khaleesi [command] [args...]'
  yield ''
  yield 'where <command> is one of:'
  yield %|	help\|h          #{Help.desc}|
  yield %|	version\|v       #{Version.desc}|
  yield %|	about           #{About.desc}|
  yield %|	produce\|p       #{Produce.desc}|
  yield %|	construction\|c  #{Construction.desc}|
  yield %|	createpost\|cp   #{CreatePost.desc}|
  yield %|	generate\|g      #{Generate.desc}|
  yield %|	build\|b         #{Build.desc}|
  yield ''
  yield 'See `khaleesi help <command>` for more info.'
end

.normalize_syntax(argv) ⇒ Object



625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
# File 'lib/khaleesi/cli.rb', line 625

def self.normalize_syntax(argv)
  out = []
  argv.each do |arg|
    case arg
      when /^(-{,2})(\p{Graph}+)=(.*)$/
        out << $2 << $3
      when /^(-{,2})(\p{Graph}+)$/
        out << $2
      else
        out << arg
    end
  end

  out
end

.parse(argv = ARGV) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/khaleesi/cli.rb', line 26

def self.parse(argv=ARGV)
  argv = normalize_syntax(argv)

  mode = argv.shift

  klass = class_from_arg(mode)
  klass.parse(argv)
end

Instance Method Details

#create_file_p(dir, name, extension) ⇒ Object



617
618
619
620
621
622
623
# File 'lib/khaleesi/cli.rb', line 617

def create_file_p(dir, name, extension)
  unless File.directory?(dir)
    FileUtils.mkdir_p(dir)
  end

  "#{dir}/#{name}" + (extension.empty? ? '' : ".#{extension}")
end