Module: License::CommandLine

Defined in:
lib/license/compatibility.rb

Class Method Summary collapse

Class Method Details

.parse(args) ⇒ Object



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
115
116
117
118
119
120
121
122
123
124
# File 'lib/license/compatibility.rb', line 86

def self.parse(args)
  options = {}
  option_parser = OptionParser.new do |opts|
    opts.banner = "#{USAGE}\n\n"
    opts.banner += "Arguments:\n"
    opts.banner += "    List of licenses or list of package:license couples (separated by ':').\n"
    opts.banner += "    Example: 'MIT' 'GPL-3.0' or 'my_package:ISC' 'other_pkg:BSD-2-Clause'.\n"
    opts.banner += "    Mixing the two formats is not allowed.\n"
    opts.banner += "    Additional args after a --read option are accepted.\n\n"
    opts.banner += "Options:"

    opts.on('-l', '--list', 'Print the list of supported licenses.') do
      options[:list] = License::Compatibility.license_data
      return options
    end

    opts.on('-r', '--read FILE', 'Read arguments from file.') do |file|
      unless File.exist?(file)
        raise Errno::ENOENT, "#{file}"
      end
      unless File.file?(file)
        raise ArgumentError, "#{file} is a directory, you must specify a regular file"
      end
      options[:read] = file
    end

    opts.on('-v', '--version', "Show the program version (#{License::Compatibility::VERSION}).") do
      options[:version] = "license-compatibility v#{License::Compatibility::VERSION}"
      return options
    end

    opts.on('-h', '--help', 'Print this help.') do
      options[:help] = opts.to_s
      return options
    end
  end
  option_parser.parse!(args)
  return options
end

.parse_positional(args) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/license/compatibility.rb', line 126

def self.parse_positional(args)
  licenses = false
  packages = false
  prepared = []

  args.each { |arg|
    split = arg.split(':', 2)
    if split.length == 2
      prepared.push(split)
      packages = true
    else
      prepared += split
      licenses = true
    end
    raise ArgumentError, 'do not mix license and package:license arguments' if (licenses && packages)
  }

  if packages
    return 'packages', prepared
  elsif licenses
    return 'licenses', prepared
  else
    return 'unknown', prepared
  end
end