summaryrefslogtreecommitdiff
path: root/cli/source/core/scala/com/rockymadden/stringmetric/cli/phonetic/soundexAlgorithm.scala
blob: 45e78d3f6e81098b3d4b7e9839733c41a8437cc1 (plain) (blame)
1
2
3
4
5
6
7
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
package com.rockymadden.stringmetric.cli.phonetic

import com.rockymadden.stringmetric.cli._
import com.rockymadden.stringmetric.phonetic.SoundexAlgorithm

/**
 * The soundexAlgorithm [[com.rockymadden.stringmetric.cli.Command]]. Returns the phonetic representation of the passed
 * string, per the Soundex algorithm.
 */
object soundexAlgorithm extends Command {
	override def main(args: Array[String]): Unit = {
		val options = OptionMap(args)

		try
			if (options.contains('h) || options.contains('help)) {
				help()
				exit(options)
			} else if (options.contains('dashless) && (options('dashless): OptionMapArray).length == 1) {
				execute(options)
				exit(options)
			} else throw new IllegalArgumentException("Expected valid syntax. See --help.")
		catch {
			case e: Throwable => error(e, options)
		}
	}

	override def help(): Unit = {
		val ls = sys.props("line.separator")
		val tab = "  "

		println(
			"Returns the phonetic representation of the passed string, per the Soundex algorithm." + ls + ls +
			"Syntax:" + ls +
			tab + "soundexAlgorithm [Options] string..." + ls + ls +
			"Options:" + ls +
			tab + "-h, --help" + ls +
			tab + tab + "Outputs description, syntax, and options."
		)
	}

	override def execute(options: OptionMap): Unit =
		println(SoundexAlgorithm.compute(options('dashless)).getOrElse("not computable"))
}