Class: Cosensee::CLI::Parser

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

Overview

Parser class for Cosensee::CLI

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Parser

Returns a new instance of Parser.



15
16
17
18
19
# File 'lib/cosensee/cli/parser.rb', line 15

def initialize(args)
  @args = args
  @option = Option.new
  @op = OptionParser.new
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



9
10
11
# File 'lib/cosensee/cli/parser.rb', line 9

def args
  @args
end

#opObject (readonly)

Returns the value of attribute op.



9
10
11
# File 'lib/cosensee/cli/parser.rb', line 9

def op
  @op
end

#optionObject (readonly)

Returns the value of attribute option.



9
10
11
# File 'lib/cosensee/cli/parser.rb', line 9

def option
  @option
end

Class Method Details

.parse(args) ⇒ Object



11
12
13
# File 'lib/cosensee/cli/parser.rb', line 11

def self.parse(args)
  new(args).parse
end

Instance Method Details

#parseObject



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
# File 'lib/cosensee/cli/parser.rb', line 21

def parse
  begin
    op.banner = 'Usage: bin/build [-f <filename>] [-r <project_name>]'
    op.version = Cosensee::VERSION

    op.on('-f FILENAME', '--file FILENAME', 'Specify the file name') do |filename|
      option.filename = filename
    end
    op.on('-r PROJECT_NAME', '--remote PROJECT_NAME', 'Retrieve the project pages file from the remote page-data API (with env var `CONNECT_SID`)') do |project_name|
      option.remote = project_name
    end
    op.on('-p PORT', '--port PORT', "Specify port number of web server (default: #{DEFAULT_PORT})") do |port|
      option.port = port
    end
    op.on('-d OUTPUT_DIR', '--dir OUTPUT_DIR', "Specify directory name of generated html files(default: #{DEFAULT_OUTPUT_DIR})") do |output_dir|
      option.output_dir = output_dir
    end
    op.on('--css-dir CSS_DIR', "Specify directory name of generated html files(default: #{DEFAULT_CSS_DIR})") do |css_dir|
      option.css_dir = css_dir
    end
    op.on('-s', '--server', 'Serves files by running a web server locally.') do
      option.server = true
    end

    op.on('--skip-tailwind', 'Skip TailwindCSS execution') do
      option.skip_tailwind_execution = true
    end

    op.on('--clean', 'Make dist directory clean') do
      option.clean = true
    end

    op.on('--init PROJECT_DIR', 'Initialize the project with default files') do |project_dir|
      option.init = project_dir
    end

    op.parse!(args)
  rescue OptionParser::MissingArgument => e
    return option_error("Error: option requires an argument: #{e.args.join(' ')}")
  rescue OptionParser::InvalidOption => e
    return option_error("Error: invalid option: #{e.args.join(' ')}")
  end

  if !option.filename && !option.server? && !option.init?
    return option_error('Error: filename not found. You must specify -f, or use server with -s.')
  elsif !option.filename && option.remote?
    return option_error('Error: project_name not found. You must not specify project name -p without -f.')
  end

  option
end