Class: YJNCopycat::CommandParser

Inherits:
Object
  • Object
show all
Defined in:
lib/yjncopycat/commandparser.rb

Class Method Summary collapse

Class Method Details

.parse(args) ⇒ Object



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
# File 'lib/yjncopycat/commandparser.rb', line 8

def self.parse(args)
    if args.length == 0
        puts YJNCopycat::USAGE
        exit 0
    end

    options = {}
    parser = OptionParser.new do |parser|
        parser.banner = "Usage: yjncopycat [options]"
        
        parser.on('--url URL', 'This is the git URL') do |url|
            options[:url] = url
        end
        parser.on('--name NAME', 'This is the new name for the project') do |name|
            options[:name] = name
        end
        parser.on_tail('-v', '--version', 'Shows version') do
            puts YJNCopycat::VERSION
            exit 0
        end
        parser.on_tail('-h', '--help', 'Shows simple usage instructions.') do
            puts YJNCopycat::USAGE
            exit 0
        end
    end

    begin
        parser.parse!(args)
    rescue SystemExit => e
        exit 0
    rescue Exception => e
        puts "ERROR: #{e}!".red 
        exit 1
    end

    unless options.key?(:url)
        puts "ERROR: Missing --url option!".red
        exit 1
    end

    unless options[:url] =~ URI::regexp
        puts "ERROR: Invalid value for --url option. Please input a valid URL of your git repo.".red
        exit 1
    end

    unless options.key?(:name)
        puts "ERROR: Missing --name options!".red
        exit 1
    end

    if options[:name].length < 3
        puts "ERROR: Invalid value for --name option. Make sure string is at least 3 characters.".red
        exit 1
    end

    unless options[:name] =~ /\w/
        puts "ERROR: Make sure there's no whitespace in --name parameter.".red
        exit 0
    end 

    options
end